{"id":21272,"url":"https://patchwork.libcamera.org/api/1.1/patches/21272/?format=json","web_url":"https://patchwork.libcamera.org/patch/21272/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/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":"<20240915232420.2106705-2-paul.elder@ideasonboard.com>","date":"2024-09-15T23:24:18","name":"[v4,1/3] libcamera: controls: Add enum names and values map to ControlId","commit_ref":"9bbccb97fa0debc86d906fdfc1fbedbd50de5d06","pull_url":null,"state":"accepted","archived":false,"hash":"0f0d57a0fa17f876c050e147f182d537161ad11e","submitter":{"id":17,"url":"https://patchwork.libcamera.org/api/1.1/people/17/?format=json","name":"Paul Elder","email":"paul.elder@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/21272/mbox/","series":[{"id":4598,"url":"https://patchwork.libcamera.org/api/1.1/series/4598/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4598","date":"2024-09-15T23:24:17","name":"libcamera: controls: Add enum information to ControlId","version":4,"mbox":"https://patchwork.libcamera.org/series/4598/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/21272/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/21272/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 1B9AAC324C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun, 15 Sep 2024 23:24:41 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 29B9763503;\n\tMon, 16 Sep 2024 01:24:37 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 31BEB634F5\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 16 Sep 2024 01:24:32 +0200 (CEST)","from pyrite.hamster-moth.ts.net (unknown\n\t[IPv6:2001:4bc9:a45:b0af:31d1:7a87:7f90:977b])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id DAE888BE;\n\tMon, 16 Sep 2024 01:23:10 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"KmR+HLPo\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1726442591;\n\tbh=bskHHRvbOOayvOrH/YHrhp0bUkWWGoRjOdpules77Qs=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=KmR+HLPo4gkXy2NyIltTC9vKquj7Z49590S+nvi+Fr6azM9HlFd1/9UI6HeEP9Pqk\n\t62JpDelEPAvSuDYMk3PfSSzsOTORq1kZPQkAI9hZMWw12J+WwEQJWiNO/iVkxRDqTv\n\t6Bp3fuciga9/uDU9NsTjHvyX4TCQcLHnKMVjuct0=","From":"Paul Elder <paul.elder@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Paul Elder <paul.elder@ideasonboard.com>, stefan.klug@ideasonboard.com, \n\tKieran Bingham <kieran.bingham@ideasonboard.com>,\n\tLaurent Pinchart <laurent.pinchart@ideasonboard.com>,\n\tUmang Jain <umang.jain@ideasonboard.com>","Subject":"[PATCH v4 1/3] libcamera: controls: Add enum names and values map to\n\tControlId","Date":"Mon, 16 Sep 2024 01:24:18 +0200","Message-Id":"<20240915232420.2106705-2-paul.elder@ideasonboard.com>","X-Mailer":"git-send-email 2.39.2","In-Reply-To":"<20240915232420.2106705-1-paul.elder@ideasonboard.com>","References":"<20240915232420.2106705-1-paul.elder@ideasonboard.com>","MIME-Version":"1.0","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 to ControlId information about the names and values of enum, in the\nevent that the ControlId is an enum type. This allows applications to\nquery the ControlId for the names of the enum values, so that they can\nbe displayed on a UI, for example. Without this, it was necessary to use\nmacros of NameOfControlNameValueMap, which is difficult to use and is\nvery inflexible.\n\nThere already exists a map from name -> value in generated code. Reuse\nthis and pass it to the ControlId constructor, which in turn generates\nthe reverse map. The reverse map is then exposed to applications.\n\nSigned-off-by: Paul Elder <paul.elder@ideasonboard.com>\nReviewed-by: Umang Jain <umang.jain@ideasonboard.com>\n\n---\nChanges in v4:\n- improve commit message\n- remove forward map, as it won't be used\n- replace enumToString() with simply exposing the reverse map (as a\n  const map reference)\n  - simplify the code accordingly\n\nChanges in v3:\n- s/nameValueMap/enumStrMap/\n- s/enumName/enumToString/\n\nNo change in v2\n---\n include/libcamera/controls.h     | 14 ++++++++------\n src/libcamera/control_ids.cpp.in |  4 +++-\n src/libcamera/controls.cpp       | 15 +++++++++++++++\n 3 files changed, 26 insertions(+), 7 deletions(-)","diff":"diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h\nindex 7c2bb2872..96a774ccd 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 <map>\n #include <optional>\n #include <set>\n #include <stdint.h>\n@@ -213,14 +214,13 @@ private:\n class ControlId\n {\n public:\n-\tControlId(unsigned int id, const std::string &name, ControlType type)\n-\t\t: id_(id), name_(name), type_(type)\n-\t{\n-\t}\n+\tControlId(unsigned int id, const std::string &name, ControlType type,\n+\t\t  const std::map<std::string, int32_t> &enumStrMap = {});\n \n \tunsigned int id() const { return id_; }\n \tconst std::string &name() const { return name_; }\n \tControlType type() const { return type_; }\n+\tconst std::map<int32_t, std::string> &enumerators() const { return reverseMap_; }\n \n private:\n \tLIBCAMERA_DISABLE_COPY_AND_MOVE(ControlId)\n@@ -228,6 +228,8 @@ private:\n \tunsigned int id_;\n \tstd::string name_;\n \tControlType type_;\n+\tstd::map<std::string, int32_t> enumStrMap_;\n+\tstd::map<int32_t, std::string> reverseMap_;\n };\n \n static inline bool operator==(unsigned int lhs, const ControlId &rhs)\n@@ -256,8 +258,8 @@ class Control : public ControlId\n public:\n \tusing type = T;\n \n-\tControl(unsigned int id, const char *name)\n-\t\t: ControlId(id, name, details::control_type<std::remove_cv_t<T>>::value)\n+\tControl(unsigned int id, const char *name, const std::map<std::string, int32_t> &enumStrMap = {})\n+\t\t: ControlId(id, name, details::control_type<std::remove_cv_t<T>>::value, enumStrMap)\n \t{\n \t}\n \ndiff --git a/src/libcamera/control_ids.cpp.in b/src/libcamera/control_ids.cpp.in\nindex 05c8fb385..3a2049311 100644\n--- a/src/libcamera/control_ids.cpp.in\n+++ b/src/libcamera/control_ids.cpp.in\n@@ -89,8 +89,10 @@ extern const std::map<std::string, {{ctrl.type}}> {{ctrl.name}}NameValueMap = {\n \t{ \"{{enum.name}}\", {{enum.name}} },\n {%- endfor %}\n };\n-{% endif -%}\n+extern const Control<{{ctrl.type}}> {{ctrl.name}}({{ctrl.name|snake_case|upper}}, \"{{ctrl.name}}\", {{ctrl.name}}NameValueMap);\n+{% else -%}\n extern const Control<{{ctrl.type}}> {{ctrl.name}}({{ctrl.name|snake_case|upper}}, \"{{ctrl.name}}\");\n+{% endif -%}\n {%- endfor %}\n \n {% if vendor != 'libcamera' %}\ndiff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp\nindex dba744042..a46c431a1 100644\n--- a/src/libcamera/controls.cpp\n+++ b/src/libcamera/controls.cpp\n@@ -389,7 +389,15 @@ void ControlValue::reserve(ControlType type, bool isArray, std::size_t numElemen\n  * \\param[in] id The control numerical ID\n  * \\param[in] name The control name\n  * \\param[in] type The control data type\n+ * \\param[in] enumStrMap The map from enum names to values (optional)\n  */\n+ControlId::ControlId(unsigned int id, const std::string &name, ControlType type,\n+\t\t     const std::map<std::string, int32_t> &enumStrMap)\n+\t: id_(id), name_(name), type_(type), enumStrMap_(enumStrMap)\n+{\n+\tfor (const auto &pair : enumStrMap_)\n+\t\treverseMap_[pair.second] = pair.first;\n+}\n \n /**\n  * \\fn unsigned int ControlId::id() const\n@@ -409,6 +417,12 @@ void ControlValue::reserve(ControlType type, bool isArray, std::size_t numElemen\n  * \\return The control data type\n  */\n \n+/**\n+ * \\fn const std::map<int32_t, std::string> &ControlId::enumerators() const\n+ * \\brief Retrieve the map of enum values to enum names\n+ * \\return The map of enum values to enum names\n+ */\n+\n /**\n  * \\fn bool operator==(unsigned int lhs, const ControlId &rhs)\n  * \\brief Compare a ControlId with a control numerical ID\n@@ -459,6 +473,7 @@ void ControlValue::reserve(ControlType type, bool isArray, std::size_t numElemen\n  * \\brief Construct a Control instance\n  * \\param[in] id The control numerical ID\n  * \\param[in] name The control name\n+ * \\param[in] enumStrMap The map from enum names to values (optional)\n  *\n  * The control data type is automatically deduced from the template type T.\n  */\n","prefixes":["v4","1/3"]}