From patchwork Mon Jan 13 16:42:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 2632 X-Patchwork-Delegate: jacopo@jmondi.org Return-Path: Received: from relay11.mail.gandi.net (relay11.mail.gandi.net [217.70.178.231]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 66A9F60703 for ; Mon, 13 Jan 2020 17:40:37 +0100 (CET) Received: from uno.lan (2-224-242-101.ip172.fastwebnet.it [2.224.242.101]) (Authenticated sender: jacopo@jmondi.org) by relay11.mail.gandi.net (Postfix) with ESMTPSA id 0428710000E; Mon, 13 Jan 2020 16:40:36 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Mon, 13 Jan 2020 17:42:39 +0100 Message-Id: <20200113164245.52535-18-jacopo@jmondi.org> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20200113164245.52535-1-jacopo@jmondi.org> References: <20200113164245.52535-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 17/23] libcamera: control: Deep-copy control values X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 13 Jan 2020 16:40:37 -0000 Implement operator=() and copy constructor for the ControlValue class, deep-copying the content of the 'other' ControlValue by using ControlValue::set() operation which, in case of compound ControlValue, guarantees proper memory allocation and data copy. Signed-off-by: Jacopo Mondi --- include/libcamera/controls.h | 5 +++ src/libcamera/controls.cpp | 62 ++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h index bdbdb213528d..d13144b69198 100644 --- a/include/libcamera/controls.h +++ b/include/libcamera/controls.h @@ -43,6 +43,9 @@ public: ControlValue(Span &values); ~ControlValue(); + ControlValue(const ControlValue &other); + ControlValue &operator=(const ControlValue &other); + ControlType type() const { return type_; } bool isNone() const { return type_ == ControlTypeNone; } std::size_t numElements() const { return numElements_; } @@ -81,6 +84,8 @@ private: std::size_t numElements_; void release(); + template + void copyValue(const ControlValue &source); bool compareElement(const ControlValue &other) const; bool compareElement(const ControlValue &other, unsigned int i) const; std::string elemToString(unsigned int i) const; diff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp index c311ab1d6af1..fd04d2311db3 100644 --- a/src/libcamera/controls.cpp +++ b/src/libcamera/controls.cpp @@ -189,6 +189,68 @@ ControlValue::~ControlValue() release(); } +template +void ControlValue::copyValue(const ControlValue &other) +{ + set(other.get()); +} + +/** + * \brief Contruct a ControlValue with the content of \a other + * \param[in] other The ControlValue to copy content from + */ +ControlValue::ControlValue(const ControlValue &other) +{ + *this = other; +} + +/** + * \brief Replace the content of the ControlValue with the one of \a other + * \param[in] other The ControlValue to copy content from + * + * Deep-copy the content of \a other into the ControlValue by reserving memory + * and copy data there in case \a other transports arrays of values in one of + * its pointer data members. + * + * \return The ControlValue with its content replaced with the one of \a other + */ +ControlValue &ControlValue::operator=(const ControlValue &other) +{ + switch (other.type_) { + case ControlTypeBool: + copyValue(other); + break; + case ControlTypeInteger32: + copyValue(other); + break; + case ControlTypeInteger64: + copyValue(other); + break; + case ControlTypeFloat: + copyValue(other); + break; + case ControlTypeCompoundBool: + copyValue>(other); + break; + case ControlTypeCompoundInt32: + copyValue>(other); + break; + case ControlTypeCompoundInt64: + copyValue>(other); + break; + case ControlTypeCompoundFloat: + copyValue>(other); + break; + default: + break; + } + + type_ = other.type_; + numElements_ = other.numElements_; + + return *this; +} + /** * \fn ControlValue::type() * \brief Retrieve the data type of the value