userver: userver/yaml_config/iterator.hpp Source File
Loading...
Searching...
No Matches
iterator.hpp
1#pragma once
2
3#include <iterator>
4
5#include <userver/formats/yaml/value.hpp>
6
7USERVER_NAMESPACE_BEGIN
8
9namespace yaml_config {
10
11template <typename iter_traits>
12class Iterator final {
13public:
14 using YamlIterator = formats::yaml::Value::const_iterator;
15 using iterator_category = std::forward_iterator_tag;
16 using difference_type = std::ptrdiff_t;
17 using value_type = typename iter_traits::value_type;
18 using reference = typename iter_traits::reference;
19 using pointer = typename iter_traits::pointer;
20
21 Iterator(const value_type& container, YamlIterator it) : container_(&container), it_(std::move(it)) {}
22
23 Iterator(const Iterator& other);
24 Iterator(Iterator&& other) noexcept;
25 Iterator& operator=(const Iterator&);
26 Iterator& operator=(Iterator&&) noexcept;
27
28 Iterator operator++(int);
29 Iterator& operator++();
30
31 reference operator*() const {
32 UpdateValue();
33 return *current_;
34 }
35 pointer operator->() const {
36 UpdateValue();
37 return &(*current_);
38 }
39
40 /// Return whether this is iterator over object or over array
41 /// @returns formats::common::kArray or formats::common::kObject
42 formats::common::Type GetIteratorType() const { return it_.GetIteratorType(); }
43
44 // Get member name - only if iterator is over object
45 std::string GetName() const;
46
47 bool operator==(const Iterator& other) const { return it_ == other.it_; }
48 bool operator!=(const Iterator& other) const { return it_ != other.it_; }
49
50private:
51 void UpdateValue() const;
52 void IncrementInternalIterator();
53
54 // Pointer to the 'container' yaml - because substitution parsing
55 // only works with container[index/key] statements
56 const value_type* container_{nullptr};
57 // Iterator over container. We actually only use its GetIndex/GetName
58 // members
59 YamlIterator it_;
60 mutable std::optional<value_type> current_;
61};
62
63} // namespace yaml_config
64
65USERVER_NAMESPACE_END