libcamera  v0.0.0+3695-a2c715d8
Supporting cameras in Linux since 2019
yaml_parser.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /*
3  * Copyright (C) 2022, Google Inc.
4  *
5  * yaml_parser.h - libcamera YAML parsing helper
6  */
7 
8 #pragma once
9 
10 #include <iterator>
11 #include <map>
12 #include <string>
13 #include <vector>
14 
15 #include <libcamera/base/class.h>
16 
17 #include <libcamera/geometry.h>
18 
19 namespace libcamera {
20 
21 class File;
22 class YamlParserContext;
23 
25 {
26 private:
27  using DictContainer = std::map<std::string, std::unique_ptr<YamlObject>>;
28  using ListContainer = std::vector<std::unique_ptr<YamlObject>>;
29 
30 public:
31 #ifndef __DOXYGEN__
32  template<typename Container, typename Derived>
33  class Iterator
34  {
35  public:
36  using difference_type = std::ptrdiff_t;
37  using iterator_category = std::forward_iterator_tag;
38 
39  Iterator(typename Container::const_iterator it)
40  : it_(it)
41  {
42  }
43 
44  Derived &operator++()
45  {
46  ++it_;
47  return *static_cast<Derived *>(this);
48  }
49 
50  Derived operator++(int)
51  {
52  Derived it = *static_cast<Derived *>(this);
53  it_++;
54  return it;
55  }
56 
57  friend bool operator==(const Iterator &a, const Iterator &b)
58  {
59  return a.it_ == b.it_;
60  }
61 
62  friend bool operator!=(const Iterator &a, const Iterator &b)
63  {
64  return a.it_ != b.it_;
65  }
66 
67  protected:
68  typename Container::const_iterator it_;
69  };
70 
71  template<typename Container, typename Iterator>
72  class Adapter
73  {
74  public:
75  Adapter(const Container &container)
76  : container_(container)
77  {
78  }
79 
80  Iterator begin() const
81  {
82  return Iterator{ container_.begin() };
83  }
84 
85  Iterator end() const
86  {
87  return Iterator{ container_.end() };
88  }
89 
90  protected:
91  const Container &container_;
92  };
93 
94  class ListIterator : public Iterator<ListContainer, ListIterator>
95  {
96  public:
97  using value_type = const YamlObject &;
98  using pointer = const YamlObject *;
99  using reference = value_type;
100 
101  value_type operator*() const
102  {
103  return *it_->get();
104  }
105 
106  pointer operator->() const
107  {
108  return it_->get();
109  }
110  };
111 
112  class DictIterator : public Iterator<DictContainer, DictIterator>
113  {
114  public:
115  using value_type = std::pair<const std::string &, const YamlObject &>;
116  using pointer = value_type *;
117  using reference = value_type &;
118 
119  value_type operator*() const
120  {
121  return { it_->first, *it_->second.get() };
122  }
123  };
124 
125  class DictAdapter : public Adapter<DictContainer, DictIterator>
126  {
127  public:
128  using key_type = std::string;
129  };
130 
131  class ListAdapter : public Adapter<ListContainer, ListIterator>
132  {
133  };
134 #endif /* __DOXYGEN__ */
135 
136  YamlObject();
137  ~YamlObject();
138 
139  bool isValue() const
140  {
141  return type_ == Type::Value;
142  }
143  bool isList() const
144  {
145  return type_ == Type::List;
146  }
147  bool isDictionary() const
148  {
149  return type_ == Type::Dictionary;
150  }
151 
152  std::size_t size() const;
153 
154 #ifndef __DOXYGEN__
155  template<typename T,
156  typename std::enable_if_t<
157  std::is_same_v<bool, T> ||
158  std::is_same_v<double, T> ||
159  std::is_same_v<int16_t, T> ||
160  std::is_same_v<uint16_t, T> ||
161  std::is_same_v<int32_t, T> ||
162  std::is_same_v<uint32_t, T> ||
163  std::is_same_v<std::string, T> ||
164  std::is_same_v<Size, T>> * = nullptr>
165 #else
166  template<typename T>
167 #endif
168  T get(const T &defaultValue, bool *ok = nullptr) const;
169 
170  DictAdapter asDict() const { return DictAdapter{ dictionary_ }; }
171  ListAdapter asList() const { return ListAdapter{ list_ }; }
172 
173  const YamlObject &operator[](std::size_t index) const;
174 
175  bool contains(const std::string &key) const;
176  const YamlObject &operator[](const std::string &key) const;
177 
178 private:
180 
181  friend class YamlParserContext;
182 
183  enum class Type {
184  Dictionary,
185  List,
186  Value,
187  };
188 
189  Type type_;
190 
191  std::string value_;
192  ListContainer list_;
193  DictContainer dictionary_;
194 };
195 
196 class YamlParser final
197 {
198 public:
199  static std::unique_ptr<YamlObject> parse(File &file);
200 };
201 
202 } /* namespace libcamera */
Utilities to help constructing class interfaces.
bool operator==(const ColorSpace &lhs, const ColorSpace &rhs)
Compare color spaces for equality.
Definition: color_space.cpp:303
bool isValue() const
Return whether the YamlObject is a value.
Definition: yaml_parser.h:139
const YamlObject & operator[](std::size_t index) const
Retrieve the element from list YamlObject by index.
Definition: yaml_parser.cpp:377
Transform operator*(Transform t0, Transform t1)
Compose two transforms together.
Definition: transform.cpp:207
Top-level libcamera namespace.
Definition: backtrace.h:17
ListAdapter asList() const
Wrap a list YamlObject in an adapter that exposes iterators.
Definition: yaml_parser.h:171
bool isDictionary() const
Return whether the YamlObject is a dictionary.
Definition: yaml_parser.h:147
bool isList() const
Return whether the YamlObject is a list.
Definition: yaml_parser.h:143
Interface for I/O operations on files.
Definition: file.h:24
bool contains(const std::string &key) const
Check if an element of a dictionary exists.
Definition: yaml_parser.cpp:396
#define LIBCAMERA_DISABLE_COPY_AND_MOVE(klass)
Disable copy and move construction and assignment of the klass.
std::size_t size() const
Retrieve the number of elements in a dictionary or list YamlObject.
Definition: yaml_parser.cpp:90
DictAdapter asDict() const
Wrap a dictionary YamlObject in an adapter that exposes iterators.
Definition: yaml_parser.h:170
A class representing the tree structure of the YAML content.
Definition: yaml_parser.h:24
A helper class for parsing a YAML file.
Definition: yaml_parser.h:196
Data structures related to geometric objects.