libcamera: yaml_parser: Replace getList() with get() specializations

The YamlObject class has two member function templates to get values:
the get() function gets a scalar value, while the getList() function
gets a vector of scalar values.

As get() is a function template, we can provide specializations for
vector types. This makes the code more systematic, and therefore more
readable. Replace all getList() occurrences, and drop the getList()
function.

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:
Laurent Pinchart
2025-10-18 00:20:38 +03:00
parent 56e679ee09
commit 3a387cc81d
15 changed files with 64 additions and 103 deletions
+1 -1
View File
@@ -126,7 +126,7 @@ int ConverterDW100Module::init(const YamlObject &params)
return -EINVAL;
}
const auto coeffs = coefficients.getList<double>();
const auto coeffs = coefficients.get<std::vector<double>>();
if (!coeffs) {
LOG(Converter, Error) << "Dewarp parameters 'coefficients' value is not a list";
return -EINVAL;
+1 -1
View File
@@ -152,7 +152,7 @@ std::optional<std::vector<std::string>> GlobalConfiguration::listOption(
if (!*c)
return {};
}
return c->getList<std::string>();
return c->get<std::vector<std::string>>();
}
/**
@@ -115,7 +115,7 @@ int ConfigParser::parseSupportedFormats(const YamlObject &cameraConfigData,
std::vector<int64_t> frameRates;
if (supportedResolution.contains("frame_rates")) {
auto frameRatesList =
supportedResolution["frame_rates"].getList<int>();
supportedResolution["frame_rates"].get<std::vector<int>>();
if (!frameRatesList || (frameRatesList->size() != 1 &&
frameRatesList->size() != 2)) {
LOG(Virtual, Error) << "Invalid frame_rates: either one or two values";
+36 -56
View File
@@ -109,12 +109,16 @@ std::size_t YamlObject::size() const
/**
* \fn template<typename T> YamlObject::get<T>() const
* \tparam T Type of the value
* \brief Parse the YamlObject as a \a T value
*
* This function parses the value of the YamlObject as a \a T object, and
* returns the value. If parsing fails (usually because the YamlObject doesn't
* store a \a T value), std::nullopt is returned.
*
* If the type \a T is an std::vector, the YamlObject will be parsed as a list
* of values.
*
* \return The YamlObject value, or std::nullopt if parsing failed
*/
@@ -127,6 +131,9 @@ std::size_t YamlObject::size() const
* returns the value. If parsing fails (usually because the YamlObject doesn't
* store a \a T value), the \a defaultValue is returned.
*
* Unlike the get() function, this overload does not support std::vector for the
* type \a T.
*
* \return The YamlObject value, or \a defaultValue if parsing failed
*/
@@ -239,65 +246,38 @@ YamlObject::Accessor<Size>::get(const YamlObject &obj) const
return Size(*width, *height);
}
#endif /* __DOXYGEN__ */
/**
* \fn template<typename T> YamlObject::getList<T>() const
* \brief Parse the YamlObject as a list of \a T
*
* This function parses the value of the YamlObject as a list of \a T objects,
* and returns the value as a \a std::vector<T>. If parsing fails, std::nullopt
* is returned.
*
* \return The YamlObject value as a std::vector<T>, or std::nullopt if parsing
* failed
*/
#ifndef __DOXYGEN__
template<typename T,
std::enable_if_t<
std::is_same_v<bool, T> ||
std::is_same_v<float, T> ||
std::is_same_v<double, T> ||
std::is_same_v<int8_t, T> ||
std::is_same_v<uint8_t, T> ||
std::is_same_v<int16_t, T> ||
std::is_same_v<uint16_t, T> ||
std::is_same_v<int32_t, T> ||
std::is_same_v<uint32_t, T> ||
std::is_same_v<std::string, T> ||
std::is_same_v<Size, T>> *>
std::optional<std::vector<T>> YamlObject::getList() const
{
if (type_ != Type::List)
return std::nullopt;
std::vector<T> values;
values.reserve(list_.size());
for (const YamlObject &entry : asList()) {
const auto value = entry.get<T>();
if (!value)
template<typename T>
struct YamlObject::Accessor<std::vector<T>> {
std::optional<std::vector<T>> get(const YamlObject &obj) const
{
if (obj.type_ != Type::List)
return std::nullopt;
values.emplace_back(*value);
std::vector<T> values;
values.reserve(obj.list_.size());
for (const YamlObject &entry : obj.asList()) {
auto value = entry.get<T>();
if (!value)
return std::nullopt;
values.emplace_back(std::move(*value));
}
return values;
}
};
return values;
}
template std::optional<std::vector<bool>> YamlObject::getList<bool>() const;
template std::optional<std::vector<float>> YamlObject::getList<float>() const;
template std::optional<std::vector<double>> YamlObject::getList<double>() const;
template std::optional<std::vector<int8_t>> YamlObject::getList<int8_t>() const;
template std::optional<std::vector<uint8_t>> YamlObject::getList<uint8_t>() const;
template std::optional<std::vector<int16_t>> YamlObject::getList<int16_t>() const;
template std::optional<std::vector<uint16_t>> YamlObject::getList<uint16_t>() const;
template std::optional<std::vector<int32_t>> YamlObject::getList<int32_t>() const;
template std::optional<std::vector<uint32_t>> YamlObject::getList<uint32_t>() const;
template std::optional<std::vector<std::string>> YamlObject::getList<std::string>() const;
template std::optional<std::vector<Size>> YamlObject::getList<Size>() const;
template struct YamlObject::Accessor<std::vector<bool>>;
template struct YamlObject::Accessor<std::vector<float>>;
template struct YamlObject::Accessor<std::vector<double>>;
template struct YamlObject::Accessor<std::vector<int8_t>>;
template struct YamlObject::Accessor<std::vector<uint8_t>>;
template struct YamlObject::Accessor<std::vector<int16_t>>;
template struct YamlObject::Accessor<std::vector<uint16_t>>;
template struct YamlObject::Accessor<std::vector<int32_t>>;
template struct YamlObject::Accessor<std::vector<uint32_t>>;
template struct YamlObject::Accessor<std::vector<std::string>>;
template struct YamlObject::Accessor<std::vector<Size>>;
#endif /* __DOXYGEN__ */
/**