From 1fa4b43402a0af7cc41bb22b58cede687663cc7b Mon Sep 17 00:00:00 2001 From: Jacopo Mondi Date: Thu, 2 Jan 2020 12:01:32 +0100 Subject: libcamera: controls: Support array controls in ControlValue Add array controls support to the ControlValue class. The polymorphic class can now store more than a single element and supports access and creation through the use of Span<>. Signed-off-by: Jacopo Mondi Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- include/libcamera/controls.h | 81 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 68 insertions(+), 13 deletions(-) (limited to 'include') diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h index 4538be06..1e24ae30 100644 --- a/include/libcamera/controls.h +++ b/include/libcamera/controls.h @@ -9,6 +9,7 @@ #define __LIBCAMERA_CONTROLS_H__ #include +#include #include #include @@ -51,6 +52,10 @@ struct control_type { static constexpr ControlType value = ControlTypeInteger64; }; +template +struct control_type> : public control_type> { +}; + } /* namespace details */ class ControlValue @@ -58,15 +63,35 @@ class ControlValue public: ControlValue(); +#ifndef __DOXYGEN__ + template::value, std::nullptr_t> = nullptr> + ControlValue(const T &value) + : type_(ControlTypeNone), numElements_(0) + { + set(details::control_type>::value, false, + &value, 1, sizeof(T)); + } + + template::value, std::nullptr_t> = nullptr> +#else template - ControlValue(T value) - : type_(details::control_type>::value) +#endif + ControlValue(const T &value) + : type_(ControlTypeNone), numElements_(0) { - *reinterpret_cast(&bool_) = value; + set(details::control_type>::value, true, + value.data(), value.size(), sizeof(typename T::value_type)); } + ~ControlValue(); + + ControlValue(const ControlValue &other); + ControlValue &operator=(const ControlValue &other); + ControlType type() const { return type_; } bool isNone() const { return type_ == ControlTypeNone; } + bool isArray() const { return isArray_; } + std::size_t numElements() const { return numElements_; } Span data() const; std::string toString() const; @@ -77,31 +102,61 @@ public: return !(*this == other); } +#ifndef __DOXYGEN__ + template::value, std::nullptr_t> = nullptr> + T get() const + { + assert(type_ == details::control_type>::value); + assert(!isArray_); + + return *reinterpret_cast(data().data()); + } + + template::value, std::nullptr_t> = nullptr> +#else template +#endif T get() const { assert(type_ == details::control_type>::value); + assert(isArray_); + + using V = typename T::value_type; + const V *value = reinterpret_cast(data().data()); + return { value, numElements_ }; + } - return *reinterpret_cast(&bool_); +#ifndef __DOXYGEN__ + template::value, std::nullptr_t> = nullptr> + void set(const T &value) + { + set(details::control_type>::value, false, + reinterpret_cast(&value), 1, sizeof(T)); } + template::value, std::nullptr_t> = nullptr> +#else template +#endif void set(const T &value) { - type_ = details::control_type>::value; - *reinterpret_cast(&bool_) = value; + set(details::control_type>::value, true, + value.data(), value.size(), sizeof(typename T::value_type)); } private: - ControlType type_; - - union { - bool bool_; - int32_t integer32_; - int64_t integer64_; - }; + ControlType type_ : 8; + bool isArray_ : 1; + std::size_t numElements_ : 16; + uint64_t storage_; + + void release(); + void set(ControlType type, bool isArray, const void *data, + std::size_t numElements, std::size_t elementSize); }; +static_assert(sizeof(ControlValue) == 16, "Invalid size of ControlValue class"); + class ControlId { public: -- cgit v1.2.1