[PATCH] libcamera: controls: Add overwriteExisting parameter to ControlList::merge

Stefan Klug stefan.klug at ideasonboard.com
Thu Feb 29 17:57:53 CET 2024


This is useful in many cases although not included in the stl.

A typical usecase would be:
ContolList controls = getDefaults()
controls.merge(someSpecialValues, true)
...


Signed-off-by: Stefan Klug <stefan.klug at ideasonboard.com>
---

Hey,

this is my first email patch - hurray :-)

Cheers,
Stefan

 include/libcamera/controls.h | 2 +-
 src/libcamera/controls.cpp   | 9 ++++++---
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h
index cf942055..0bf778d8 100644
--- a/include/libcamera/controls.h
+++ b/include/libcamera/controls.h
@@ -368,7 +368,7 @@ public:
 	std::size_t size() const { return controls_.size(); }
 
 	void clear() { controls_.clear(); }
-	void merge(const ControlList &source);
+	void merge(const ControlList &source, bool overwriteExisting = false);
 
 	bool contains(unsigned int id) const;
 
diff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp
index b808116c..8ef29a96 100644
--- a/src/libcamera/controls.cpp
+++ b/src/libcamera/controls.cpp
@@ -910,10 +910,13 @@ ControlList::ControlList(const ControlInfoMap &infoMap,
 /**
  * \brief Merge the \a source into the ControlList
  * \param[in] source The ControlList to merge into this object
+ * \param[in] overwriteExisting Control if existing elements in *this shall be
+ * overwritten
  *
  * Merging two control lists copies elements from the \a source and inserts
  * them in *this. If the \a source contains elements whose key is already
- * present in *this, then those elements are not overwritten.
+ * present in *this, then those elements are only overwritten if
+ * overwriteExisting is true.
  *
  * Only control lists created from the same ControlIdMap or ControlInfoMap may
  * be merged. Attempting to do otherwise results in undefined behaviour.
@@ -921,7 +924,7 @@ ControlList::ControlList(const ControlInfoMap &infoMap,
  * \todo Reimplement or implement an overloaded version which internally uses
  * std::unordered_map::merge() and accepts a non-const argument.
  */
-void ControlList::merge(const ControlList &source)
+void ControlList::merge(const ControlList &source, bool overwriteExisting)
 {
 	/**
 	 * \todo ASSERT that the current and source ControlList are derived
@@ -936,7 +939,7 @@ void ControlList::merge(const ControlList &source)
 	 */
 
 	for (const auto &ctrl : source) {
-		if (contains(ctrl.first)) {
+		if (!overwriteExisting && contains(ctrl.first)) {
 			const ControlId *id = idmap_->at(ctrl.first);
 			LOG(Controls, Warning)
 				<< "Control " << id->name() << " not overwritten";
-- 
2.40.1



More information about the libcamera-devel mailing list