[libcamera-devel,1/3] libcamera: utils: Add exchange()

Message ID 20191216121029.1048242-2-niklas.soderlund@ragnatech.se
State Superseded
Headers show
Series
  • libcamera: Add FileDescriptor to help pass numerical fds around
Related show

Commit Message

Niklas Söderlund Dec. 16, 2019, 12:10 p.m. UTC
C++11 does not support std::exchange(), add a custom implementation in
utils.

Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
---
 src/libcamera/include/utils.h | 9 +++++++++
 src/libcamera/utils.cpp       | 7 +++++++
 2 files changed, 16 insertions(+)

Comments

Laurent Pinchart Dec. 16, 2019, 12:18 p.m. UTC | #1
Hi Niklas,

Thank you for the patch.

On Mon, Dec 16, 2019 at 01:10:27PM +0100, Niklas Söderlund wrote:
> C++11 does not support std::exchange(), add a custom implementation in
> utils.
> 
> Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
> ---
>  src/libcamera/include/utils.h | 9 +++++++++
>  src/libcamera/utils.cpp       | 7 +++++++
>  2 files changed, 16 insertions(+)
> 
> diff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h
> index a80f7d096bf725a1..32e3b009040d9efc 100644
> --- a/src/libcamera/include/utils.h
> +++ b/src/libcamera/include/utils.h
> @@ -66,6 +66,15 @@ const T& clamp(const T& v, const T& lo, const T& hi)
>  	return std::max(lo, std::min(v, hi));
>  }
>  
> +/* C++11 doesn't provide std::exchange */
> +template<class T, class U = T>
> +T exchange(T &obj, U &&new_value)
> +{
> +	T old_value = std::move(obj);
> +	obj = std::forward<U>(new_value);
> +	return old_value;
> +}
> +
>  using clock = std::chrono::steady_clock;
>  using duration = std::chrono::steady_clock::duration;
>  using time_point = std::chrono::steady_clock::time_point;
> diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp
> index d632f6e66638038d..0e2decd966dd8d0c 100644
> --- a/src/libcamera/utils.cpp
> +++ b/src/libcamera/utils.cpp
> @@ -95,6 +95,13 @@ char *secure_getenv(const char *name)
>   * \return lo if v is less than lo, hi if v is greater than hi, otherwise v
>   */
>  
> +/**
> + * \fn libcamera::utils::exchange(T &obj, U &&new_value)

It would b e nice to also explain what the function does ;-)

 * \brief Replace the value of \a obj with \a new_value and return the old value

> + * \param[in] obj object whose value to replace

Isn't this a param[inout] ?

s/object/Object/

> + * \param[in] new_value the value to assign to obj

s/the/The/

> + * \return The old value of obj

s/obj/\\a obj/

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

> + */
> +
>  /**
>   * \typedef clock
>   * \brief The libcamera clock (monotonic)

Patch

diff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h
index a80f7d096bf725a1..32e3b009040d9efc 100644
--- a/src/libcamera/include/utils.h
+++ b/src/libcamera/include/utils.h
@@ -66,6 +66,15 @@  const T& clamp(const T& v, const T& lo, const T& hi)
 	return std::max(lo, std::min(v, hi));
 }
 
+/* C++11 doesn't provide std::exchange */
+template<class T, class U = T>
+T exchange(T &obj, U &&new_value)
+{
+	T old_value = std::move(obj);
+	obj = std::forward<U>(new_value);
+	return old_value;
+}
+
 using clock = std::chrono::steady_clock;
 using duration = std::chrono::steady_clock::duration;
 using time_point = std::chrono::steady_clock::time_point;
diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp
index d632f6e66638038d..0e2decd966dd8d0c 100644
--- a/src/libcamera/utils.cpp
+++ b/src/libcamera/utils.cpp
@@ -95,6 +95,13 @@  char *secure_getenv(const char *name)
  * \return lo if v is less than lo, hi if v is greater than hi, otherwise v
  */
 
+/**
+ * \fn libcamera::utils::exchange(T &obj, U &&new_value)
+ * \param[in] obj object whose value to replace
+ * \param[in] new_value the value to assign to obj
+ * \return The old value of obj
+ */
+
 /**
  * \typedef clock
  * \brief The libcamera clock (monotonic)