summaryrefslogtreecommitdiff
path: root/include/libcamera/internal/yaml_parser.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/libcamera/internal/yaml_parser.h')
-rw-r--r--include/libcamera/internal/yaml_parser.h68
1 files changed, 52 insertions, 16 deletions
diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h
index 064cf443..8ca71df8 100644
--- a/include/libcamera/internal/yaml_parser.h
+++ b/include/libcamera/internal/yaml_parser.h
@@ -9,6 +9,7 @@
#include <iterator>
#include <map>
+#include <optional>
#include <string>
#include <vector>
@@ -24,12 +25,21 @@ class YamlParserContext;
class YamlObject
{
private:
- using DictContainer = std::map<std::string, std::unique_ptr<YamlObject>>;
+ 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 Container, typename Derived>
+ template<typename Derived>
class Iterator
{
public:
@@ -65,10 +75,10 @@ public:
}
protected:
- typename Container::const_iterator it_;
+ Container::const_iterator it_;
};
- template<typename Container, typename Iterator>
+ template<typename Iterator>
class Adapter
{
public:
@@ -91,7 +101,7 @@ public:
const Container &container_;
};
- class ListIterator : public Iterator<ListContainer, ListIterator>
+ class ListIterator : public Iterator<ListIterator>
{
public:
using value_type = const YamlObject &;
@@ -100,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<DictContainer, DictIterator>
+ class DictIterator : public Iterator<DictIterator>
{
public:
using value_type = std::pair<const std::string &, const YamlObject &>;
@@ -118,17 +128,17 @@ public:
value_type operator*() const
{
- return { it_->first, *it_->second.get() };
+ return { it_->key, *it_->value.get() };
}
};
- class DictAdapter : public Adapter<DictContainer, DictIterator>
+ class DictAdapter : public Adapter<DictIterator>
{
public:
using key_type = std::string;
};
- class ListAdapter : public Adapter<ListContainer, ListIterator>
+ class ListAdapter : public Adapter<ListIterator>
{
};
#endif /* __DOXYGEN__ */
@@ -153,9 +163,35 @@ public:
#ifndef __DOXYGEN__
template<typename T,
- typename std::enable_if_t<
+ std::enable_if_t<
+ std::is_same_v<bool, 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>> * = nullptr>
+#else
+ template<typename T>
+#endif
+ std::optional<T> get() const;
+
+ template<typename T>
+ T get(const T &defaultValue) const
+ {
+ return get<T>().value_or(defaultValue);
+ }
+
+#ifndef __DOXYGEN__
+ template<typename T,
+ std::enable_if_t<
std::is_same_v<bool, 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> ||
@@ -165,9 +201,9 @@ public:
#else
template<typename T>
#endif
- T get(const T &defaultValue, bool *ok = nullptr) const;
+ std::optional<std::vector<T>> getList() const;
- 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;
@@ -189,8 +225,8 @@ private:
Type type_;
std::string value_;
- ListContainer list_;
- DictContainer dictionary_;
+ Container list_;
+ std::map<std::string, YamlObject *> dictionary_;
};
class YamlParser final