diff options
author | Laurent Pinchart <laurent.pinchart@ideasonboard.com> | 2020-12-27 11:20:59 +0200 |
---|---|---|
committer | Laurent Pinchart <laurent.pinchart@ideasonboard.com> | 2021-02-05 01:24:35 +0200 |
commit | 36c15f32c95552107903604db4b3e63752e49378 (patch) | |
tree | 10fae0408cc6db209c4974146bc3688226473103 | |
parent | 79266225d2af742195f99be7c91c7d5356f7eb78 (diff) |
libcamera: utils: Add reverse adapter for range-based loop
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@ideasonboard.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
-rw-r--r-- | include/libcamera/internal/utils.h | 27 | ||||
-rw-r--r-- | src/libcamera/utils.cpp | 8 |
2 files changed, 35 insertions, 0 deletions
diff --git a/include/libcamera/internal/utils.h b/include/libcamera/internal/utils.h index f08134af..d0146b71 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 e90375ae..c4098a74 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 */ |