11#include <initializer_list>
30 Value(std::string k, std::unique_ptr<ValueNode> &&v)
31 : key(std::move(k)), value(std::move(v))
35 std::unique_ptr<ValueNode> value;
38 using ValueContainer = std::vector<Value>;
42 template<
typename Derived,
typename ContainerIterator>
46 using difference_type = std::ptrdiff_t;
47 using iterator_category = std::forward_iterator_tag;
49 Iterator(ContainerIterator it)
57 return *
static_cast<Derived *
>(
this);
60 Derived operator++(
int)
62 Derived it = *
static_cast<Derived *
>(
this);
67 friend bool operator==(
const Iterator &a,
const Iterator &b)
69 return a.it_ == b.it_;
72 friend bool operator!=(
const Iterator &a,
const Iterator &b)
74 return a.it_ != b.it_;
78 ContainerIterator it_;
81 template<
typename Iterator,
typename Container>
85 Adapter(Container &container)
86 : container_(container)
90 Iterator begin()
const
92 return Iterator{ container_.begin() };
97 return Iterator{ container_.end() };
101 Container &container_;
104 template<
typename Value,
typename ContainerIterator>
105 class ListIterator :
public Iterator<ListIterator<Value, ContainerIterator>,
109 using Base = Iterator<ListIterator<Value, ContainerIterator>,
113 using value_type = Value;
114 using pointer = value_type *;
115 using reference = value_type &;
119 return *Base::it_->value.get();
122 pointer operator->()
const
124 return Base::it_->value.get();
128 template<
typename Value,
typename ContainerIterator>
129 class DictIterator :
public Iterator<DictIterator<Value, ContainerIterator>,
133 using Base = Iterator<DictIterator<Value, ContainerIterator>,
137 using value_type = std::pair<const std::string &, Value &>;
138 using pointer = value_type *;
139 using reference = value_type &;
143 return { Base::it_->key, *Base::it_->value.get() };
147 class DictAdapter :
public Adapter<DictIterator<ValueNode,
148 ValueContainer::iterator>,
152 using key_type = std::string;
155 class ListAdapter :
public Adapter<ListIterator<ValueNode,
156 ValueContainer::iterator>,
161 class ConstDictAdapter :
public Adapter<DictIterator<const ValueNode,
162 ValueContainer::const_iterator>,
163 const ValueContainer>
166 using key_type = std::string;
169 class ConstListAdapter :
public Adapter<ListIterator<const ValueNode,
170 ValueContainer::const_iterator>,
171 const ValueContainer>
182 set(std::forward<T>(value));
189 return type_ == Type::Value;
193 return type_ == Type::List;
197 return type_ == Type::Dictionary;
201 return type_ == Type::Empty;
203 explicit operator bool()
const
205 return type_ != Type::Empty;
208 std::size_t
size()
const;
211 std::optional<T>
get()
const
213 return Accessor<T>{}.get(*
this);
216 template<
typename T,
typename U>
217 T
get(U &&defaultValue)
const
219 return get<T>().value_or(std::forward<U>(defaultValue));
225 return Accessor<std::remove_cv_t<std::remove_reference_t<T>>>{}
226 .set(*
this, std::forward<T>(value));
229 DictAdapter
asDict() {
return DictAdapter{ list_ }; }
230 ListAdapter
asList() {
return ListAdapter{ list_ }; }
231 ConstDictAdapter
asDict()
const {
return ConstDictAdapter{ list_ }; }
232 ConstListAdapter
asList()
const {
return ConstListAdapter{ list_ }; }
237 bool contains(std::string_view key)
const;
243 ValueNode *
add(std::string key, std::unique_ptr<ValueNode> &&child);
244 ValueNode *
add(std::initializer_list<std::string_view> path,
245 std::unique_ptr<ValueNode> &&child);
247 void erase(std::string_view key);
248 void erase(std::initializer_list<std::string_view> path);
254 friend struct Accessor;
263 template<
typename T,
typename Enable =
void>
265 std::optional<T>
get(
const ValueNode &obj)
const;
266 void set(ValueNode &obj, T value);
272 ValueContainer list_;
273 std::map<std::string, ValueNode *, std::less<>> dictionary_;
Utilities to help constructing class interfaces.
#define LIBCAMERA_DISABLE_COPY_AND_MOVE(klass)
Disable copy and move construction and assignment of the klass.
Definition class.h:29
A class representing a tree structure of values.
Definition value_node.h:27
bool isValue() const
Return whether the ValueNode is a value.
Definition value_node.h:187
ValueNode * at(std::size_t index)
Retrieve the element from list ValueNode by index.
Definition value_node.cpp:404
const ValueNode & operator[](std::size_t index) const
Retrieve the element from list ValueNode by index.
Definition value_node.cpp:423
ValueNode * add(std::unique_ptr< ValueNode > &&child)
Add a child node to a list.
Definition value_node.cpp:528
T get(U &&defaultValue) const
Parse the ValueNode as a T value.
Definition value_node.h:217
void set(T &&value)
Set the value of a ValueNode.
Definition value_node.h:223
bool isEmpty() const
Return whether the ValueNode is an empty.
Definition value_node.h:199
ListAdapter asList()
Wrap a list ValueNode in an adapter that exposes iterators.
Definition value_node.h:230
bool isDictionary() const
Return whether the ValueNode is a dictionary.
Definition value_node.h:195
ConstDictAdapter asDict() const
Wrap a dictionary ValueNode in an adapter that exposes iterators.
Definition value_node.h:231
void erase(std::string_view key)
Erase a child node in a dictionary.
Definition value_node.cpp:633
bool isList() const
Return whether the ValueNode is a list.
Definition value_node.h:191
ValueNode(T &&value)
Construct a ValueNode instance with a value.
Definition value_node.h:179
bool contains(std::string_view key) const
Check if an element of a dictionary exists.
Definition value_node.cpp:442
std::size_t size() const
Retrieve the number of elements in a dictionary or list ValueNode.
Definition value_node.cpp:106
std::optional< T > get() const
Parse the ValueNode as a T value.
Definition value_node.h:211
ConstListAdapter asList() const
Wrap a list ValueNode in an adapter that exposes iterators.
Definition value_node.h:232
DictAdapter asDict()
Wrap a dictionary ValueNode in an adapter that exposes iterators.
Definition value_node.h:229
Top-level libcamera namespace.
Definition backtrace.h:17
Matrix< U, Rows, Cols > operator*(T d, const Matrix< U, Rows, Cols > &m)
Multiply the matrix by a scalar.
Definition matrix.h:133