[libcamera-devel] [PATCH] libcamera: utils: Add reverse adapter for range-based loop
Niklas Söderlund
niklas.soderlund at ragnatech.se
Sun Dec 27 12:22:05 CET 2020
Hi Laurent,
I like template magic :-)
On 2020-12-27 12:03:31 +0200, Laurent Pinchart wrote:
> Add a utils::reverse() function that creates an adapter wrapping in
> iterable, such that range-based iteration over the adapter iterates over
> the iterable in reverse order.
>
> Signed-off-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
> ---
> include/libcamera/internal/utils.h | 27 +++++++++++++++++++++++++++
> src/libcamera/utils.cpp | 8 ++++++++
> 2 files changed, 35 insertions(+)
>
> This will be used in an upcoming patch series, I'm already sending it
> out to get the idea reviewed.
Looking forward do see what you have in store ;-)
Reviewed-by: Niklas Söderlund <niklas.soderlund at ragnatech.se>
>
> diff --git a/include/libcamera/internal/utils.h b/include/libcamera/internal/utils.h
> index f08134afb5ba..d0146b71727d 100644
> --- a/include/libcamera/internal/utils.h
> +++ b/include/libcamera/internal/utils.h
> @@ -203,6 +203,33 @@ constexpr unsigned int alignUp(unsigned int value, unsigned int alignment)
> return (value + alignment - 1) / alignment * alignment;
> }
>
> +namespace details {
> +
> +template<typename T>
> +struct reverse_adapter {
> + T &iterable;
> +};
> +
> +template<typename T>
> +auto begin(reverse_adapter<T> r)
> +{
> + return std::rbegin(r.iterable);
> +}
> +
> +template<typename T>
> +auto end(reverse_adapter<T> r)
> +{
> + return std::rend(r.iterable);
> +}
> +
> +} /* namespace details */
> +
> +template<typename T>
> +details::reverse_adapter<T> reverse(T &&iterable)
> +{
> + return { iterable };
> +}
> +
> } /* namespace utils */
>
> } /* namespace libcamera */
> diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp
> index e90375ae115c..c4098a74e0ab 100644
> --- a/src/libcamera/utils.cpp
> +++ b/src/libcamera/utils.cpp
> @@ -464,6 +464,14 @@ std::string libcameraSourcePath()
> * \return The value rounded up to the nearest multiple of \a alignment
> */
>
> +/**
> + * \fn reverse(T &&iterable)
> + * \brief Wrap an iterable to reverse iteration in a range-based loop
> + * \param[in] iterable The iterable
> + * \return A value of unspecified type that, when used in a range-based for
> + * loop, will cause the loop to iterate over the \a iterable in reverse order
> + */
> +
> } /* namespace utils */
>
> } /* namespace libcamera */
> --
> Regards,
>
> Laurent Pinchart
>
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel at lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel
--
Regards,
Niklas Söderlund
More information about the libcamera-devel
mailing list