libcamera: yaml_parser: Add function to set a YamlObject value
Add a YamlObject::set() function to set the value of an object, with specializations for scalar types. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Isaac Scott <isaac.scott@ideasonboard.com> Reviewed-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
This commit is contained in:
@@ -182,6 +182,13 @@ public:
|
||||
return get<T>().value_or(std::forward<U>(defaultValue));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void set(T &&value)
|
||||
{
|
||||
return Accessor<std::remove_cv_t<std::remove_reference_t<T>>>{}
|
||||
.set(*this, std::forward<T>(value));
|
||||
}
|
||||
|
||||
DictAdapter asDict() const { return DictAdapter{ list_ }; }
|
||||
ListAdapter asList() const { return ListAdapter{ list_ }; }
|
||||
|
||||
@@ -207,6 +214,7 @@ private:
|
||||
template<typename T, typename Enable = void>
|
||||
struct Accessor {
|
||||
std::optional<T> get(const YamlObject &obj) const;
|
||||
void set(YamlObject &obj, T value);
|
||||
};
|
||||
|
||||
Type type_;
|
||||
|
||||
@@ -137,6 +137,22 @@ std::size_t YamlObject::size() const
|
||||
* \return The YamlObject value, or \a defaultValue if parsing failed
|
||||
*/
|
||||
|
||||
/**
|
||||
* \fn template<typename T> YamlObject::set<T>(T &&value)
|
||||
* \brief Set the value of a YamlObject
|
||||
* \param[in] value The value
|
||||
*
|
||||
* This function sets the value stored in a YamlObject to \a value. The value is
|
||||
* converted to a string in an implementation-specific way that guarantees that
|
||||
* subsequent calls to get<T>() will return the same value.
|
||||
*
|
||||
* \todo Implement the conversion guarantee for floating point types
|
||||
*
|
||||
* Values can only be set on YamlObject of Type::Value type or empty YamlObject.
|
||||
* Attempting to set a value on an object of type Type::Dict or Type::List does
|
||||
* not modify the YamlObject.
|
||||
*/
|
||||
|
||||
#ifndef __DOXYGEN__
|
||||
|
||||
template<>
|
||||
@@ -154,6 +170,16 @@ YamlObject::Accessor<bool>::get(const YamlObject &obj) const
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
template<>
|
||||
void YamlObject::Accessor<bool>::set(YamlObject &obj, bool value)
|
||||
{
|
||||
if (obj.type_ != Type::Empty && obj.type_ != Type::Value)
|
||||
return;
|
||||
|
||||
obj.type_ = Type::Value;
|
||||
obj.value_ = value ? "true" : "false";
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
struct YamlObject::Accessor<T, std::enable_if_t<
|
||||
std::is_same_v<int8_t, T> ||
|
||||
@@ -178,6 +204,15 @@ struct YamlObject::Accessor<T, std::enable_if_t<
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
void set(YamlObject &obj, T value)
|
||||
{
|
||||
if (obj.type_ != Type::Empty && obj.type_ != Type::Value)
|
||||
return;
|
||||
|
||||
obj.type_ = Type::Value;
|
||||
obj.value_ = std::to_string(value);
|
||||
}
|
||||
};
|
||||
|
||||
template struct YamlObject::Accessor<int8_t>;
|
||||
@@ -194,6 +229,12 @@ YamlObject::Accessor<float>::get(const YamlObject &obj) const
|
||||
return obj.get<double>();
|
||||
}
|
||||
|
||||
template<>
|
||||
void YamlObject::Accessor<float>::set(YamlObject &obj, float value)
|
||||
{
|
||||
obj.set<double>(value);
|
||||
}
|
||||
|
||||
template<>
|
||||
std::optional<double>
|
||||
YamlObject::Accessor<double>::get(const YamlObject &obj) const
|
||||
@@ -215,6 +256,16 @@ YamlObject::Accessor<double>::get(const YamlObject &obj) const
|
||||
return value;
|
||||
}
|
||||
|
||||
template<>
|
||||
void YamlObject::Accessor<double>::set(YamlObject &obj, double value)
|
||||
{
|
||||
if (obj.type_ != Type::Empty && obj.type_ != Type::Value)
|
||||
return;
|
||||
|
||||
obj.type_ = Type::Value;
|
||||
obj.value_ = std::to_string(value);
|
||||
}
|
||||
|
||||
template<>
|
||||
std::optional<std::string>
|
||||
YamlObject::Accessor<std::string>::get(const YamlObject &obj) const
|
||||
@@ -225,6 +276,16 @@ YamlObject::Accessor<std::string>::get(const YamlObject &obj) const
|
||||
return obj.value_;
|
||||
}
|
||||
|
||||
template<>
|
||||
void YamlObject::Accessor<std::string>::set(YamlObject &obj, std::string value)
|
||||
{
|
||||
if (obj.type_ != Type::Empty && obj.type_ != Type::Value)
|
||||
return;
|
||||
|
||||
obj.type_ = Type::Value;
|
||||
obj.value_ = std::move(value);
|
||||
}
|
||||
|
||||
template<>
|
||||
std::optional<Size>
|
||||
YamlObject::Accessor<Size>::get(const YamlObject &obj) const
|
||||
|
||||
Reference in New Issue
Block a user