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