[PATCH 7/8] libcamera: vector: Add explicit cast and data accessor

Paul Elder paul.elder at ideasonboard.com
Mon May 12 23:36:38 CEST 2025


Quoting Stefan Klug (2025-04-11 15:04:14)
> When dealing with Vectors it is useful to be able to cast the to a

s/the//

> different underlying type or to access the underlying data.
> 
> Add functions for that.
> 
> Signed-off-by: Stefan Klug <stefan.klug at ideasonboard.com>

Reviewed-by: Paul Elder <paul.elder at ideasonboard.com>

> ---
>  include/libcamera/internal/vector.h | 11 +++++++++++
>  src/libcamera/vector.cpp            | 18 ++++++++++++++++++
>  2 files changed, 29 insertions(+)
> 
> diff --git a/include/libcamera/internal/vector.h b/include/libcamera/internal/vector.h
> index 16b6aef0b38f..63da55019aef 100644
> --- a/include/libcamera/internal/vector.h
> +++ b/include/libcamera/internal/vector.h
> @@ -63,6 +63,17 @@ public:
>                 return data_[i];
>         }
>  
> +       template<typename T2>
> +       explicit operator Vector<T2, Rows>() const
> +       {
> +               Vector<T2, Rows> ret;
> +               for (unsigned int i = 0; i < Rows; i++)
> +                       ret[i] = static_cast<T2>(data_[i]);
> +               return ret;
> +       }
> +
> +       constexpr Span<const T, Rows> data() const { return data_; }
> +
>         constexpr Vector<T, Rows> operator-() const
>         {
>                 Vector<T, Rows> ret;
> diff --git a/src/libcamera/vector.cpp b/src/libcamera/vector.cpp
> index 4dad1b9001c5..bb89f0de7c4f 100644
> --- a/src/libcamera/vector.cpp
> +++ b/src/libcamera/vector.cpp
> @@ -64,6 +64,24 @@ LOG_DEFINE_CATEGORY(Vector)
>   * \copydoc Vector::operator[](size_t i) const
>   */
>  
> +/**
> + * \fn Vector::data()
> + * \brief Access the vector data
> + *
> + * Access the contents of the vector as linear array of values.
> + *
> + * \return A span referencing the vector data as a linear array
> + */
> +
> +/**
> + * \fn Vector::operator Vector<T2, Rows>()
> + * \brief Cast to a different underlying type
> + *
> + * Cast the vector to a different underlying type using static_cast().
> + *
> + * \return A vector of type T2
> + */
> +
>  /**
>   * \fn Vector::operator-() const
>   * \brief Negate a Vector by negating both all of its coordinates
> -- 
> 2.43.0
>


More information about the libcamera-devel mailing list