[libcamera-devel,v6,4/5] ipa: raspberrypi: Add Overwrite method to RPiController::Metadata
diff mbox series

Message ID 20210507084042.31879-5-naush@raspberrypi.com
State Superseded
Headers show
Series
  • ipa: raspberrypi: Rate-limit the controller algorithms
Related show

Commit Message

Naushir Patuck May 7, 2021, 8:40 a.m. UTC
Add a new Overwrite method to the Metadata class. This will overwrite
all key/value pairs in a destination object that have an identical key
in the source metadata object. Once complete, the source Metadata
object will be empty.

Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
---
 src/ipa/raspberrypi/controller/metadata.hpp | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

Comments

Laurent Pinchart May 8, 2021, 12:32 a.m. UTC | #1
Hi Naush,

Thank you for the patch.

On Fri, May 07, 2021 at 09:40:41AM +0100, Naushir Patuck wrote:
> Add a new Overwrite method to the Metadata class. This will overwrite
> all key/value pairs in a destination object that have an identical key
> in the source metadata object. Once complete, the source Metadata
> object will be empty.
> 
> Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
> ---
>  src/ipa/raspberrypi/controller/metadata.hpp | 20 ++++++++++++++++++++
>  1 file changed, 20 insertions(+)
> 
> diff --git a/src/ipa/raspberrypi/controller/metadata.hpp b/src/ipa/raspberrypi/controller/metadata.hpp
> index 319f2320fc70..ac64f17f617c 100644
> --- a/src/ipa/raspberrypi/controller/metadata.hpp
> +++ b/src/ipa/raspberrypi/controller/metadata.hpp
> @@ -75,6 +75,26 @@ public:
>  		return *this;
>  	}
>  
> +	void Overwrite(Metadata &other)

I may have used an rvalue reference to force the caller to acknowledge
that other will be cleared, but that doesn't matter much.

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

> +	{
> +		std::lock_guard<std::mutex> lock(mutex_);
> +		std::lock_guard<std::mutex> other_lock(other.mutex_);
> +
> +		/*
> +		 * Overwrite any contents of data_ with identical keys from
> +		 * other.data_. Any keys from other.data_ that are not preset in
> +		 * data_ will simply be copied across.
> +		 *
> +		 * The reason for the seemingly illogical steps below is because
> +		 * std::map::merge() does not overwrite existing keys.
> +		 */
> +		other.data_.merge(data_);
> +		std::swap(data_, other.data_);
> +
> +		/* Render the other object as empty now! */
> +		other.data_.clear();
> +	}
> +
>  	template<typename T>
>  	T *GetLocked(std::string const &tag)
>  	{

Patch
diff mbox series

diff --git a/src/ipa/raspberrypi/controller/metadata.hpp b/src/ipa/raspberrypi/controller/metadata.hpp
index 319f2320fc70..ac64f17f617c 100644
--- a/src/ipa/raspberrypi/controller/metadata.hpp
+++ b/src/ipa/raspberrypi/controller/metadata.hpp
@@ -75,6 +75,26 @@  public:
 		return *this;
 	}
 
+	void Overwrite(Metadata &other)
+	{
+		std::lock_guard<std::mutex> lock(mutex_);
+		std::lock_guard<std::mutex> other_lock(other.mutex_);
+
+		/*
+		 * Overwrite any contents of data_ with identical keys from
+		 * other.data_. Any keys from other.data_ that are not preset in
+		 * data_ will simply be copied across.
+		 *
+		 * The reason for the seemingly illogical steps below is because
+		 * std::map::merge() does not overwrite existing keys.
+		 */
+		other.data_.merge(data_);
+		std::swap(data_, other.data_);
+
+		/* Render the other object as empty now! */
+		other.data_.clear();
+	}
+
 	template<typename T>
 	T *GetLocked(std::string const &tag)
 	{