From 38987e165c2835d3d172be8a39c64c50903c86d6 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Mon, 18 Jul 2022 09:15:56 +0100 Subject: libcamera: yaml_parser: Preserve order of items in dictionary The std::map container used to store dictionary items in YamlObject doesn't preserve the YAML data order, as maps are ordered by key, not by insertion order. While this is compliant with the YAML specification which doesn't guarantee ordering of mappings, the Raspberry Pi IPA relies on elements being ordered as in the YAML data. To replace the dependency on boost with the YamlParser class, we thus need to guarantee that the order is preserved. Preserve the order by storing items in list_ unconditionally. Turn the list_ vector from storing YamlObject unique pointers to storing key-value pairs, with the key being absent when the object is a list, not a dictionary. The YamlObject implementation is updated to preserve the existing API, with the only difference being that YamlObject::memberNames() now returns member names in the same order as in the YAML file. The ordering is an implementation detail, so changing it doesn't violate the YAML specification. The documentation is not updated to reflect this, as we don't want any new user to rely on a particular ordering. This commit could be reverted if desired when the Raspberry Pi IPA updates to a new tuning data format and drops support for the old format. Signed-off-by: Laurent Pinchart Reviewed-by: Naushir Patuck Tested-by: Naushir Patuck --- include/libcamera/internal/yaml_parser.h | 37 ++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 14 deletions(-) (limited to 'include') diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h index 61f22232..9c85d26a 100644 --- a/include/libcamera/internal/yaml_parser.h +++ b/include/libcamera/internal/yaml_parser.h @@ -25,12 +25,21 @@ class YamlParserContext; class YamlObject { private: - using DictContainer = std::map>; + struct Value { + Value(std::string &&k, std::unique_ptr &&v) + : key(std::move(k)), value(std::move(v)) + { + } + std::string key; + std::unique_ptr value; + }; + + using Container = std::vector; using ListContainer = std::vector>; public: #ifndef __DOXYGEN__ - template + template class Iterator { public: @@ -66,10 +75,10 @@ public: } protected: - typename Container::const_iterator it_; + Container::const_iterator it_; }; - template + template class Adapter { public: @@ -92,7 +101,7 @@ public: const Container &container_; }; - class ListIterator : public Iterator + class ListIterator : public Iterator { public: using value_type = const YamlObject &; @@ -101,16 +110,16 @@ public: value_type operator*() const { - return *it_->get(); + return *it_->value.get(); } pointer operator->() const { - return it_->get(); + return it_->value.get(); } }; - class DictIterator : public Iterator + class DictIterator : public Iterator { public: using value_type = std::pair; @@ -119,17 +128,17 @@ public: value_type operator*() const { - return { it_->first, *it_->second.get() }; + return { it_->key, *it_->value.get() }; } }; - class DictAdapter : public Adapter + class DictAdapter : public Adapter { public: using key_type = std::string; }; - class ListAdapter : public Adapter + class ListAdapter : public Adapter { }; #endif /* __DOXYGEN__ */ @@ -174,7 +183,7 @@ public: return get().value_or(defaultValue); } - DictAdapter asDict() const { return DictAdapter{ dictionary_ }; } + DictAdapter asDict() const { return DictAdapter{ list_ }; } ListAdapter asList() const { return ListAdapter{ list_ }; } const YamlObject &operator[](std::size_t index) const; @@ -196,8 +205,8 @@ private: Type type_; std::string value_; - ListContainer list_; - DictContainer dictionary_; + Container list_; + std::map dictionary_; }; class YamlParser final -- cgit v1.2.1