OGS
ConfigTree.h
Go to the documentation of this file.
1
11#pragma once
12
13#include <boost/property_tree/ptree.hpp>
14#include <functional>
15#include <iosfwd>
16#include <iterator>
17#include <map>
18#include <memory>
19#include <optional>
20#include <string>
21#include <string_view>
22#include <typeindex>
23#include <utility>
24#include <vector>
25
26extern template class boost::property_tree::basic_ptree<
27 std::string, std::string, std::less<>>;
28
29namespace BaseLib
30{
31class ConfigTree;
32
39void checkAndInvalidate(ConfigTree* const conf);
40
42void checkAndInvalidate(std::unique_ptr<ConfigTree> const& conf);
43
45void checkAndInvalidate(ConfigTree& conf);
46
47template <typename Iterator>
48class Range;
49
106class ConfigTree final
107{
108public:
115 {
116 public:
117 using iterator_category = std::input_iterator_tag;
119 using difference_type = std::ptrdiff_t;
122
123 using Iterator = boost::property_tree::ptree::const_assoc_iterator;
124
125 explicit SubtreeIterator(Iterator const& it, std::string const& root,
126 ConfigTree const& parent)
127 : it_(it), tagname_(root), parent_(parent)
128 {
129 }
130
132 {
133 ++it_;
134 has_incremented_ = true;
135 return *this;
136 }
137
139 {
140 // if this iterator has been incremented since the last dereference,
141 // tell the parent_ instance that a subtree now has been parsed.
143 {
144 has_incremented_ = false;
146 }
147 return ConfigTree(it_->second, parent_, tagname_);
148 }
149
150 bool operator==(SubtreeIterator const& other) const
151 {
152 return it_ == other.it_;
153 }
154
155 bool operator!=(SubtreeIterator const& other) const
156 {
157 return it_ != other.it_;
158 }
159
160 private:
161 bool has_incremented_ = true;
163
164 protected:
165 std::string const tagname_;
167 };
168
176 {
177 public:
180
182 {
183 auto st = SubtreeIterator::operator*();
184 if (st.hasChildren())
185 {
186 parent_.error("The requested parameter <" + tagname_ +
187 "> has child elements.");
188 }
189 return st;
190 }
191 };
192
199 template <typename ValueType>
201 {
202 public:
203 using iterator_category = std::input_iterator_tag;
205 using difference_type = std::ptrdiff_t;
208
209 using Iterator = boost::property_tree::ptree::const_assoc_iterator;
210
211 explicit ValueIterator(Iterator const& it, std::string const& root,
212 ConfigTree const& parent)
213 : it_(it), tagname_(root), parent_(parent)
214 {
215 }
216
218 {
219 ++it_;
220 has_incremented_ = true;
221 return *this;
222 }
223
224 ValueType operator*()
225 {
226 // if this iterator has been incremented since the last dereference,
227 // tell the parent_ instance that a setting now has been parsed.
229 {
230 has_incremented_ = false;
231 parent_.markVisited<ValueType>(tagname_, Attr::TAG, false);
232 }
233 return ConfigTree(it_->second, parent_, tagname_)
234 .getValue<ValueType>();
235 }
236
237 bool operator==(ValueIterator<ValueType> const& other) const
238 {
239 return it_ == other.it_;
240 }
241
242 bool operator!=(ValueIterator<ValueType> const& other) const
243 {
244 return it_ != other.it_;
245 }
246
247 private:
248 bool has_incremented_ = true;
250 std::string const tagname_;
252 };
253
255 using PTree = boost::property_tree::ptree;
256
264 using Callback = std::function<void(const std::string& filename,
265 const std::string& path,
266 const std::string& message)>;
267
283 explicit ConfigTree(PTree&& top_level_tree,
284 std::string filename,
285 Callback error_cb,
286 Callback warning_cb);
287
289 ConfigTree(ConfigTree const&) = delete;
290
293 ConfigTree(ConfigTree&& other);
294
296 ConfigTree& operator=(ConfigTree const&) = delete;
297
301
303 std::string const& getProjectFileName() const { return filename_; }
304
309
316 template <typename T>
317 T getConfigParameter(std::string const& param) const;
318
328 template <typename T>
329 T getConfigParameter(std::string const& param,
330 T const& default_value) const;
331
341 template <typename T>
342 std::optional<T> getConfigParameterOptional(std::string const& param) const;
343
351 template <typename T>
353 std::string const& param) const;
354
356
369
376 ConfigTree getConfigParameter(std::string const& root) const;
377
384 std::optional<ConfigTree> getConfigParameterOptional(
385 std::string const& param) const;
386
395 std::string const& param) const;
396
403 template <typename T>
404 T getValue() const;
405
412 template <typename T>
413 T getConfigAttribute(std::string const& attr) const;
414
426 template <typename T>
427 T getConfigAttribute(std::string const& attr, T const& default_value) const;
428
436 template <typename T>
437 std::optional<T> getConfigAttributeOptional(std::string const& attr) const;
438
440
448
461 template <typename T>
462 T peekConfigParameter(std::string const& param) const;
463
469 void checkConfigParameter(std::string const& param,
470 std::string_view const value) const;
471
473
477
484 ConfigTree getConfigSubtree(std::string const& root) const;
485
490 std::optional<ConfigTree> getConfigSubtreeOptional(
491 std::string const& root) const;
492
500 Range<SubtreeIterator> getConfigSubtreeList(std::string const& root) const;
501
503
507
514 void ignoreConfigParameter(std::string const& param) const;
515
523 void ignoreConfigParameterAll(std::string const& param) const;
524
531 void ignoreConfigAttribute(std::string const& attr) const;
532
534
538 ~ConfigTree();
539
542 static void onerror(std::string const& filename, std::string const& path,
543 std::string const& message);
544
547 static void onwarning(std::string const& filename, std::string const& path,
548 std::string const& message);
549
551 static void assertNoSwallowedErrors();
552
553private:
555 template <typename T>
556 std::optional<T> getConfigParameterOptionalImpl(std::string const& param,
557 T* /*unused*/) const;
558
560 template <typename T>
561 std::optional<std::vector<T>> getConfigParameterOptionalImpl(
562 std::string const& param, std::vector<T>* /*unused*/) const;
563
565 {
566 int count;
567 std::type_index type;
568 };
569
571 enum class Attr : bool
572 {
573 TAG = false,
574 ATTR = true
575 };
576
578 explicit ConfigTree(PTree const& tree, ConfigTree const& parent,
579 std::string const& root);
580
585 [[noreturn]] void error(std::string const& message) const;
586
589 void warning(std::string const& message) const;
590
592 void checkKeyname(std::string const& key) const;
593
595 std::string joinPaths(std::string const& p1, std::string const& p2) const;
596
598 void checkUnique(std::string const& key) const;
599
601 void checkUniqueAttr(std::string const& attr) const;
602
610 template <typename T>
611 CountType& markVisited(std::string const& key, Attr const is_attr,
612 bool peek_only) const;
613
621 CountType& markVisited(std::string const& key, Attr const is_attr,
622 bool const peek_only) const;
623
626 void markVisitedDecrement(Attr const is_attr, std::string const& key) const;
627
629 bool hasChildren() const;
630
637 void checkAndInvalidate();
638
640 static std::string shortString(std::string const& s);
641
645 std::shared_ptr<PTree const> top_level_tree_;
646
648 PTree const* tree_;
649
651 std::string path_;
652
654 std::string filename_;
655
657 using KeyType = std::pair<Attr, std::string>;
658
666 mutable std::map<KeyType, CountType> visited_params_;
667
670 mutable bool have_read_data_ = false;
671
674
676 static const char pathseparator;
677
679 static const std::string key_chars_start;
680
682 static const std::string key_chars;
683
684 friend void checkAndInvalidate(ConfigTree* const conf);
685 friend void checkAndInvalidate(ConfigTree& conf);
686 friend void checkAndInvalidate(std::unique_ptr<ConfigTree> const& conf);
687};
688
689} // namespace BaseLib
690
691#include "ConfigTree-impl.h" // IWYU pragma: keep
bool operator!=(SubtreeIterator const &other) const
Definition ConfigTree.h:155
std::input_iterator_tag iterator_category
Definition ConfigTree.h:117
boost::property_tree::ptree::const_assoc_iterator Iterator
Definition ConfigTree.h:123
SubtreeIterator(Iterator const &it, std::string const &root, ConfigTree const &parent)
Definition ConfigTree.h:125
bool operator==(SubtreeIterator const &other) const
Definition ConfigTree.h:150
boost::property_tree::ptree::const_assoc_iterator Iterator
Definition ConfigTree.h:209
std::input_iterator_tag iterator_category
Definition ConfigTree.h:203
ValueIterator< ValueType > & operator++()
Definition ConfigTree.h:217
bool operator==(ValueIterator< ValueType > const &other) const
Definition ConfigTree.h:237
ValueIterator(Iterator const &it, std::string const &root, ConfigTree const &parent)
Definition ConfigTree.h:211
bool operator!=(ValueIterator< ValueType > const &other) const
Definition ConfigTree.h:242
T peekConfigParameter(std::string const &param) const
void markVisitedDecrement(Attr const is_attr, std::string const &key) const
void ignoreConfigParameter(std::string const &param) const
static void onerror(std::string const &filename, std::string const &path, std::string const &message)
static void assertNoSwallowedErrors()
Asserts that there have not been any errors reported in the destructor.
Callback onwarning_
Custom warning callback.
Definition ConfigTree.h:673
static const char pathseparator
Character separating two path components.
Definition ConfigTree.h:676
void checkUniqueAttr(std::string const &attr) const
Asserts that the attribute attr has not been read yet.
std::map< KeyType, CountType > visited_params_
Definition ConfigTree.h:666
std::string const & getProjectFileName() const
Used to get the project file name.
Definition ConfigTree.h:303
static std::string shortString(std::string const &s)
returns a short string at suitable for error/warning messages
std::shared_ptr< PTree const > top_level_tree_
Definition ConfigTree.h:645
void error(std::string const &message) const
std::optional< ConfigTree > getConfigSubtreeOptional(std::string const &root) const
std::optional< T > getConfigParameterOptional(std::string const &param) const
void ignoreConfigAttribute(std::string const &attr) const
void checkUnique(std::string const &key) const
Asserts that the key has not been read yet.
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:572
Range< SubtreeIterator > getConfigSubtreeList(std::string const &root) const
static const std::string key_chars_start
Set of allowed characters as the first letter of a key name.
Definition ConfigTree.h:679
ConfigTree getConfigSubtree(std::string const &root) const
std::pair< Attr, std::string > KeyType
A pair (is attribute, tag/attribute name).
Definition ConfigTree.h:657
std::string path_
A path printed in error/warning messages.
Definition ConfigTree.h:651
CountType & markVisited(std::string const &key, Attr const is_attr, bool peek_only) const
void ignoreConfigParameterAll(std::string const &param) const
Callback onerror_
Custom error callback.
Definition ConfigTree.h:672
std::string joinPaths(std::string const &p1, std::string const &p2) const
Used to generate the path of a subtree.
std::optional< T > getConfigParameterOptionalImpl(std::string const &param, T *) const
Default implementation of reading a value of type T.
ConfigTree(ConfigTree const &)=delete
copying is not compatible with the semantics of this class
Range< ValueIterator< T > > getConfigParameterList(std::string const &param) const
std::optional< T > getConfigAttributeOptional(std::string const &attr) const
void warning(std::string const &message) const
bool hasChildren() const
Checks if this tree has any children.
PTree const * tree_
The wrapped tree.
Definition ConfigTree.h:648
ConfigTree(PTree &&top_level_tree, std::string filename, Callback error_cb, Callback warning_cb)
void checkConfigParameter(std::string const &param, std::string_view const value) const
boost::property_tree::ptree PTree
The tree being wrapped by this class.
Definition ConfigTree.h:255
ConfigTree & operator=(ConfigTree const &)=delete
copying is not compatible with the semantics of this class
std::string filename_
The path of the file from which this tree has been read.
Definition ConfigTree.h:654
void checkKeyname(std::string const &key) const
Checks if key complies with the rules [a-z0-9_].
std::function< void(const std::string &filename, const std::string &path, const std::string &message)> Callback
Definition ConfigTree.h:264
static void onwarning(std::string const &filename, std::string const &path, std::string const &message)
static const std::string key_chars
Set of allowed characters in a key name.
Definition ConfigTree.h:682
Wraps a pair of iterators for use as a range in range-based for-loops.
Definition ConfigTree.h:48
void checkAndInvalidate(ConfigTree &conf)
This is an overloaded member function, provided for convenience. It differs from the above function o...