Github   Telegram
Loading...
Searching...
No Matches
iterator.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/formats/yaml/iterator.hpp
4/// @brief @copybrief formats::yaml::Iterator
5
6#include <iterator>
7#include <optional>
8
9#include <userver/formats/yaml/types.hpp>
10#include <userver/utils/fast_pimpl.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace formats::yaml {
15
16/// @brief Iterator for `formats::yaml::Value`
17template <typename iter_traits>
18class Iterator final {
19 public:
20 using iterator_category = std::forward_iterator_tag;
21 using difference_type = std::ptrdiff_t;
22 using value_type = typename iter_traits::value_type;
23 using reference = typename iter_traits::reference;
24 using pointer = typename iter_traits::pointer;
25
26 Iterator(const typename iter_traits::native_iter& iter, int index,
27 const formats::yaml::Path& path);
28 Iterator(const Iterator& other);
29 Iterator(Iterator&& other) noexcept;
30 Iterator& operator=(const Iterator& other);
31 Iterator& operator=(Iterator&& other) noexcept;
32 ~Iterator();
33
34 Iterator operator++(int);
35 Iterator& operator++();
36 reference operator*() const;
37 pointer operator->() const;
38
39 bool operator==(const Iterator& other) const;
40 bool operator!=(const Iterator& other) const;
41
42 /// @brief Returns name of the referenced field
43 /// @throws `TypeMismatchException` if iterated value is not an object
44 std::string GetName() const;
45 /// @brief Returns index of the referenced field
46 /// @throws `TypeMismatchException` if iterated value is not an array
47 uint32_t GetIndex() const;
48
49 /// @brief Returns whether iterator is over array or over object
51
52 private:
53 void UpdateValue() const;
54
55 static constexpr std::size_t kNativeIterSize = 48;
56 static constexpr std::size_t kNativeIterAlignment = alignof(void*);
57 utils::FastPimpl<typename iter_traits::native_iter, kNativeIterSize,
58 kNativeIterAlignment>
59 iter_pimpl_;
60 formats::yaml::Path path_;
61 // Temporary object replaced on every value access.
62 mutable int index_;
63 mutable std::optional<value_type> current_;
64};
65
66} // namespace formats::yaml
67
68USERVER_NAMESPACE_END