[{"id":31238,"web_url":"https://patchwork.libcamera.org/comment/31238/","msgid":"<172647353802.3421941.2640071858799389525@ping.linuxembedded.co.uk>","date":"2024-09-16T07:58:58","subject":"Re: [PATCH v4 1/3] libcamera: controls: Add enum names and values\n\tmap to ControlId","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Quoting Paul Elder (2024-09-16 00:24:18)\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> There already exists a map from name -> value in generated code. Reuse\n> this and pass it to the ControlId constructor, which in turn generates\n> the reverse map. The reverse map is then exposed to applications.\n> \n> Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>\n> Reviewed-by: Umang Jain <umang.jain@ideasonboard.com>\n> \n\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n> ---\n> Changes 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> \n> Changes in v3:\n> - s/nameValueMap/enumStrMap/\n> - s/enumName/enumToString/\n> \n> No 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(-)\n> \n> diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h\n> index 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> -       ControlId(unsigned int id, const std::string &name, ControlType type)\n> -               : id_(id), name_(name), type_(type)\n> -       {\n> -       }\n> +       ControlId(unsigned int id, const std::string &name, ControlType type,\n> +                 const std::map<std::string, int32_t> &enumStrMap = {});\n>  \n>         unsigned int id() const { return id_; }\n>         const std::string &name() const { return name_; }\n>         ControlType type() const { return type_; }\n> +       const std::map<int32_t, std::string> &enumerators() const { return reverseMap_; }\n>  \n>  private:\n>         LIBCAMERA_DISABLE_COPY_AND_MOVE(ControlId)\n> @@ -228,6 +228,8 @@ private:\n>         unsigned int id_;\n>         std::string name_;\n>         ControlType type_;\n> +       std::map<std::string, int32_t> enumStrMap_;\n> +       std::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>         using type = T;\n>  \n> -       Control(unsigned int id, const char *name)\n> -               : ControlId(id, name, details::control_type<std::remove_cv_t<T>>::value)\n> +       Control(unsigned int id, const char *name, const std::map<std::string, int32_t> &enumStrMap = {})\n> +               : ControlId(id, name, details::control_type<std::remove_cv_t<T>>::value, enumStrMap)\n>         {\n>         }\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>         { \"{{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..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> +                    const std::map<std::string, int32_t> &enumStrMap)\n> +       : id_(id), name_(name), type_(type), enumStrMap_(enumStrMap)\n> +{\n> +       for (const auto &pair : enumStrMap_)\n> +               reverseMap_[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> -- \n> 2.39.2\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 6FD15C3257\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 16 Sep 2024 07:59:04 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 24F16634FD;\n\tMon, 16 Sep 2024 09:59:03 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 1851A618F0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 16 Sep 2024 09:59:01 +0200 (CEST)","from pendragon.ideasonboard.com\n\t(cpc89244-aztw30-2-0-cust6594.18-1.cable.virginm.net [86.31.185.195])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 01FCA497;\n\tMon, 16 Sep 2024 09:57:39 +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=\"P3yVX8/2\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1726473460;\n\tbh=T94v/nwEXm+6W9dBiUOQL6uxdu5RAi1JtFmgq7lTxRk=;\n\th=In-Reply-To:References:Subject:From:Cc:To:Date:From;\n\tb=P3yVX8/2gwonR5ZOrJoHgMj8CvFOvTlwfZF8T5Z543j4Y9iygVoo+FSu9oVD6UYEl\n\tlRN7+9JFSCwyiIwL9rv4O2NUSqqStD5YiqU1c4ZX407FqJ5MFRe2CuMwZgELZ6ZBX7\n\tuV9CH2ZFJ6X6CNNchYEfCoYPXRrNvDzaCsq3t9j0=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20240915232420.2106705-2-paul.elder@ideasonboard.com>","References":"<20240915232420.2106705-1-paul.elder@ideasonboard.com>\n\t<20240915232420.2106705-2-paul.elder@ideasonboard.com>","Subject":"Re: [PATCH v4 1/3] libcamera: controls: Add enum names and values\n\tmap to ControlId","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Cc":"Paul Elder <paul.elder@ideasonboard.com>, stefan.klug@ideasonboard.com, \n\tLaurent Pinchart <laurent.pinchart@ideasonboard.com>,\n\tUmang Jain <umang.jain@ideasonboard.com>","To":"Paul Elder <paul.elder@ideasonboard.com>,\n\tlibcamera-devel@lists.libcamera.org","Date":"Mon, 16 Sep 2024 08:58:58 +0100","Message-ID":"<172647353802.3421941.2640071858799389525@ping.linuxembedded.co.uk>","User-Agent":"alot/0.10","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>"}},{"id":31362,"web_url":"https://patchwork.libcamera.org/comment/31362/","msgid":"<20240925205135.GA5373@pendragon.ideasonboard.com>","date":"2024-09-25T20:51:35","subject":"Re: [PATCH v4 1/3] libcamera: controls: Add enum names and values\n\tmap to ControlId","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Paul,\n\nThank you for the patch.\n\nOn Mon, Sep 16, 2024 at 01:24:18AM +0200, 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> There already exists a map from name -> value in generated code. Reuse\n> this and pass it to the ControlId constructor, which in turn generates\n> the reverse map. The reverse map is then exposed to applications.\n> \n> Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>\n> Reviewed-by: Umang Jain <umang.jain@ideasonboard.com>\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> \n> ---\n> Changes 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> \n> Changes in v3:\n> - s/nameValueMap/enumStrMap/\n> - s/enumName/enumToString/\n> \n> No 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(-)\n> \n> diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h\n> index 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>  \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..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>   */","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 48705C0F1B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 25 Sep 2024 20:51:41 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 0D2716350E;\n\tWed, 25 Sep 2024 22:51:40 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 71180634F4\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 25 Sep 2024 22:51:38 +0200 (CEST)","from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi\n\t[81.175.209.231])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 5CCF7842;\n\tWed, 25 Sep 2024 22:50: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=\"OE+9TF9p\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1727297410;\n\tbh=M9OfI3R1GrnOLdHAQUvILHf470/7yWBydTVp0TWhEww=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=OE+9TF9phxFQHKCu5tON0+vUzKrXgCwmyIf7rVNXm7/EwjHU8VCPSOeGG2EEKqw9/\n\t/qqYXgDUNqoCo4ktr835RTz+DmZWe+4DmluDGcUZJHUO3lotf/LBwVGFQqJSdNZqTl\n\tV8cXfJoHuQS6K7X8USUxFajtynMg0ThxDttjeu1k=","Date":"Wed, 25 Sep 2024 23:51:35 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Paul Elder <paul.elder@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org, stefan.klug@ideasonboard.com,\n\tKieran Bingham <kieran.bingham@ideasonboard.com>,\n\tUmang Jain <umang.jain@ideasonboard.com>","Subject":"Re: [PATCH v4 1/3] libcamera: controls: Add enum names and values\n\tmap to ControlId","Message-ID":"<20240925205135.GA5373@pendragon.ideasonboard.com>","References":"<20240915232420.2106705-1-paul.elder@ideasonboard.com>\n\t<20240915232420.2106705-2-paul.elder@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20240915232420.2106705-2-paul.elder@ideasonboard.com>","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>"}}]