From patchwork Fri Jun 21 16:13:53 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 1494 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id B89E160E4D for ; Fri, 21 Jun 2019 18:14:06 +0200 (CEST) Received: from localhost.localdomain (cpc89242-aztw30-2-0-cust488.18-1.cable.virginm.net [86.31.129.233]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 43E1D33C; Fri, 21 Jun 2019 18:14:06 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1561133646; bh=+5rebP32rbsRGWdoofnbvFIibREVMlWM+svdDmUePKQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=reKn0PQPRuZo9r5cT+PRJoRuYG1ZMy/p6mMed4BA/G4TTjGDGmOrUlLNmoWxz+vhw b7SPg1lwFUuE20EZibcX1+q2KVZu9XvqzriX10GCTZ+LUOr7ZMP5dwx7pFtblVR6Bg yvEjVFYWtDhUYIacpLUJ/Bc14hDPUC1JFz5Fa3TM= From: Kieran Bingham To: LibCamera Devel Date: Fri, 21 Jun 2019 17:13:53 +0100 Message-Id: <20190621161401.28337-2-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190621161401.28337-1-kieran.bingham@ideasonboard.com> References: <20190621161401.28337-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC PATCH v2 1/9] libcamera: Value: Provide abstract value class X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 21 Jun 2019 16:14:07 -0000 To facilitate passing typed data generically, implement a class which stores the type and value as efficiently as possible. Signed-off-by: Kieran Bingham --- include/libcamera/meson.build | 1 + include/libcamera/value.h | 63 ++++++++++ src/libcamera/meson.build | 1 + src/libcamera/value.cpp | 226 ++++++++++++++++++++++++++++++++++ 4 files changed, 291 insertions(+) create mode 100644 include/libcamera/value.h create mode 100644 src/libcamera/value.cpp diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build index 201832105457..eb2211ae1fc3 100644 --- a/include/libcamera/meson.build +++ b/include/libcamera/meson.build @@ -12,6 +12,7 @@ libcamera_api = files([ 'signal.h', 'stream.h', 'timer.h', + 'value.h', 'version.h', ]) diff --git a/include/libcamera/value.h b/include/libcamera/value.h new file mode 100644 index 000000000000..00c5d53d5cc0 --- /dev/null +++ b/include/libcamera/value.h @@ -0,0 +1,63 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * value.h - Abstract value handling + */ + +#include + +#ifndef __LIBCAMERA_VALUE_H__ +#define __LIBCAMERA_VALUE_H__ + +namespace libcamera { + +enum ValueType : uint8_t { + ValueNull, + ValueBool, + ValueInteger, + ValueInteger64, + ValueString, +}; + +class Value +{ +public: + Value(); + Value(bool value); + Value(int value); + Value(const char *value); + Value(const std::string &value); + + ValueType type() const { return type_; }; + bool isNull() const { return type_ == ValueNull; }; + + void set(bool value); + void set(int value); + void set(int64_t value); + void set(const char *value); + void set(const std::string &value); + + bool getBool() const; + int getInt() const; + int getInt64() const; + std::string getString() const; + + std::string toString() const; + +private: + ValueType type_; + + union { + bool bool_; + int integer_; + int64_t integer64_; + }; + std::string string_; +}; + +std::ostream &operator<<(std::ostream &stream, const Value &value); + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_VALUE_H__ */ diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index 68c7ce14b5b4..8e68373118df 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -27,6 +27,7 @@ libcamera_sources = files([ 'v4l2_device.cpp', 'v4l2_subdevice.cpp', 'v4l2_videodevice.cpp', + 'value.cpp', 'version.cpp', ]) diff --git a/src/libcamera/value.cpp b/src/libcamera/value.cpp new file mode 100644 index 000000000000..f713907b38dd --- /dev/null +++ b/src/libcamera/value.cpp @@ -0,0 +1,226 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * value.cpp - Abstract value handling + */ + +#include +#include +#include + +#include + +#include "log.h" // For ASSERT() + +/** + * \file value.h + * \brief Handles variant value types as an abstract object + */ + +namespace libcamera { + +/** + * \enum ValueType + * Determines the type of value represented by a \a Value + * \var ValueNull + * Identifies an unset value + * \var ValueBool + * Identifies controls storing a boolean value + * \var ValueInteger + * Identifies controls storing an integer value + * \var ValueString + * Identifies controls storing a string value + */ + +/** + * \class Value + * \brief Abstract a value for a common data exchange + */ + +/** + * \brief Construct an empty Value. + * The Value must be \a set() before being used. + */ +Value::Value() + : type_(ValueNull) +{ +} + +/** + * \brief Construct a Boolean Value + * \param[in] value Boolean value to store + */ +Value::Value(bool value) + : type_(ValueBool), bool_(value) +{ +} + +/** + * \brief Construct an integer Value + * \param[in] value Integer value to store + */ +Value::Value(int value) + : type_(ValueInteger), integer_(value) +{ +} + +/** + * \brief Construct a string Value + * \param[in] value String representation to store + */ +Value::Value(const char *value) + : type_(ValueString), string_(value) +{ +} + +/** + * \brief Construct a string Value + * \param[in] value String representation to store + */ +Value::Value(const std::string &value) + : type_(ValueString), string_(value) +{ +} + +/** + * \fn Value::type + * \brief Return the type of value represented by the object + */ + +/** + * \fn Value::isNull + * \brief Determines if the Value is initialised + * \return true if the value type is ValueNull, false otherwise + */ + +/** + * \brief Set the value with a boolean + * \param[in] value Boolean value to store + */ +void Value::set(bool value) +{ + type_ = ValueBool; + bool_ = value; +} + +/** + * \brief Set the value with an integer + * \param[in] value Integer value to store + */ +void Value::set(int value) +{ + type_ = ValueInteger; + integer_ = value; +} + +/** + * \brief Set the value with a 64 bit integer + * \param[in] value Integer value to store + */ +void Value::set(int64_t value) +{ + type_ = ValueInteger64; + integer64_ = value; +} + +/** + * \brief Set the value with a string representation + * \param[in] value String value to store + */ +void Value::set(const char *value) +{ + type_ = ValueString; + string_ = value; +} + +/** + * \brief Set the value with a string representation + * \param[in] value String value to store + */ +void Value::set(const std::string &value) +{ + type_ = ValueString; + string_ = value; +} + +/** + * \brief Get the boolean value. + * + * The Value type must be Boolean. + */ +bool Value::getBool() const +{ + ASSERT(type_ == ValueBool); + + return bool_; +} + +/** + * \brief Get the integer value. + * + * The Value type must be Integer. + */ +int Value::getInt() const +{ + ASSERT(type_ == ValueInteger); + + return integer_; +} + +/** + * \brief Get the 64bit integer value. + * + * The Value type must be Integer64. + */ +int Value::getInt64() const +{ + ASSERT(type_ == ValueInteger64); + + return integer64_; +} + +/** + * \brief Get the string value. + * + * The Value type must be String. + */ +std::string Value::getString() const +{ + ASSERT(type_ == ValueString); + + return string_; +} + +/** + * \brief Prepare a string representation of the Value + */ +std::string Value::toString() const +{ + switch (type_) { + case ValueNull: + return ""; + case ValueBool: + return bool_ ? "True" : "False"; + case ValueInteger: + return std::to_string(integer_); + case ValueInteger64: + return std::to_string(integer64_); + case ValueString: + return string_; + } + + /* Unreachable */ + return ""; +} + +/** + * \brief Provide a string stream representation of the Value \a value to + * the \a stream. + */ +std::ostream &operator<<(std::ostream &stream, const Value &value) +{ + return stream << value.toString(); +} + +} /* namespace libcamera */