[libcamera-devel] [PATCH 2/3] cam: options: Add an array data type to OptionValue
Laurent Pinchart
laurent.pinchart at ideasonboard.com
Tue Mar 26 07:23:37 CET 2019
Hi Niklas,
Thank you for the patch.
On Tue, Mar 26, 2019 at 12:47:35AM +0100, Niklas Söderlund wrote:
> To allow specifying the same argument option multiple times a new type
> of OptionValue is needed. As parsing of options is an iterative process
> there is a need to append options as they are parsed so instead of
> setting values using the constructor a new add() method is used.
>
> Signed-off-by: Niklas Söderlund <niklas.soderlund at ragnatech.se>
> ---
> src/cam/options.cpp | 19 +++++++++++++++++++
> src/cam/options.h | 7 +++++++
> 2 files changed, 26 insertions(+)
>
> diff --git a/src/cam/options.cpp b/src/cam/options.cpp
> index 497833397d894f82..0dec154815d3cad5 100644
> --- a/src/cam/options.cpp
> +++ b/src/cam/options.cpp
> @@ -272,6 +272,12 @@ OptionValue::OptionValue(const KeyValueParser::Options &value)
> {
> }
>
> +void OptionValue::add(const OptionValue &value)
I think add is a bit too generic as a name. I would name this addValue()
or possibly push_back() if we want to stick with the STL semantics (I
think I have a preference for the former though).
> +{
> + type_ = ValueArray;
I wonder if this should error out if type_ is different than ValueArray
or ValueNone. I suppose the caller wouldn't check for this though, and
it would be a bug in the option parser, so I suppose we could keep it
as-is (possibly with an std::assert ?).
Reviewed-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
> + array_.push_back(value);
> +}
> +
> OptionValue::operator int() const
> {
> return toInteger();
> @@ -287,6 +293,11 @@ OptionValue::operator KeyValueParser::Options() const
> return toKeyValues();
> }
>
> +OptionValue::operator std::vector<OptionValue>() const
> +{
> + return toArray();
> +}
> +
> int OptionValue::toInteger() const
> {
> if (type_ != ValueInteger)
> @@ -311,6 +322,14 @@ KeyValueParser::Options OptionValue::toKeyValues() const
> return keyValues_;
> }
>
> +std::vector<OptionValue> OptionValue::toArray() const
> +{
> + if (type_ != ValueArray)
> + return std::vector<OptionValue>{};
> +
> + return array_;
> +}
> +
> /* -----------------------------------------------------------------------------
> * OptionsParser
> */
> diff --git a/src/cam/options.h b/src/cam/options.h
> index b33a90fc6058febf..6a887416c0070c41 100644
> --- a/src/cam/options.h
> +++ b/src/cam/options.h
> @@ -10,6 +10,7 @@
> #include <ctype.h>
> #include <list>
> #include <map>
> +#include <vector>
>
> class KeyValueParser;
> class OptionValue;
> @@ -84,6 +85,7 @@ public:
> ValueInteger,
> ValueString,
> ValueKeyValue,
> + ValueArray,
> };
>
> OptionValue();
> @@ -92,21 +94,26 @@ public:
> OptionValue(const std::string &value);
> OptionValue(const KeyValueParser::Options &value);
>
> + void add(const OptionValue &value);
> +
> ValueType type() const { return type_; }
>
> operator int() const;
> operator std::string() const;
> operator KeyValueParser::Options() const;
> + operator std::vector<OptionValue>() const;
>
> int toInteger() const;
> std::string toString() const;
> KeyValueParser::Options toKeyValues() const;
> + std::vector<OptionValue> toArray() const;
>
> private:
> ValueType type_;
> int integer_;
> std::string string_;
> KeyValueParser::Options keyValues_;
> + std::vector<OptionValue> array_;
> };
>
> class OptionsParser
--
Regards,
Laurent Pinchart
More information about the libcamera-devel
mailing list