[RFC PATCH v1] libcamera: controls: ControlInfo: Ensure types match

Kieran Bingham kieran.bingham at ideasonboard.com
Thu Feb 13 11:57:15 CET 2025


Quoting Barnabás Pőcze (2025-01-28 12:13:55)
> It is expected that the min/max/default/etc. values in a `ControlInfo`
> have the same type. So add assertions to check that.
> 

This patch looks like a very good addition, which could have helped
catch the issue Naush faced yesterday
(https://patchwork.libcamera.org/patch/22778/)

Is there anyway we can make this a compile time assertion instead of a
runtime check?

--
Kieran


> Signed-off-by: Barnabás Pőcze <pobrn at protonmail.com>
> ---
>  src/libcamera/controls.cpp | 22 +++++++++++++++++++++-
>  1 file changed, 21 insertions(+), 1 deletion(-)
> 
> diff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp
> index 70f6f6092..07f276b35 100644
> --- a/src/libcamera/controls.cpp
> +++ b/src/libcamera/controls.cpp
> @@ -580,6 +580,19 @@ ControlId::ControlId(unsigned int id, const std::string &name,
>   * \brief The Control template type T
>   */
>  
> +namespace {
> +
> +bool sameShape(const ControlValue &a, const ControlValue &b)
> +{
> +       /**
> +        * \todo This is a best effort approach. Without the `ControlId`
> +        * there is no way to check if the sizes of fixed size arrays match.
> +        */
> +       return a.type() == b.type() && a.isArray() == b.isArray();
> +}
> +
> +}
> +
>  /**
>   * \class ControlInfo
>   * \brief Describe the limits of valid values for a Control
> @@ -601,6 +614,7 @@ ControlInfo::ControlInfo(const ControlValue &min,
>                          const ControlValue &def)
>         : min_(min), max_(max), def_(def)
>  {
> +       ASSERT(sameShape(min_, max_) && (def_.isNone() || sameShape(max_, def_)));
>  }
>  
>  /**
> @@ -616,13 +630,19 @@ ControlInfo::ControlInfo(const ControlValue &min,
>  ControlInfo::ControlInfo(Span<const ControlValue> values,
>                          const ControlValue &def)
>  {
> +       ASSERT(!values.empty());
> +
>         min_ = values.front();
>         max_ = values.back();
>         def_ = !def.isNone() ? def : values.front();
>  
> +       ASSERT(sameShape(min_, max_) && sameShape(max_, def_));
> +
>         values_.reserve(values.size());
> -       for (const ControlValue &value : values)
> +       for (const ControlValue &value : values) {
> +               ASSERT(sameShape(def_, value));
>                 values_.push_back(value);
> +       }
>  }
>  
>  /**
> -- 
> 2.48.1
> 
>


More information about the libcamera-devel mailing list