[libcamera-devel] [PATCH 2/2] libcamera: span: Provide and use helper variable templates for type traits
Niklas Söderlund
niklas.soderlund at ragnatech.se
Tue Oct 27 22:53:54 CET 2020
Hi Laurent,
Thanks for your work.
On 2020-10-26 21:52:09 +0200, Laurent Pinchart wrote:
> Following the C++17 practice, provide is_array_v<T> and is_span_v<T>
> helper variable templates as shorter versions of is_array<T>::value and
> is_span<T>::value, and use them through the code.
>
> Signed-off-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund at ragnatech.se>
> ---
> include/libcamera/controls.h | 12 ++++++------
> include/libcamera/span.h | 22 ++++++++++++++--------
> 2 files changed, 20 insertions(+), 14 deletions(-)
>
> diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h
> index dc549a92f975..3634dc431618 100644
> --- a/include/libcamera/controls.h
> +++ b/include/libcamera/controls.h
> @@ -96,7 +96,7 @@ public:
> ControlValue();
>
> #ifndef __DOXYGEN__
> - template<typename T, typename std::enable_if_t<!details::is_span<T>::value &&
> + template<typename T, typename std::enable_if_t<!details::is_span_v<T> &&
> details::control_type<T>::value &&
> !std::is_same_v<std::string, std::remove_cv_t<T>>,
> std::nullptr_t> = nullptr>
> @@ -107,7 +107,7 @@ public:
> &value, 1, sizeof(T));
> }
>
> - template<typename T, typename std::enable_if_t<details::is_span<T>::value ||
> + template<typename T, typename std::enable_if_t<details::is_span_v<T> ||
> std::is_same_v<std::string, std::remove_cv_t<T>>,
> std::nullptr_t> = nullptr>
> #else
> @@ -141,7 +141,7 @@ public:
> }
>
> #ifndef __DOXYGEN__
> - template<typename T, typename std::enable_if_t<!details::is_span<T>::value &&
> + template<typename T, typename std::enable_if_t<!details::is_span_v<T> &&
> !std::is_same_v<std::string, std::remove_cv_t<T>>,
> std::nullptr_t> = nullptr>
> T get() const
> @@ -152,7 +152,7 @@ public:
> return *reinterpret_cast<const T *>(data().data());
> }
>
> - template<typename T, typename std::enable_if_t<details::is_span<T>::value ||
> + template<typename T, typename std::enable_if_t<details::is_span_v<T> ||
> std::is_same_v<std::string, std::remove_cv_t<T>>,
> std::nullptr_t> = nullptr>
> #else
> @@ -169,7 +169,7 @@ public:
> }
>
> #ifndef __DOXYGEN__
> - template<typename T, typename std::enable_if_t<!details::is_span<T>::value &&
> + template<typename T, typename std::enable_if_t<!details::is_span_v<T> &&
> !std::is_same_v<std::string, std::remove_cv_t<T>>,
> std::nullptr_t> = nullptr>
> void set(const T &value)
> @@ -178,7 +178,7 @@ public:
> reinterpret_cast<const void *>(&value), 1, sizeof(T));
> }
>
> - template<typename T, typename std::enable_if_t<details::is_span<T>::value ||
> + template<typename T, typename std::enable_if_t<details::is_span_v<T> ||
> std::is_same_v<std::string, std::remove_cv_t<T>>,
> std::nullptr_t> = nullptr>
> #else
> diff --git a/include/libcamera/span.h b/include/libcamera/span.h
> index d720adb81f89..e7ffef1255d4 100644
> --- a/include/libcamera/span.h
> +++ b/include/libcamera/span.h
> @@ -31,6 +31,9 @@ template<typename U, std::size_t N>
> struct is_array<std::array<U, N>> : public std::true_type {
> };
>
> +template<typename T>
> +inline constexpr bool is_array_v = is_array<T>::value;
> +
> template<typename U>
> struct is_span : public std::false_type {
> };
> @@ -39,6 +42,9 @@ template<typename U, std::size_t Extent>
> struct is_span<Span<U, Extent>> : public std::true_type {
> };
>
> +template<typename T>
> +inline constexpr bool is_span_v = is_span<T>::value;
> +
> } /* namespace details */
>
> namespace utils {
> @@ -155,8 +161,8 @@ public:
>
> template<class Container>
> explicit constexpr Span(Container &cont,
> - std::enable_if_t<!details::is_span<Container>::value &&
> - !details::is_array<Container>::value &&
> + std::enable_if_t<!details::is_span_v<Container> &&
> + !details::is_array_v<Container> &&
> !std::is_array_v<Container> &&
> std::is_convertible_v<std::remove_pointer_t<decltype(utils::data(cont))> (*)[],
> element_type (*)[]>,
> @@ -167,8 +173,8 @@ public:
>
> template<class Container>
> explicit constexpr Span(const Container &cont,
> - std::enable_if_t<!details::is_span<Container>::value &&
> - !details::is_array<Container>::value &&
> + std::enable_if_t<!details::is_span_v<Container> &&
> + !details::is_array_v<Container> &&
> !std::is_array_v<Container> &&
> std::is_convertible_v<std::remove_pointer_t<decltype(utils::data(cont))> (*)[],
> element_type (*)[]>,
> @@ -317,8 +323,8 @@ public:
>
> template<class Container>
> constexpr Span(Container &cont,
> - std::enable_if_t<!details::is_span<Container>::value &&
> - !details::is_array<Container>::value &&
> + std::enable_if_t<!details::is_span_v<Container> &&
> + !details::is_array_v<Container> &&
> !std::is_array_v<Container> &&
> std::is_convertible_v<std::remove_pointer_t<decltype(utils::data(cont))> (*)[],
> element_type (*)[]>,
> @@ -329,8 +335,8 @@ public:
>
> template<class Container>
> constexpr Span(const Container &cont,
> - std::enable_if_t<!details::is_span<Container>::value &&
> - !details::is_array<Container>::value &&
> + std::enable_if_t<!details::is_span_v<Container> &&
> + !details::is_array_v<Container> &&
> !std::is_array_v<Container> &&
> std::is_convertible_v<std::remove_pointer_t<decltype(utils::data(cont))> (*)[],
> element_type (*)[]>,
> --
> 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