OGS  v6.4.0
ConfigTree-impl.h
Go to the documentation of this file.
1 
11 #include "ConfigTree.h"
12 
13 #include <sstream>
14 #include <utility>
15 
16 namespace BaseLib
17 {
18 
20 template<typename Iterator>
21 class Range
22 {
23 public:
24  explicit Range(Iterator begin, Iterator end)
25  : begin_(std::move(begin)), end_(std::move(end))
26  {}
27 
28  Iterator begin() const { return begin_; }
29  Iterator end() const { return end_; }
30  std::size_t size() const { return std::distance(begin_, end_); }
31  bool empty() const { return size() == 0; }
32 
33 private:
34  Iterator begin_;
35  Iterator end_;
36 };
37 
38 template<typename T>
39 T
41 getConfigParameter(std::string const& param) const
42 {
43  if (auto p = getConfigParameterOptional<T>(param))
44  {
45  return *p;
46  }
47 
48  error("Key <" + param + "> has not been found");
49 }
50 
51 template<typename T>
52 T
54 getConfigParameter(std::string const& param, T const& default_value) const
55 {
56  if (auto p = getConfigParameterOptional<T>(param))
57  {
58  return *p;
59  }
60 
61  return default_value;
62 }
63 
64 template <typename T>
66  std::string const& param) const
67 {
68  checkUnique(param);
69 
70  return getConfigParameterOptionalImpl(param, static_cast<T*>(nullptr));
71 }
72 
73 template <typename T>
75  std::string const& param, T* /*unused*/) const
76 {
77  if (auto p = getConfigSubtreeOptional(param))
78  {
79  return p->getValue<T>();
80  }
81 
82  return std::nullopt;
83 }
84 
85 template <typename T>
86 std::optional<std::vector<T>> ConfigTree::getConfigParameterOptionalImpl(
87  std::string const& param, std::vector<T>* /*unused*/) const
88 {
89  if (auto p = getConfigSubtreeOptional(param))
90  {
91  std::istringstream sstr{p->getValue<std::string>()};
92  std::vector<T> result;
93  T value;
94  while (sstr >> value)
95  {
96  result.push_back(value);
97  }
98  if (!sstr.eof()) // The stream is not read until the end, must be an
99  // error. result contains number of read values.
100  {
101  error("Value for key <" + param + "> `" +
102  shortString(sstr.str()) +
103  "' not convertible to a vector of the desired type."
104  " Could not convert token no. " +
105  std::to_string(result.size() + 1) + ".");
106  return std::nullopt;
107  }
108 
109  return std::make_optional(result);
110  }
111 
112  return std::nullopt;
113 }
114 
115 template<typename T>
118 getConfigParameterList(std::string const& param) const
119 {
120  checkUnique(param);
121  markVisited<T>(param, Attr::TAG, true);
122 
123  auto p = tree_->equal_range(param);
124  return Range<ValueIterator<T> >(
125  ValueIterator<T>(p.first, param, *this),
126  ValueIterator<T>(p.second, param, *this));
127 }
128 
129 template<typename T>
130 T
132 peekConfigParameter(std::string const& param) const
133 {
134  checkKeyname(param);
135 
136  if (auto p = tree_->get_child_optional(param))
137  {
138  try
139  {
140  return p->get_value<T>();
141  }
142  catch (boost::property_tree::ptree_bad_data const&)
143  {
144  error("Value for key <" + param + "> `" + shortString(p->data()) +
145  "' not convertible to the desired type.");
146  }
147  }
148  else
149  {
150  error("Key <" + param + "> has not been found");
151  }
152 }
153 
154 template<typename T>
155 void
157 checkConfigParameter(std::string const& param, T const& value) const
158 {
159  if (getConfigParameter<T>(param) != value) {
160  error("The value of key <" + param + "> is not the expected one.");
161  }
162 }
163 
164 template<typename Ch>
165 void
167 checkConfigParameter(std::string const& param, Ch const* value) const
168 {
169  if (getConfigParameter<std::string>(param) != value) {
170  error("The value of key <" + param + "> is not the expected one.");
171  }
172 }
173 
174 template<typename T>
175 T
177 getValue() const
178 {
179  if (have_read_data_)
180  {
181  error("The data of this subtree has already been read.");
182  }
183 
184  have_read_data_ = true;
185 
186  if (auto v = tree_->get_value_optional<T>())
187  {
188  return *v;
189  }
190  error("Value `" + shortString(tree_->data()) +
191  "' is not convertible to the desired type.");
192 }
193 
194 template<typename T>
195 T
197 getConfigAttribute(std::string const& attr) const
198 {
199  if (auto a = getConfigAttributeOptional<T>(attr))
200  {
201  return *a;
202  }
203 
204  error("Did not find XML attribute with name '" + attr + "'.");
205 }
206 
207 template <typename T>
208 T ConfigTree::getConfigAttribute(std::string const& attr,
209  T const& default_value) const
210 {
211  if (auto a = getConfigAttributeOptional<T>(attr))
212  {
213  return *a;
214  }
215 
216  return default_value;
217 }
218 
219 template <typename T>
221  std::string const& attr) const
222 {
223  checkUniqueAttr(attr);
224  auto& ct = markVisited<T>(attr, Attr::ATTR, true);
225 
226  if (auto attrs = tree_->get_child_optional("<xmlattr>"))
227  {
228  if (auto a = attrs->get_child_optional(attr)) {
229  ++ct.count; // count only if attribute has been found
230  if (auto v = a->get_value_optional<T>()) {
231  return std::make_optional(*v);
232  }
233  error("Value for XML attribute '" + attr + "' `" +
234  shortString(a->data()) +
235  "' not convertible to the desired type.");
236  }
237  }
238 
239  return std::nullopt;
240 }
241 
242 template<typename T>
245 markVisited(std::string const& key, Attr const is_attr,
246  bool const peek_only) const
247 {
248  auto const type = std::type_index(typeid(T));
249 
250  auto p = visited_params_.emplace(std::make_pair(is_attr, key),
251  CountType{peek_only ? 0 : 1, type});
252 
253  if (!p.second) { // no insertion happened
254  auto& v = p.first->second;
255  if (v.type == type) {
256  if (!peek_only)
257  {
258  ++v.count;
259  }
260  } else {
261  error("There already was an attempt to obtain key <" + key +
262  "> with type '" + v.type.name() + "' (now: '" + type.name() +
263  "').");
264  }
265  }
266 
267  return p.first->second;
268 }
269 
270 } // namespace BaseLib
T peekConfigParameter(std::string const &param) const
bool have_read_data_
Indicates if the plain data contained in this tree has already been read.
Definition: ConfigTree.h:631
void checkConfigParameter(std::string const &param, T const &value) const
void checkUniqueAttr(std::string const &attr) const
Asserts that the attribute attr has not been read yet.
Definition: ConfigTree.cpp:319
std::map< KeyType, CountType > visited_params_
Definition: ConfigTree.h:628
static std::string shortString(std::string const &s)
returns a short string at suitable for error/warning messages
Definition: ConfigTree.cpp:264
void error(std::string const &message) const
Definition: ConfigTree.cpp:219
std::optional< ConfigTree > getConfigSubtreeOptional(std::string const &root) const
Definition: ConfigTree.cpp:160
std::optional< T > getConfigParameterOptional(std::string const &param) const
void checkUnique(std::string const &key) const
Asserts that the key has not been read yet.
Definition: ConfigTree.cpp:309
T getConfigParameter(std::string const &param) const
T getConfigAttribute(std::string const &attr) const
Attr
Used to indicate if dealing with XML tags or XML attributes.
Definition: ConfigTree.h:544
CountType & markVisited(std::string const &key, Attr const is_attr, bool peek_only) const
boost::property_tree::ptree const * tree_
The wrapped tree.
Definition: ConfigTree.h:610
std::optional< T > getConfigParameterOptionalImpl(std::string const &param, T *) const
Default implementation of reading a value of type T.
Range< ValueIterator< T > > getConfigParameterList(std::string const &param) const
std::optional< T > getConfigAttributeOptional(std::string const &attr) const
void checkKeyname(std::string const &key) const
Checks if key complies with the rules [a-z0-9_].
Definition: ConfigTree.cpp:277
Wraps a pair of iterators for use as a range in range-based for-loops.
Range(Iterator begin, Iterator end)
bool empty() const
Iterator end() const
Iterator begin() const
std::size_t size() const