[libcamera-devel,13/31] libcamera: controls: Move ControlValue constructor to controls.h

Message ID 20200229164254.23604-14-laurent.pinchart@ideasonboard.com
State Superseded
Headers show
Series
  • libcamera: Add support for array controls
Related show

Commit Message

Laurent Pinchart Feb. 29, 2020, 4:42 p.m. UTC
To avoid defining all specializations of the ControlValue constructor
manually, move the definition of those functions to controls.h. The
default constructor is still kept in controls.cpp.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 include/libcamera/controls.h | 10 +++++++---
 src/libcamera/controls.cpp   | 27 +++------------------------
 2 files changed, 10 insertions(+), 27 deletions(-)

Comments

Kieran Bingham March 2, 2020, 11:28 p.m. UTC | #1
Hi Laurent,

On 29/02/2020 16:42, Laurent Pinchart wrote:
> To avoid defining all specializations of the ControlValue constructor
> manually, move the definition of those functions to controls.h. The
> default constructor is still kept in controls.cpp.
> 

Nice :-)

> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

> ---
>  include/libcamera/controls.h | 10 +++++++---
>  src/libcamera/controls.cpp   | 27 +++------------------------
>  2 files changed, 10 insertions(+), 27 deletions(-)
> 
> diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h
> index 39e240438861..dfe69916cd64 100644
> --- a/include/libcamera/controls.h
> +++ b/include/libcamera/controls.h
> @@ -55,9 +55,13 @@ class ControlValue
>  {
>  public:
>  	ControlValue();
> -	ControlValue(bool value);
> -	ControlValue(int32_t value);
> -	ControlValue(int64_t value);
> +
> +	template<typename T>
> +	ControlValue(T value)
> +		: type_(details::control_type<std::remove_cv_t<T>>::value)
> +	{
> +		*reinterpret_cast<T *>(&bool_) = value;

That's horrible to read but it makes sense :-)

(except for that pesky bool_ of course)

> +	}
>  
>  	ControlType type() const { return type_; }
>  	bool isNone() const { return type_ == ControlTypeNone; }
> diff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp
> index f3d79785e6a8..5cc8ce2199d0 100644
> --- a/src/libcamera/controls.cpp
> +++ b/src/libcamera/controls.cpp
> @@ -74,31 +74,10 @@ ControlValue::ControlValue()
>  }
>  
>  /**
> - * \brief Construct a Boolean ControlValue
> - * \param[in] value Boolean value to store
> + * \fn template<typename T> T ControlValue::ControlValue(T value)
> + * \brief Construct a ControlValue of type T
> + * \param[in] value Initial value
>   */
> -ControlValue::ControlValue(bool value)
> -	: type_(ControlTypeBool), bool_(value)
> -{
> -}
> -
> -/**
> - * \brief Construct an integer ControlValue
> - * \param[in] value Integer value to store
> - */
> -ControlValue::ControlValue(int32_t value)
> -	: type_(ControlTypeInteger32), integer32_(value)
> -{
> -}
> -
> -/**
> - * \brief Construct a 64 bit integer ControlValue
> - * \param[in] value Integer value to store
> - */
> -ControlValue::ControlValue(int64_t value)
> -	: type_(ControlTypeInteger64), integer64_(value)
> -{
> -}
>  
>  /**
>   * \fn ControlValue::type()
>

Patch

diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h
index 39e240438861..dfe69916cd64 100644
--- a/include/libcamera/controls.h
+++ b/include/libcamera/controls.h
@@ -55,9 +55,13 @@  class ControlValue
 {
 public:
 	ControlValue();
-	ControlValue(bool value);
-	ControlValue(int32_t value);
-	ControlValue(int64_t value);
+
+	template<typename T>
+	ControlValue(T value)
+		: type_(details::control_type<std::remove_cv_t<T>>::value)
+	{
+		*reinterpret_cast<T *>(&bool_) = value;
+	}
 
 	ControlType type() const { return type_; }
 	bool isNone() const { return type_ == ControlTypeNone; }
diff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp
index f3d79785e6a8..5cc8ce2199d0 100644
--- a/src/libcamera/controls.cpp
+++ b/src/libcamera/controls.cpp
@@ -74,31 +74,10 @@  ControlValue::ControlValue()
 }
 
 /**
- * \brief Construct a Boolean ControlValue
- * \param[in] value Boolean value to store
+ * \fn template<typename T> T ControlValue::ControlValue(T value)
+ * \brief Construct a ControlValue of type T
+ * \param[in] value Initial value
  */
-ControlValue::ControlValue(bool value)
-	: type_(ControlTypeBool), bool_(value)
-{
-}
-
-/**
- * \brief Construct an integer ControlValue
- * \param[in] value Integer value to store
- */
-ControlValue::ControlValue(int32_t value)
-	: type_(ControlTypeInteger32), integer32_(value)
-{
-}
-
-/**
- * \brief Construct a 64 bit integer ControlValue
- * \param[in] value Integer value to store
- */
-ControlValue::ControlValue(int64_t value)
-	: type_(ControlTypeInteger64), integer64_(value)
-{
-}
 
 /**
  * \fn ControlValue::type()