{"id":25632,"url":"https://patchwork.libcamera.org/api/patches/25632/?format=json","web_url":"https://patchwork.libcamera.org/patch/25632/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20260106165754.1759831-3-barnabas.pocze@ideasonboard.com>","date":"2026-01-06T16:57:34","name":"[v4,02/22] libcamera: controls: Add `ControlValueView`","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"11aa0fa2ddc4b381d66fc7a37b7b4fcca754ccfa","submitter":{"id":216,"url":"https://patchwork.libcamera.org/api/people/216/?format=json","name":"Barnabás Pőcze","email":"barnabas.pocze@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/25632/mbox/","series":[{"id":5688,"url":"https://patchwork.libcamera.org/api/series/5688/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=5688","date":"2026-01-06T16:57:32","name":"libcamera: Add `MetadataList`","version":4,"mbox":"https://patchwork.libcamera.org/series/5688/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/25632/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/25632/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 7E70FBDCBF\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue,  6 Jan 2026 16:58:08 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 200AA61FD1;\n\tTue,  6 Jan 2026 17:58:02 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 8505B61F9F\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  6 Jan 2026 17:57:58 +0100 (CET)","from pb-laptop.local (185.221.143.114.nat.pool.zt.hu\n\t[185.221.143.114])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 5D2CAF01;\n\tTue,  6 Jan 2026 17:57:37 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"NJkPujGN\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1767718657;\n\tbh=T4amu+oRL1icmh4Aw3kyGhF5Ips5bMN2VLJrgGtAuw8=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=NJkPujGNSNXtmoFWSoQUJVF3ggEyIGQyaA4f/5T7eIMpH91JFOj5i+FwvjRXBeZC/\n\tyhK0EZ7taG+l3ZOX+yehSZs7oemEiesGHx/KBwpCfA7PlyDVF6CfFq52WdaFz73yZs\n\tFWUkAKZ73MmburXFhCjmIs4a1KiQGYKSUzCbqBjA=","From":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <barnabas.pocze@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Paul Elder <paul.elder@ideasonboard.com>,\n\tKieran Bingham <kieran.bingham@ideasonboard.com>","Subject":"[PATCH v4 02/22] libcamera: controls: Add `ControlValueView`","Date":"Tue,  6 Jan 2026 17:57:34 +0100","Message-ID":"<20260106165754.1759831-3-barnabas.pocze@ideasonboard.com>","X-Mailer":"git-send-email 2.52.0","In-Reply-To":"<20260106165754.1759831-1-barnabas.pocze@ideasonboard.com>","References":"<20260106165754.1759831-1-barnabas.pocze@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Add `ControlValueView`, which is a non-owning read-only handle to a control\nvalue with a very similar interface.\n\nSigned-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>\nReviewed-by: Paul Elder <paul.elder@ideasonboard.com>\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n---\nchanges in v3:\n  * reword some documentation\n\nchanges in v2:\n  * rewrite `ControlValue::toString()` in terms of `operator<<(std::ostream&, const ControlValueView&)\n    to avoid duplication\n---\n include/libcamera/controls.h |  68 +++++++++\n src/libcamera/controls.cpp   | 273 +++++++++++++++++++++++++----------\n 2 files changed, 263 insertions(+), 78 deletions(-)","diff":"diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h\nindex e35dd7376..5b4ab4aa7 100644\n--- a/include/libcamera/controls.h\n+++ b/include/libcamera/controls.h\n@@ -8,6 +8,7 @@\n #pragma once\n \n #include <assert.h>\n+#include <cstddef>\n #include <map>\n #include <optional>\n #include <stdint.h>\n@@ -25,6 +26,7 @@\n namespace libcamera {\n \n class ControlValidator;\n+class ControlValueView;\n \n enum ControlType {\n \tControlTypeNone,\n@@ -160,6 +162,8 @@ public:\n \t\t    value.data(), value.size(), sizeof(typename T::value_type));\n \t}\n \n+\texplicit ControlValue(const ControlValueView &cvv);\n+\n \t~ControlValue();\n \n \tControlValue(const ControlValue &other);\n@@ -247,6 +251,70 @@ private:\n \t\t std::size_t numElements, std::size_t elementSize);\n };\n \n+class ControlValueView\n+{\n+public:\n+\tconstexpr ControlValueView() noexcept\n+\t\t: type_(ControlTypeNone)\n+\t{\n+\t}\n+\n+\tControlValueView(const ControlValue &cv) noexcept\n+\t\t: ControlValueView(cv.type(), cv.isArray(), cv.numElements(),\n+\t\t\t\t   reinterpret_cast<const std::byte *>(cv.data().data()))\n+\t{\n+\t}\n+\n+#ifndef __DOXYGEN__\n+\t// TODO: should have restricted access?\n+\tControlValueView(ControlType type, bool isArray, std::size_t numElements,\n+\t\t\t const std::byte *data) noexcept\n+\t\t: type_(type), isArray_(isArray), numElements_(numElements),\n+\t\t  data_(data)\n+\t{\n+\t\tassert(isArray || numElements == 1);\n+\t}\n+#endif\n+\n+\t[[nodiscard]] explicit operator bool() const { return type_ != ControlTypeNone; }\n+\t[[nodiscard]] ControlType type() const { return type_; }\n+\t[[nodiscard]] bool isNone() const { return type_ == ControlTypeNone; }\n+\t[[nodiscard]] bool isArray() const { return isArray_; }\n+\t[[nodiscard]] std::size_t numElements() const { return numElements_; }\n+\t[[nodiscard]] Span<const std::byte> data() const;\n+\n+\t[[nodiscard]] bool operator==(const ControlValueView &other) const;\n+\n+\t[[nodiscard]] bool operator!=(const ControlValueView &other) const\n+\t{\n+\t\treturn !(*this == other);\n+\t}\n+\n+\ttemplate<typename T>\n+\t[[nodiscard]] auto get() const\n+\t{\n+\t\tusing TypeInfo = details::control_type<std::remove_cv_t<T>>;\n+\n+\t\tassert(type_ == TypeInfo::value);\n+\t\tassert(isArray_ == (TypeInfo::size > 0));\n+\n+\t\tif constexpr (TypeInfo::size > 0) {\n+\t\t\treturn T(reinterpret_cast<const typename T::value_type *>(data().data()), numElements_);\n+\t\t} else {\n+\t\t\tassert(numElements_ == 1);\n+\t\t\treturn *reinterpret_cast<const T *>(data().data());\n+\t\t}\n+\t}\n+\n+private:\n+\tControlType type_ : 8;\n+\tbool isArray_ = false;\n+\tuint32_t numElements_ = 0;\n+\tconst std::byte *data_ = nullptr;\n+};\n+\n+std::ostream &operator<<(std::ostream &s, const ControlValueView &v);\n+\n class ControlId\n {\n public:\ndiff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp\nindex 1e1b49e6b..15e6b58ef 100644\n--- a/src/libcamera/controls.cpp\n+++ b/src/libcamera/controls.cpp\n@@ -106,6 +106,16 @@ ControlValue::ControlValue()\n {\n }\n \n+/**\n+ * \\brief Construct a ControlValue from a ControlValueView\n+ */\n+ControlValue::ControlValue(const ControlValueView &cvv)\n+\t: ControlValue()\n+{\n+\tset(cvv.type(), cvv.isArray(), cvv.data().data(),\n+\t    cvv.numElements(), ControlValueSize[cvv.type()]);\n+}\n+\n /**\n  * \\fn template<typename T> T ControlValue::ControlValue(const T &value)\n  * \\brief Construct a ControlValue of type T\n@@ -213,84 +223,7 @@ Span<uint8_t> ControlValue::data()\n  */\n std::string ControlValue::toString() const\n {\n-\tif (type_ == ControlTypeNone)\n-\t\treturn \"<ValueType Error>\";\n-\n-\tconst uint8_t *data = ControlValue::data().data();\n-\n-\tif (type_ == ControlTypeString)\n-\t\treturn std::string(reinterpret_cast<const char *>(data),\n-\t\t\t\t   numElements_);\n-\n-\tstd::string str(isArray_ ? \"[ \" : \"\");\n-\n-\tfor (unsigned int i = 0; i < numElements_; ++i) {\n-\t\tswitch (type_) {\n-\t\tcase ControlTypeBool: {\n-\t\t\tconst bool *value = reinterpret_cast<const bool *>(data);\n-\t\t\tstr += *value ? \"true\" : \"false\";\n-\t\t\tbreak;\n-\t\t}\n-\t\tcase ControlTypeByte: {\n-\t\t\tconst uint8_t *value = reinterpret_cast<const uint8_t *>(data);\n-\t\t\tstr += std::to_string(*value);\n-\t\t\tbreak;\n-\t\t}\n-\t\tcase ControlTypeUnsigned16: {\n-\t\t\tconst uint16_t *value = reinterpret_cast<const uint16_t *>(data);\n-\t\t\tstr += std::to_string(*value);\n-\t\t\tbreak;\n-\t\t}\n-\t\tcase ControlTypeUnsigned32: {\n-\t\t\tconst uint32_t *value = reinterpret_cast<const uint32_t *>(data);\n-\t\t\tstr += std::to_string(*value);\n-\t\t\tbreak;\n-\t\t}\n-\t\tcase ControlTypeInteger32: {\n-\t\t\tconst int32_t *value = reinterpret_cast<const int32_t *>(data);\n-\t\t\tstr += std::to_string(*value);\n-\t\t\tbreak;\n-\t\t}\n-\t\tcase ControlTypeInteger64: {\n-\t\t\tconst int64_t *value = reinterpret_cast<const int64_t *>(data);\n-\t\t\tstr += std::to_string(*value);\n-\t\t\tbreak;\n-\t\t}\n-\t\tcase ControlTypeFloat: {\n-\t\t\tconst float *value = reinterpret_cast<const float *>(data);\n-\t\t\tstr += std::to_string(*value);\n-\t\t\tbreak;\n-\t\t}\n-\t\tcase ControlTypeRectangle: {\n-\t\t\tconst Rectangle *value = reinterpret_cast<const Rectangle *>(data);\n-\t\t\tstr += value->toString();\n-\t\t\tbreak;\n-\t\t}\n-\t\tcase ControlTypeSize: {\n-\t\t\tconst Size *value = reinterpret_cast<const Size *>(data);\n-\t\t\tstr += value->toString();\n-\t\t\tbreak;\n-\t\t}\n-\t\tcase ControlTypePoint: {\n-\t\t\tconst Point *value = reinterpret_cast<const Point *>(data);\n-\t\t\tstr += value->toString();\n-\t\t\tbreak;\n-\t\t}\n-\t\tcase ControlTypeNone:\n-\t\tcase ControlTypeString:\n-\t\t\tbreak;\n-\t\t}\n-\n-\t\tif (i + 1 != numElements_)\n-\t\t\tstr += \", \";\n-\n-\t\tdata += ControlValueSize[type_];\n-\t}\n-\n-\tif (isArray_)\n-\t\tstr += \" ]\";\n-\n-\treturn str;\n+\treturn static_cast<std::ostringstream&&>(std::ostringstream{} << ControlValueView(*this)).str();\n }\n \n /**\n@@ -395,6 +328,190 @@ void ControlValue::reserve(ControlType type, bool isArray, std::size_t numElemen\n \t\tstorage_ = reinterpret_cast<void *>(new uint8_t[newSize]);\n }\n \n+/**\n+ * \\class ControlValueView\n+ * \\brief A non-owning view-like type to the value of a control\n+ */\n+\n+/**\n+ * \\fn ControlValueView::ControlValueView()\n+ * \\brief Construct an empty view\n+ * \\sa ControlValue::ControlValue()\n+ */\n+\n+/**\n+ * \\fn ControlValueView::ControlValueView(const ControlValue &v)\n+ * \\brief Construct a view referring to \\a v\n+ *\n+ * The constructed view will refer to the value stored by \\a v, and thus\n+ * \\a v must not be modified or destroyed before the view is destroyed.\n+ *\n+ * \\sa ControlValue::ControlValue()\n+ */\n+\n+/**\n+ * \\fn ControlValueView::operator bool() const\n+ * \\brief Determine if the referenced ControlValue is valid\n+ * \\sa ControlValueView::isNone()\n+ */\n+\n+/**\n+ * \\fn ControlType ControlValueView::type() const\n+ * \\copydoc ControlValue::type()\n+ * \\sa ControlValue::type()\n+ */\n+\n+/**\n+ * \\fn ControlValueView::isNone() const\n+ * \\copydoc ControlValue::isNone()\n+ * \\sa ControlValue::isNone()\n+ */\n+\n+/**\n+ * \\fn ControlValueView::isArray() const\n+ * \\copydoc ControlValue::isArray()\n+ * \\sa ControlValue::isArray()\n+ */\n+\n+/**\n+ * \\fn ControlValueView::numElements() const\n+ * \\copydoc ControlValue::numElements()\n+ * \\sa ControlValue::numElements()\n+ */\n+\n+/**\n+ * \\copydoc ControlValue::data()\n+ * \\sa ControlValue::data()\n+ */\n+Span<const std::byte> ControlValueView::data() const\n+{\n+\treturn { data_, numElements_ * ControlValueSize[type_] };\n+}\n+\n+/**\n+ * \\copydoc ControlValue::operator==()\n+ * \\sa ControlValue::operator==()\n+ * \\sa ControlValueView::operator!=()\n+ */\n+bool ControlValueView::operator==(const ControlValueView &other) const\n+{\n+\tif (type_ != other.type_)\n+\t\treturn false;\n+\n+\tif (numElements_ != other.numElements_)\n+\t\treturn false;\n+\n+\tif (isArray_ != other.isArray_)\n+\t\treturn false;\n+\n+\tconst auto d = data();\n+\n+\treturn memcmp(d.data(), other.data_, d.size_bytes()) == 0;\n+}\n+\n+/**\n+ * \\fn ControlValueView::operator!=() const\n+ * \\copydoc ControlValue::operator!=()\n+ * \\sa ControlValue::operator!=()\n+ * \\sa ControlValueView::operator==()\n+ */\n+\n+/**\n+ * \\fn template<typename T> T ControlValueView::get() const\n+ * \\copydoc ControlValue::get()\n+ * \\sa ControlValue::get()\n+ */\n+\n+/**\n+ * \\brief Insert a text representation of a value into an output stream\n+ * \\sa ControlValue::toString()\n+ */\n+std::ostream &operator<<(std::ostream &s, const ControlValueView &v)\n+{\n+\tconst auto type = v.type();\n+\tif (type == ControlTypeNone)\n+\t\treturn s << \"None\";\n+\n+\tconst auto *data = v.data().data();\n+\tconst auto numElements = v.numElements();\n+\n+\tif (type == ControlTypeString)\n+\t\treturn s << std::string_view(reinterpret_cast<const char *>(data),\n+\t\t\t\t\t     numElements);\n+\n+\tconst bool isArray = v.isArray();\n+\tif (isArray)\n+\t\ts << \"[ \";\n+\n+\tfor (std::size_t i = 0; i < numElements; ++i) {\n+\t\tif (i > 0)\n+\t\t\ts << \", \";\n+\n+\t\tswitch (type) {\n+\t\tcase ControlTypeBool: {\n+\t\t\tconst bool *value = reinterpret_cast<const bool *>(data);\n+\t\t\ts << (*value ? \"true\" : \"false\");\n+\t\t\tbreak;\n+\t\t}\n+\t\tcase ControlTypeByte: {\n+\t\t\tconst auto *value = reinterpret_cast<const uint8_t *>(data);\n+\t\t\ts << static_cast<unsigned int>(*value);\n+\t\t\tbreak;\n+\t\t}\n+\t\tcase ControlTypeUnsigned16: {\n+\t\t\tconst auto *value = reinterpret_cast<const uint16_t *>(data);\n+\t\t\ts << *value;\n+\t\t\tbreak;\n+\t\t}\n+\t\tcase ControlTypeUnsigned32: {\n+\t\t\tconst auto *value = reinterpret_cast<const uint32_t *>(data);\n+\t\t\ts << *value;\n+\t\t\tbreak;\n+\t\t}\n+\t\tcase ControlTypeInteger32: {\n+\t\t\tconst auto *value = reinterpret_cast<const int32_t *>(data);\n+\t\t\ts << *value;\n+\t\t\tbreak;\n+\t\t}\n+\t\tcase ControlTypeInteger64: {\n+\t\t\tconst auto *value = reinterpret_cast<const int64_t *>(data);\n+\t\t\ts << *value;\n+\t\t\tbreak;\n+\t\t}\n+\t\tcase ControlTypeFloat: {\n+\t\t\tconst auto *value = reinterpret_cast<const float *>(data);\n+\t\t\ts << std::fixed << *value;\n+\t\t\tbreak;\n+\t\t}\n+\t\tcase ControlTypeRectangle: {\n+\t\t\tconst auto *value = reinterpret_cast<const Rectangle *>(data);\n+\t\t\ts << *value;\n+\t\t\tbreak;\n+\t\t}\n+\t\tcase ControlTypeSize: {\n+\t\t\tconst auto *value = reinterpret_cast<const Size *>(data);\n+\t\t\ts << *value;\n+\t\t\tbreak;\n+\t\t}\n+\t\tcase ControlTypePoint: {\n+\t\t\tconst auto *value = reinterpret_cast<const Point *>(data);\n+\t\t\ts << *value;\n+\t\t\tbreak;\n+\t\t}\n+\t\tcase ControlTypeNone:\n+\t\tcase ControlTypeString:\n+\t\t\tbreak;\n+\t\t}\n+\n+\t\tdata += ControlValueSize[type];\n+\t}\n+\n+\tif (isArray)\n+\t\ts << \" ]\";\n+\n+\treturn s;\n+}\n+\n /**\n  * \\class ControlId\n  * \\brief Control static metadata\n","prefixes":["v4","02/22"]}