Message ID | 20240911093600.671979-2-paul.elder@ideasonboard.com |
---|---|
State | Superseded |
Headers | show |
Series |
|
Related | show |
Hi Paul, Thank you for the patch. On Wed, Sep 11, 2024 at 11:35:58AM +0200, Paul Elder wrote: > Add to ControlId information about the names and values of enum, in the > event that the ControlId is an enum type. This allows applications to > query the ControlId for the names of the enum values, so that they can > be displayed on a UI, for example. Without this, it was necessary to use > macros of NameOfControlNameValueMap, which is difficult to use and is > very inflexible. > > The main map is name -> value, as this was necessary for mapping names > from tuning files to enum values. We reuse this generated code to > reduce complexity, and generate a reverse map from it. This is confusing, I read it as ControlId having a forward map already, but it doesn't. If I understand this correctly, the forward map exists in the generated control_ids.h header, you're now passing it to the ControlId constructor. It would be clearer to explain that the forward map exists in generated code, so you pass it to the ControlId constructor, which then generates the reverse map. > Signed-off-by: Paul Elder <paul.elder@ideasonboard.com> > Reviewed-by: Umang Jain <umang.jain@ideasonboard.com> > > --- > Changes in v3: > - s/nameValueMap/enumStrMap/ > - s/enumName/enumToString/ > > No change in v2 > --- > include/libcamera/controls.h | 15 +++++++++------ > src/libcamera/control_ids.cpp.in | 4 +++- > src/libcamera/controls.cpp | 29 +++++++++++++++++++++++++++++ > 3 files changed, 41 insertions(+), 7 deletions(-) > > diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h > index 7c2bb2872..8959dfcc2 100644 > --- a/include/libcamera/controls.h > +++ b/include/libcamera/controls.h > @@ -8,6 +8,7 @@ > #pragma once > > #include <assert.h> > +#include <map> > #include <optional> > #include <set> > #include <stdint.h> > @@ -213,14 +214,14 @@ private: > class ControlId > { > public: > - ControlId(unsigned int id, const std::string &name, ControlType type) > - : id_(id), name_(name), type_(type) > - { > - } > + ControlId(unsigned int id, const std::string &name, ControlType type, > + const std::map<std::string, int32_t> &enumStrMap = {}); > > unsigned int id() const { return id_; } > const std::string &name() const { return name_; } > ControlType type() const { return type_; } > + const std::map<std::string, int32_t> &enumStrMap() const { return enumStrMap_; } This function is never used. Unless you have a plan to use it, I'd drop it. You can then also drop enumStrMap_. > + const std::string enumToString(int32_t value) const; How about const std::map<int32_t, std::string>& enumerators() const { return reverseMap_; } ? You wouldn't need to return a magic "UNKNOWN" value. > > private: > LIBCAMERA_DISABLE_COPY_AND_MOVE(ControlId) > @@ -228,6 +229,8 @@ private: > unsigned int id_; > std::string name_; > ControlType type_; > + std::map<std::string, int32_t> enumStrMap_; > + std::map<int32_t, std::string> reverseMap_; > }; > > static inline bool operator==(unsigned int lhs, const ControlId &rhs) > @@ -256,8 +259,8 @@ class Control : public ControlId > public: > using type = T; > > - Control(unsigned int id, const char *name) > - : ControlId(id, name, details::control_type<std::remove_cv_t<T>>::value) > + Control(unsigned int id, const char *name, const std::map<std::string, int32_t> &enumStrMap = {}) > + : ControlId(id, name, details::control_type<std::remove_cv_t<T>>::value, enumStrMap) > { > } > > diff --git a/src/libcamera/control_ids.cpp.in b/src/libcamera/control_ids.cpp.in > index 05c8fb385..3a2049311 100644 > --- a/src/libcamera/control_ids.cpp.in > +++ b/src/libcamera/control_ids.cpp.in > @@ -89,8 +89,10 @@ extern const std::map<std::string, {{ctrl.type}}> {{ctrl.name}}NameValueMap = { > { "{{enum.name}}", {{enum.name}} }, > {%- endfor %} > }; > -{% endif -%} > +extern const Control<{{ctrl.type}}> {{ctrl.name}}({{ctrl.name|snake_case|upper}}, "{{ctrl.name}}", {{ctrl.name}}NameValueMap); > +{% else -%} > extern const Control<{{ctrl.type}}> {{ctrl.name}}({{ctrl.name|snake_case|upper}}, "{{ctrl.name}}"); > +{% endif -%} > {%- endfor %} > > {% if vendor != 'libcamera' %} > diff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp > index dba744042..0f83139fb 100644 > --- a/src/libcamera/controls.cpp > +++ b/src/libcamera/controls.cpp > @@ -7,6 +7,8 @@ > > #include <libcamera/controls.h> > > +#include <algorithm> Is this used below or a leftover ? > +#include <map> You can drop map as controls.h is guaranteed to provide it. > #include <sstream> > #include <string.h> > #include <string> > @@ -389,7 +391,15 @@ void ControlValue::reserve(ControlType type, bool isArray, std::size_t numElemen > * \param[in] id The control numerical ID > * \param[in] name The control name > * \param[in] type The control data type > + * \param[in] enumStrMap The map from enum names to values (optional) > */ > +ControlId::ControlId(unsigned int id, const std::string &name, ControlType type, > + const std::map<std::string, int32_t> &enumStrMap) > + : id_(id), name_(name), type_(type), enumStrMap_(enumStrMap) > +{ > + for (const auto &pair : enumStrMap_) > + reverseMap_[pair.second] = pair.first; > +} > > /** > * \fn unsigned int ControlId::id() const > @@ -409,6 +419,24 @@ void ControlValue::reserve(ControlType type, bool isArray, std::size_t numElemen > * \return The control data type > */ > > +/** > + * \brief Retrieve the name of an enum value > + * \return The name of the enum value > + */ > +const std::string ControlId::enumToString(int32_t value) const > +{ > + if (reverseMap_.find(value) != reverseMap_.end()) > + return reverseMap_.at(value); > + > + return "UNKNOWN"; > +} > + > +/** > + * \fn std::map<std::string, int32_t> ControlId::enumStrMap() const > + * \brief Retrieve the map from enum names to enum values (if applicable) > + * \return The map from enum names to enum values > + */ > + > /** > * \fn bool operator==(unsigned int lhs, const ControlId &rhs) > * \brief Compare a ControlId with a control numerical ID > @@ -459,6 +487,7 @@ void ControlValue::reserve(ControlType type, bool isArray, std::size_t numElemen > * \brief Construct a Control instance > * \param[in] id The control numerical ID > * \param[in] name The control name > + * \param[in] enumStrMap The map from enum names to values (optional) > * > * The control data type is automatically deduced from the template type T. > */
diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h index 7c2bb2872..8959dfcc2 100644 --- a/include/libcamera/controls.h +++ b/include/libcamera/controls.h @@ -8,6 +8,7 @@ #pragma once #include <assert.h> +#include <map> #include <optional> #include <set> #include <stdint.h> @@ -213,14 +214,14 @@ private: class ControlId { public: - ControlId(unsigned int id, const std::string &name, ControlType type) - : id_(id), name_(name), type_(type) - { - } + ControlId(unsigned int id, const std::string &name, ControlType type, + const std::map<std::string, int32_t> &enumStrMap = {}); unsigned int id() const { return id_; } const std::string &name() const { return name_; } ControlType type() const { return type_; } + const std::map<std::string, int32_t> &enumStrMap() const { return enumStrMap_; } + const std::string enumToString(int32_t value) const; private: LIBCAMERA_DISABLE_COPY_AND_MOVE(ControlId) @@ -228,6 +229,8 @@ private: unsigned int id_; std::string name_; ControlType type_; + std::map<std::string, int32_t> enumStrMap_; + std::map<int32_t, std::string> reverseMap_; }; static inline bool operator==(unsigned int lhs, const ControlId &rhs) @@ -256,8 +259,8 @@ class Control : public ControlId public: using type = T; - Control(unsigned int id, const char *name) - : ControlId(id, name, details::control_type<std::remove_cv_t<T>>::value) + Control(unsigned int id, const char *name, const std::map<std::string, int32_t> &enumStrMap = {}) + : ControlId(id, name, details::control_type<std::remove_cv_t<T>>::value, enumStrMap) { } diff --git a/src/libcamera/control_ids.cpp.in b/src/libcamera/control_ids.cpp.in index 05c8fb385..3a2049311 100644 --- a/src/libcamera/control_ids.cpp.in +++ b/src/libcamera/control_ids.cpp.in @@ -89,8 +89,10 @@ extern const std::map<std::string, {{ctrl.type}}> {{ctrl.name}}NameValueMap = { { "{{enum.name}}", {{enum.name}} }, {%- endfor %} }; -{% endif -%} +extern const Control<{{ctrl.type}}> {{ctrl.name}}({{ctrl.name|snake_case|upper}}, "{{ctrl.name}}", {{ctrl.name}}NameValueMap); +{% else -%} extern const Control<{{ctrl.type}}> {{ctrl.name}}({{ctrl.name|snake_case|upper}}, "{{ctrl.name}}"); +{% endif -%} {%- endfor %} {% if vendor != 'libcamera' %} diff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp index dba744042..0f83139fb 100644 --- a/src/libcamera/controls.cpp +++ b/src/libcamera/controls.cpp @@ -7,6 +7,8 @@ #include <libcamera/controls.h> +#include <algorithm> +#include <map> #include <sstream> #include <string.h> #include <string> @@ -389,7 +391,15 @@ void ControlValue::reserve(ControlType type, bool isArray, std::size_t numElemen * \param[in] id The control numerical ID * \param[in] name The control name * \param[in] type The control data type + * \param[in] enumStrMap The map from enum names to values (optional) */ +ControlId::ControlId(unsigned int id, const std::string &name, ControlType type, + const std::map<std::string, int32_t> &enumStrMap) + : id_(id), name_(name), type_(type), enumStrMap_(enumStrMap) +{ + for (const auto &pair : enumStrMap_) + reverseMap_[pair.second] = pair.first; +} /** * \fn unsigned int ControlId::id() const @@ -409,6 +419,24 @@ void ControlValue::reserve(ControlType type, bool isArray, std::size_t numElemen * \return The control data type */ +/** + * \brief Retrieve the name of an enum value + * \return The name of the enum value + */ +const std::string ControlId::enumToString(int32_t value) const +{ + if (reverseMap_.find(value) != reverseMap_.end()) + return reverseMap_.at(value); + + return "UNKNOWN"; +} + +/** + * \fn std::map<std::string, int32_t> ControlId::enumStrMap() const + * \brief Retrieve the map from enum names to enum values (if applicable) + * \return The map from enum names to enum values + */ + /** * \fn bool operator==(unsigned int lhs, const ControlId &rhs) * \brief Compare a ControlId with a control numerical ID @@ -459,6 +487,7 @@ void ControlValue::reserve(ControlType type, bool isArray, std::size_t numElemen * \brief Construct a Control instance * \param[in] id The control numerical ID * \param[in] name The control name + * \param[in] enumStrMap The map from enum names to values (optional) * * The control data type is automatically deduced from the template type T. */