[{"id":31160,"web_url":"https://patchwork.libcamera.org/comment/31160/","msgid":"<b4a2001b-07c6-4313-8a92-8c49b9314b14@ideasonboard.com>","date":"2024-09-11T06:25:03","subject":"Re: [PATCH 1/3] libcamera: controls: Add enum names and values map\n\tto ControlId","submitter":{"id":86,"url":"https://patchwork.libcamera.org/api/people/86/","name":"Umang Jain","email":"umang.jain@ideasonboard.com"},"content":"Hi Paul,\n\nOn 11/09/24 2:18 am, Paul Elder wrote:\n> Add to ControlId information about the names and values of enum, in the\n> event that the ControlId is an enum type. This allows applications to\n> query the ControlId for the names of the enum values, so that they can\n> be displayed on a UI, for example. Without this, it was necessary to use\n> macros of NameOfControlNameValueMap, which is difficult to use and is\n> very inflexible.\n>\n> The main map is name -> value, as this was necessary for mapping names\n> from tuning files to enum values. We reuse this generated code to\n> reduce complexity, and generate a reverse map from it.\n>\n> Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>\n>\n> ---\n> No change in v2\n> ---\n>   include/libcamera/controls.h     | 15 +++++++++------\n>   src/libcamera/control_ids.cpp.in |  4 +++-\n>   src/libcamera/controls.cpp       | 29 +++++++++++++++++++++++++++++\n>   3 files changed, 41 insertions(+), 7 deletions(-)\n>\n> diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h\n> index 7c2bb2872..407a12256 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,14 @@ 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> &nameValueMap = {});\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<std::string, int32_t> &nameValueMap() const { return nameValueMap_; }\n\nmaybe enumStrMap_  and enumStrMap() ?\n\nThis is a suggestion throughout the patch, as 'name' sounds very generic \nand can be confused/associated with name_ in my opinion\n> +\tconst std::string enumName(int32_t value) const;\n\nmaybe 'enumToString()'  ?\n\nOther than this,\n\nReviewed-by: Umang Jain <umang.jain@ideasonboard.com>\n>   \n>   private:\n>   \tLIBCAMERA_DISABLE_COPY_AND_MOVE(ControlId)\n> @@ -228,6 +229,8 @@ private:\n>   \tunsigned int id_;\n>   \tstd::string name_;\n>   \tControlType type_;\n> +\tstd::map<std::string, int32_t> nameValueMap_;\n> +\tstd::map<int32_t, std::string> reverseMap_;\n>   };\n>   \n>   static inline bool operator==(unsigned int lhs, const ControlId &rhs)\n> @@ -256,8 +259,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> &nameValueMap = {})\n> +\t\t: ControlId(id, name, details::control_type<std::remove_cv_t<T>>::value, nameValueMap)\n>   \t{\n>   \t}\n>   \n> diff --git a/src/libcamera/control_ids.cpp.in b/src/libcamera/control_ids.cpp.in\n> index 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' %}\n> diff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp\n> index dba744042..1c7e7fde1 100644\n> --- a/src/libcamera/controls.cpp\n> +++ b/src/libcamera/controls.cpp\n> @@ -7,6 +7,8 @@\n>   \n>   #include <libcamera/controls.h>\n>   \n> +#include <algorithm>\n> +#include <map>\n>   #include <sstream>\n>   #include <string.h>\n>   #include <string>\n> @@ -389,7 +391,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] nameValueMap 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> &nameValueMap)\n> +\t: id_(id), name_(name), type_(type), nameValueMap_(nameValueMap)\n> +{\n> +\tfor (const auto &pair : nameValueMap_)\n> +\t\treverseMap_[pair.second] = pair.first;\n> +}\n>   \n>   /**\n>    * \\fn unsigned int ControlId::id() const\n> @@ -409,6 +419,24 @@ void ControlValue::reserve(ControlType type, bool isArray, std::size_t numElemen\n>    * \\return The control data type\n>    */\n>   \n> +/**\n> + * \\brief Retrieve the name of an enum value\n> + * \\return The name of the enum value\n> + */\n> +const std::string ControlId::enumName(int32_t value) const\n> +{\n> +\tif (reverseMap_.find(value) != reverseMap_.end())\n> +\t\treturn reverseMap_.at(value);\n> +\n> +\treturn \"UNKNOWN\";\n> +}\n> +\n> +/**\n> + * \\fn std::map<std::string, int32_t> ControlId::nameValueMap() const\n> + * \\brief Retrieve the map from enum names to enum values (if applicable)\n> + * \\return The map from enum names to enum values\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 +487,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] nameValueMap The map from enum names to values (optional)\n>    *\n>    * The control data type is automatically deduced from the template type T.\n>    */","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 79E44BF415\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 11 Sep 2024 06:25:11 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 00AC5634FC;\n\tWed, 11 Sep 2024 08:25:09 +0200 (CEST)","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 5D5E1618F1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 11 Sep 2024 08:25:08 +0200 (CEST)","from [IPV6:2405:201:2015:f873:c173:4b:4a04:3a21] (unknown\n\t[IPv6:2405:201:2015:f873:c173:4b:4a04:3a21])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 419725B3;\n\tWed, 11 Sep 2024 08:23:50 +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=\"pfHUx0aD\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1726035830;\n\tbh=JqpIyPutJ7wgTWXnK32fdUoZA3xkFCEcBhL5MeZOIo8=;\n\th=Date:Subject:To:Cc:References:From:In-Reply-To:From;\n\tb=pfHUx0aDqGK8dpYFCbVhuwC3thmyZn3NOmORS5P+TsjqCriAT7vQQJrFdNAqv7mbg\n\tMSAm++rrr4DkLdZFzRUBngjTINE5+W3qe5F24WOdogl/qlqmOe8cS77ZjTHU7MlVjz\n\tC43PVVzFZDEeI1VZhArxsB5uN49FX8/eGfTNcKag=","Message-ID":"<b4a2001b-07c6-4313-8a92-8c49b9314b14@ideasonboard.com>","Date":"Wed, 11 Sep 2024 11:55:03 +0530","MIME-Version":"1.0","User-Agent":"Mozilla Thunderbird","Subject":"Re: [PATCH 1/3] libcamera: controls: Add enum names and values map\n\tto ControlId","To":"Paul Elder <paul.elder@ideasonboard.com>,\n\tlibcamera-devel@lists.libcamera.org","Cc":"stefan.klug@ideasonboard.com","References":"<20240910204812.2581246-1-paul.elder@ideasonboard.com>\n\t<20240910204812.2581246-2-paul.elder@ideasonboard.com>","Content-Language":"en-US","From":"Umang Jain <umang.jain@ideasonboard.com>","In-Reply-To":"<20240910204812.2581246-2-paul.elder@ideasonboard.com>","Content-Type":"text/plain; charset=UTF-8; format=flowed","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>"}}]