[libcamera-devel] [RFC PATCH v2 07/14] libcamera: yaml_parser: Add iterator API
Laurent Pinchart
laurent.pinchart at ideasonboard.com
Sat Jun 4 20:59:32 CEST 2022
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 <laurent.pinchart at ideasonboard.com>
---
include/libcamera/internal/yaml_parser.h | 131 +++++++++++++++++++++--
src/libcamera/yaml_parser.cpp | 37 +++++++
2 files changed, 158 insertions(+), 10 deletions(-)
diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h
index bd1d06d5c488..e142cde0fb6f 100644
--- a/include/libcamera/internal/yaml_parser.h
+++ b/include/libcamera/internal/yaml_parser.h
@@ -7,6 +7,7 @@
#pragma once
+#include <iterator>
#include <map>
#include <string>
#include <vector>
@@ -22,7 +23,123 @@ class YamlParserContext;
class YamlObject
{
+private:
+ struct Value {
+ Value(std::string &&k, std::unique_ptr<YamlObject> &&v)
+ : key(std::move(k)), value(std::move(v))
+ {
+ }
+ std::string key;
+ std::unique_ptr<YamlObject> value;
+ };
+
+ using Container = std::vector<Value>;
+ using ListContainer = std::vector<std::unique_ptr<YamlObject>>;
+
public:
+#ifndef __DOXYGEN__
+ template<typename Derived>
+ class Iterator
+ {
+ public:
+ using difference_type = std::ptrdiff_t;
+ using iterator_category = std::bidirectional_iterator_tag;
+
+ Iterator(typename Container::const_iterator it)
+ : it_(it)
+ {
+ }
+
+ Derived &operator++()
+ {
+ ++it_;
+ return *static_cast<Derived *>(this);
+ }
+
+ Derived operator++(int)
+ {
+ Derived it = *static_cast<Derived *>(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:
+ Container::const_iterator it_;
+ };
+
+ template<typename Iterator>
+ 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<ListIterator>
+ {
+ public:
+ using value_type = const YamlObject &;
+ using pointer = const YamlObject *;
+ using reference = value_type;
+
+ value_type operator*() const
+ {
+ return *it_->value.get();
+ }
+
+ pointer operator->() const
+ {
+ return it_->value.get();
+ }
+ };
+
+ class DictIterator : public Iterator<DictIterator>
+ {
+ public:
+ using value_type = std::pair<const std::string &, const YamlObject &>;
+ using pointer = value_type *;
+ using reference = value_type &;
+
+ value_type operator*() const
+ {
+ return { it_->key, *it_->value.get() };
+ }
+ };
+
+ class DictAdapter : public Adapter<DictIterator>
+ {
+ };
+
+ class ListAdapter : public Adapter<ListIterator>
+ {
+ };
+#endif /* __DOXYGEN__ */
+
YamlObject();
~YamlObject();
@@ -55,6 +172,9 @@ public:
#endif
T get(const T &defaultValue, bool *ok = nullptr) const;
+ DictAdapter asDict() const { return DictAdapter{ list_ }; }
+ ListAdapter asList() const { return ListAdapter{ list_ }; }
+
const YamlObject &operator[](std::size_t index) const;
bool contains(const std::string &key) const;
@@ -72,19 +192,10 @@ private:
Value,
};
- struct Value {
- Value(std::string &&k, std::unique_ptr<YamlObject> &&v)
- : key(std::move(k)), value(std::move(v))
- {
- }
- std::string key;
- std::unique_ptr<YamlObject> value;
- };
-
Type type_;
std::string value_;
- std::vector<Value> list_;
+ Container list_;
std::map<std::string, YamlObject *> dictionary_;
};
diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp
index 83813e2a8552..9cdb370d0f2e 100644
--- a/src/libcamera/yaml_parser.cpp
+++ b/src/libcamera/yaml_parser.cpp
@@ -258,6 +258,43 @@ 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.
+ *
+ * The iterator's value_type is a
+ * <em>std::pair<const std::string &, const \ref YamlObject &></em>.
+ *
+ * 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.
+ *
+ * The iterator's value_type is a <em>const YamlObject &</em>.
+ *
+ * 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
--
Regards,
Laurent Pinchart
More information about the libcamera-devel
mailing list