From 02e387e7b65252a9cd15b672a87f3bcc6be26399 Mon Sep 17 00:00:00 2001 From: Florian Sylvestre Date: Fri, 22 Jul 2022 17:16:31 +0200 Subject: libcamera: yaml_parser: Add getList() function Allow to retrieve a YAML list of any already supported types in a std::vector. Signed-off-by: Florian Sylvestre Tested-by: Naushir Patuck Reviewed-by: Laurent Pinchart Reviewed-by: Jacopo Mondi Signed-off-by: Laurent Pinchart --- src/libcamera/yaml_parser.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'src') diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp index 89c234fb..440e35c4 100644 --- a/src/libcamera/yaml_parser.cpp +++ b/src/libcamera/yaml_parser.cpp @@ -292,6 +292,59 @@ std::optional YamlObject::get() const #endif /* __DOXYGEN__ */ +/** + * \fn template YamlObject::getList() 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. If parsing fails, std::nullopt + * is returned. + * + * \return The YamlObject value as a std::vector, or std::nullopt if parsing + * failed + */ + +#ifndef __DOXYGEN__ + +template || + std::is_same_v || + std::is_same_v || + std::is_same_v || + std::is_same_v || + std::is_same_v || + std::is_same_v || + std::is_same_v> *> +std::optional> YamlObject::getList() const +{ + if (type_ != Type::List) + return {}; + + std::vector values; + values.reserve(list_.size()); + + for (const YamlObject &entry : asList()) { + const auto value = entry.get(); + if (!value) + return {}; + values.emplace_back(*value); + } + + return values; +} + +template std::optional> YamlObject::getList() const; +template std::optional> YamlObject::getList() const; +template std::optional> YamlObject::getList() const; +template std::optional> YamlObject::getList() const; +template std::optional> YamlObject::getList() const; +template std::optional> YamlObject::getList() const; +template std::optional> YamlObject::getList() const; +template std::optional> YamlObject::getList() const; + +#endif /* __DOXYGEN__ */ + /** * \fn YamlObject::asDict() const * \brief Wrap a dictionary YamlObject in an adapter that exposes iterators -- cgit v1.2.1