utils: Add unary negation operation to Duration
diff mbox series

Message ID 20250825114642.576534-1-paul.elder@ideasonboard.com
State New
Headers show
Series
  • utils: Add unary negation operation to Duration
Related show

Commit Message

Paul Elder Aug. 25, 2025, 11:46 a.m. UTC
In the near future we will add a SyncAdjustment control for adjusting
the frame duration via the sync algorithm. This control needs to be able
to take on a negative value, since the frame duration can be shortened
in addition to being extended. While the control is an int, it would be
convenient to be able to clamp it to frame duration limits, which are
usually handled as utils::Duration values internally. To allow this
using utils::Duration, add a unary negation operation to
utils::Duration. Also add a test for the operator.

Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>
---
 include/libcamera/base/utils.h | 5 +++++
 src/libcamera/base/utils.cpp   | 6 ++++++
 test/utils.cpp                 | 6 ++++++
 3 files changed, 17 insertions(+)

Comments

Barnabás Pőcze Aug. 25, 2025, 11:54 a.m. UTC | #1
Hi

2025. 08. 25. 13:46 keltezéssel, Paul Elder írta:
> In the near future we will add a SyncAdjustment control for adjusting
> the frame duration via the sync algorithm. This control needs to be able
> to take on a negative value, since the frame duration can be shortened
> in addition to being extended. While the control is an int, it would be
> convenient to be able to clamp it to frame duration limits, which are
> usually handled as utils::Duration values internally. To allow this
> using utils::Duration, add a unary negation operation to
> utils::Duration. Also add a test for the operator.
> 
> Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>

Reviewed-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>


> ---
>   include/libcamera/base/utils.h | 5 +++++
>   src/libcamera/base/utils.cpp   | 6 ++++++
>   test/utils.cpp                 | 6 ++++++
>   3 files changed, 17 insertions(+)
> 
> diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h
> index f21c6dc016ec..cb8caaa9bacc 100644
> --- a/include/libcamera/base/utils.h
> +++ b/include/libcamera/base/utils.h
> @@ -394,6 +394,11 @@ public:
>   		return c.count();
>   	}
>   
> +	constexpr Duration operator-() const
> +	{
> +		return BaseDuration::operator-();
> +	}
> +
>   	explicit constexpr operator bool() const
>   	{
>   		return *this != BaseDuration::zero();
> diff --git a/src/libcamera/base/utils.cpp b/src/libcamera/base/utils.cpp
> index bcfc1941a92a..cb9fe0049c83 100644
> --- a/src/libcamera/base/utils.cpp
> +++ b/src/libcamera/base/utils.cpp
> @@ -425,6 +425,12 @@ std::string toAscii(const std::string &str)
>    * \return The tick count of the Duration expressed in \a Period
>    */
>   
> +/**
> + * \fn Duration::operator-()
> + * \brief Negation operator to negate a \a Duration
> + * \return The duration, with the number of ticks negated
> + */
> +
>   /**
>    * \fn Duration::operator bool()
>    * \brief Boolean operator to test if a \a Duration holds a non-zero time value
> diff --git a/test/utils.cpp b/test/utils.cpp
> index d25475cb93b9..195fddc97d97 100644
> --- a/test/utils.cpp
> +++ b/test/utils.cpp
> @@ -167,6 +167,12 @@ protected:
>   			return TestFail;
>   		}
>   
> +		exposure = 100ms;
> +		if ((-exposure).get<std::milli>() != -100) {
> +			cerr << "utils::Duration failed negation" << endl;
> +			return TestFail;
> +		}
> +
>   		return TestPass;
>   	}
>

Patch
diff mbox series

diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h
index f21c6dc016ec..cb8caaa9bacc 100644
--- a/include/libcamera/base/utils.h
+++ b/include/libcamera/base/utils.h
@@ -394,6 +394,11 @@  public:
 		return c.count();
 	}
 
+	constexpr Duration operator-() const
+	{
+		return BaseDuration::operator-();
+	}
+
 	explicit constexpr operator bool() const
 	{
 		return *this != BaseDuration::zero();
diff --git a/src/libcamera/base/utils.cpp b/src/libcamera/base/utils.cpp
index bcfc1941a92a..cb9fe0049c83 100644
--- a/src/libcamera/base/utils.cpp
+++ b/src/libcamera/base/utils.cpp
@@ -425,6 +425,12 @@  std::string toAscii(const std::string &str)
  * \return The tick count of the Duration expressed in \a Period
  */
 
+/**
+ * \fn Duration::operator-()
+ * \brief Negation operator to negate a \a Duration
+ * \return The duration, with the number of ticks negated
+ */
+
 /**
  * \fn Duration::operator bool()
  * \brief Boolean operator to test if a \a Duration holds a non-zero time value
diff --git a/test/utils.cpp b/test/utils.cpp
index d25475cb93b9..195fddc97d97 100644
--- a/test/utils.cpp
+++ b/test/utils.cpp
@@ -167,6 +167,12 @@  protected:
 			return TestFail;
 		}
 
+		exposure = 100ms;
+		if ((-exposure).get<std::milli>() != -100) {
+			cerr << "utils::Duration failed negation" << endl;
+			return TestFail;
+		}
+
 		return TestPass;
 	}