From 3a18ad1607d4963105270f48e78d6b4e94a2133f Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Wed, 25 May 2022 01:58:09 +0300 Subject: libcamera: yaml_parser: Add iterator API Allow using range-based for loops over YamlObject instances by implementing iterators. New YamlObject::DictAdapter and YamlObject::ListAdapter adapter classes are introduced to provide different iterators depending on the object type. Signed-off-by: Laurent Pinchart Reviewed-by: Jacopo Mondi Reviewed-by: Han-Lin Chen --- include/libcamera/internal/yaml_parser.h | 117 ++++++++++++++++++++++++++++++- src/libcamera/yaml_parser.cpp | 39 +++++++++++ 2 files changed, 154 insertions(+), 2 deletions(-) diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h index 29b7b218..d681ba11 100644 --- a/include/libcamera/internal/yaml_parser.h +++ b/include/libcamera/internal/yaml_parser.h @@ -7,6 +7,7 @@ #pragma once +#include #include #include #include @@ -22,7 +23,116 @@ class YamlParserContext; class YamlObject { +private: + using DictContainer = std::map>; + using ListContainer = std::vector>; + public: +#ifndef __DOXYGEN__ + template + class Iterator + { + public: + using difference_type = std::ptrdiff_t; + using iterator_category = std::forward_iterator_tag; + + Iterator(typename Container::const_iterator it) + : it_(it) + { + } + + Derived &operator++() + { + ++it_; + return *static_cast(this); + } + + Derived operator++(int) + { + Derived it = *static_cast(this); + it_++; + return it; + } + + friend bool operator==(const Iterator &a, const Iterator &b) + { + return a.it_ == b.it_; + } + + friend bool operator!=(const Iterator &a, const Iterator &b) + { + return a.it_ != b.it_; + } + + protected: + typename Container::const_iterator it_; + }; + + template + class Adapter + { + public: + Adapter(const Container &container) + : container_(container) + { + } + + Iterator begin() const + { + return Iterator{ container_.begin() }; + } + + Iterator end() const + { + return Iterator{ container_.end() }; + } + + protected: + const Container &container_; + }; + + class ListIterator : public Iterator + { + public: + using value_type = const YamlObject &; + using pointer = const YamlObject *; + using reference = value_type; + + value_type operator*() const + { + return *it_->get(); + } + + pointer operator->() const + { + return it_->get(); + } + }; + + class DictIterator : public Iterator + { + public: + using value_type = std::pair; + using pointer = value_type *; + using reference = value_type &; + + value_type operator*() const + { + return { it_->first, *it_->second.get() }; + } + }; + + class DictAdapter : public Adapter + { + public: + using key_type = std::string; + }; + + class ListAdapter : public Adapter + { + }; +#endif /* __DOXYGEN__ */ + YamlObject(); ~YamlObject(); @@ -55,6 +165,9 @@ public: #endif T get(const T &defaultValue, bool *ok = nullptr) const; + DictAdapter asDict() const { return DictAdapter{ dictionary_ }; } + ListAdapter asList() const { return ListAdapter{ list_ }; } + const YamlObject &operator[](std::size_t index) const; bool contains(const std::string &key) const; @@ -75,8 +188,8 @@ private: Type type_; std::string value_; - std::vector> list_; - std::map> dictionary_; + ListContainer list_; + DictContainer dictionary_; }; class YamlParser final diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp index 85f6694f..4df7e5a3 100644 --- a/src/libcamera/yaml_parser.cpp +++ b/src/libcamera/yaml_parser.cpp @@ -259,6 +259,45 @@ Size YamlObject::get(const Size &defaultValue, bool *ok) const #endif /* __DOXYGEN__ */ +/** + * \fn YamlObject::asDict() const + * \brief Wrap a dictionary YamlObject in an adapter that exposes iterators + * + * The YamlObject class doesn't directly implement iterators, as the iterator + * type depends on whether the object is a Dictionary or List. This function + * wraps a YamlObject of Dictionary type into an adapter that exposes + * iterators, as well as begin() and end() functions, allowing usage of + * range-based for loops with YamlObject. As YAML mappings are not ordered, the + * iteration order is not specified. + * + * The iterator's value_type is a + * std::pair. + * + * If the YamlObject is not of Dictionary type, the returned adapter operates + * as an empty container. + * + * \return An adapter of unspecified type compatible with range-based for loops + */ + +/** + * \fn YamlObject::asList() const + * \brief Wrap a list YamlObject in an adapter that exposes iterators + * + * The YamlObject class doesn't directly implement iterators, as the iterator + * type depends on whether the object is a Dictionary or List. This function + * wraps a YamlObject of List type into an adapter that exposes iterators, as + * well as begin() and end() functions, allowing usage of range-based for loops + * with YamlObject. As YAML lists are ordered, the iteration order is identical + * to the list order in the YAML data. + * + * The iterator's value_type is a const YamlObject &. + * + * If the YamlObject is not of List type, the returned adapter operates as an + * empty container. + * + * \return An adapter of unspecified type compatible with range-based for loops + */ + /** * \fn YamlObject::operator[](std::size_t index) const * \brief Retrieve the element from list YamlObject by index -- cgit v1.2.1