[libcamera-devel] libcamera: base: utils: Add missing constructor for Duration
diff mbox series

Message ID 20220406151833.1176-1-laurent.pinchart@ideasonboard.com
State Accepted
Commit e2d00fbcbb1a9b2da50fa2e280817a0de1a7d286
Headers show
Series
  • [libcamera-devel] libcamera: base: utils: Add missing constructor for Duration
Related show

Commit Message

Laurent Pinchart April 6, 2022, 3:18 p.m. UTC
The Duration class is missing the equivalent to the
std::chrono::duration constructor that takes a number of ticks expressed
as a scalar. Fix it, which allows initializing a Duration instance to 0
or 0.0.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 include/libcamera/base/utils.h | 6 ++++++
 src/libcamera/base/utils.cpp   | 9 +++++++++
 2 files changed, 15 insertions(+)

Comments

Umang Jain April 6, 2022, 6:05 p.m. UTC | #1
Hi Laurent,

On 4/6/22 20:48, Laurent Pinchart via libcamera-devel wrote:
> The Duration class is missing the equivalent to the
> std::chrono::duration constructor that takes a number of ticks expressed
> as a scalar. Fix it, which allows initializing a Duration instance to 0
> or 0.0.


yay,

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


Reviewed-by: Umang Jain <umang.jain@ideasonboard.com>

> ---
>   include/libcamera/base/utils.h | 6 ++++++
>   src/libcamera/base/utils.cpp   | 9 +++++++++
>   2 files changed, 15 insertions(+)
>
> diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h
> index 9ab18101cf27..cfff05836de7 100644
> --- a/include/libcamera/base/utils.h
> +++ b/include/libcamera/base/utils.h
> @@ -327,6 +327,12 @@ class Duration : public std::chrono::duration<double, std::nano>
>   public:
>   	Duration() = default;
>   
> +	template<typename Rep>
> +	constexpr explicit Duration(const Rep &r)
> +		: BaseDuration(r)
> +	{
> +	}
> +
>   	template<typename Rep, typename Period>
>   	constexpr Duration(const std::chrono::duration<Rep, Period> &d)
>   		: BaseDuration(d)
> diff --git a/src/libcamera/base/utils.cpp b/src/libcamera/base/utils.cpp
> index 8cf8a5b2c104..6a307940448e 100644
> --- a/src/libcamera/base/utils.cpp
> +++ b/src/libcamera/base/utils.cpp
> @@ -406,6 +406,15 @@ std::string toAscii(const std::string &str)
>    * duration in nanoseconds with double precision
>    */
>   
> +/**
> + * \fn Duration::Duration(const Rep &r)
> + * \brief Construct a Duration with \a r ticks
> + * \param[in] r The number of ticks
> + *
> + * The constructed \a Duration object is internally represented in double
> + * precision with \a r nanoseconds ticks.
> + */
> +
>   /**
>    * \fn Duration::Duration(const std::chrono::duration<Rep, Period> &d)
>    * \brief Construct a Duration by converting an arbitrary std::chrono::duration
Kieran Bingham April 6, 2022, 7 p.m. UTC | #2
Quoting Laurent Pinchart via libcamera-devel (2022-04-06 16:18:33)
> The Duration class is missing the equivalent to the
> std::chrono::duration constructor that takes a number of ticks expressed
> as a scalar. Fix it, which allows initializing a Duration instance to 0
> or 0.0.
> 

Sounds good. 0ms 0s and 0years are all the same when initialising so 0
and 0.0 seems good to me ;-)


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


> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> ---
>  include/libcamera/base/utils.h | 6 ++++++
>  src/libcamera/base/utils.cpp   | 9 +++++++++
>  2 files changed, 15 insertions(+)
> 
> diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h
> index 9ab18101cf27..cfff05836de7 100644
> --- a/include/libcamera/base/utils.h
> +++ b/include/libcamera/base/utils.h
> @@ -327,6 +327,12 @@ class Duration : public std::chrono::duration<double, std::nano>
>  public:
>         Duration() = default;
>  
> +       template<typename Rep>
> +       constexpr explicit Duration(const Rep &r)
> +               : BaseDuration(r)
> +       {
> +       }
> +
>         template<typename Rep, typename Period>
>         constexpr Duration(const std::chrono::duration<Rep, Period> &d)
>                 : BaseDuration(d)
> diff --git a/src/libcamera/base/utils.cpp b/src/libcamera/base/utils.cpp
> index 8cf8a5b2c104..6a307940448e 100644
> --- a/src/libcamera/base/utils.cpp
> +++ b/src/libcamera/base/utils.cpp
> @@ -406,6 +406,15 @@ std::string toAscii(const std::string &str)
>   * duration in nanoseconds with double precision
>   */
>  
> +/**
> + * \fn Duration::Duration(const Rep &r)
> + * \brief Construct a Duration with \a r ticks
> + * \param[in] r The number of ticks
> + *
> + * The constructed \a Duration object is internally represented in double
> + * precision with \a r nanoseconds ticks.
> + */
> +
>  /**
>   * \fn Duration::Duration(const std::chrono::duration<Rep, Period> &d)
>   * \brief Construct a Duration by converting an arbitrary std::chrono::duration
> -- 
> Regards,
> 
> Laurent Pinchart
>

Patch
diff mbox series

diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h
index 9ab18101cf27..cfff05836de7 100644
--- a/include/libcamera/base/utils.h
+++ b/include/libcamera/base/utils.h
@@ -327,6 +327,12 @@  class Duration : public std::chrono::duration<double, std::nano>
 public:
 	Duration() = default;
 
+	template<typename Rep>
+	constexpr explicit Duration(const Rep &r)
+		: BaseDuration(r)
+	{
+	}
+
 	template<typename Rep, typename Period>
 	constexpr Duration(const std::chrono::duration<Rep, Period> &d)
 		: BaseDuration(d)
diff --git a/src/libcamera/base/utils.cpp b/src/libcamera/base/utils.cpp
index 8cf8a5b2c104..6a307940448e 100644
--- a/src/libcamera/base/utils.cpp
+++ b/src/libcamera/base/utils.cpp
@@ -406,6 +406,15 @@  std::string toAscii(const std::string &str)
  * duration in nanoseconds with double precision
  */
 
+/**
+ * \fn Duration::Duration(const Rep &r)
+ * \brief Construct a Duration with \a r ticks
+ * \param[in] r The number of ticks
+ *
+ * The constructed \a Duration object is internally represented in double
+ * precision with \a r nanoseconds ticks.
+ */
+
 /**
  * \fn Duration::Duration(const std::chrono::duration<Rep, Period> &d)
  * \brief Construct a Duration by converting an arbitrary std::chrono::duration