userver: userver/formats/yaml/iterator.hpp Source File
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 <cstdint>
7#include <iterator>
8#include <optional>
9
10#include <userver/formats/yaml/types.hpp>
11#include <userver/utils/fast_pimpl.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace formats::yaml {
16
17/// @brief Iterator for `formats::yaml::Value`
18template <typename iter_traits>
19class Iterator final {
20 public:
21 using iterator_category = std::forward_iterator_tag;
22 using difference_type = std::ptrdiff_t;
23 using value_type = typename iter_traits::value_type;
24 using reference = typename iter_traits::reference;
25 using pointer = typename iter_traits::pointer;
26
27 Iterator(const typename iter_traits::native_iter& iter, int index,
28 const formats::yaml::Path& path);
29 Iterator(const Iterator& other);
30 Iterator(Iterator&& other) noexcept;
31 Iterator& operator=(const Iterator& other);
32 Iterator& operator=(Iterator&& other) noexcept;
33 ~Iterator();
34
35 Iterator operator++(int);
36 Iterator& operator++();
37 reference operator*() const;
38 pointer operator->() const;
39
40 bool operator==(const Iterator& other) const;
41 bool operator!=(const Iterator& other) const;
42
43 /// @brief Returns name of the referenced field
44 /// @throws `TypeMismatchException` if iterated value is not an object
45 std::string GetName() const;
46 /// @brief Returns index of the referenced field
47 /// @throws `TypeMismatchException` if iterated value is not an array
48 uint32_t GetIndex() const;
49
50 /// @brief Returns whether iterator is over array or over object
52
53 private:
54 void UpdateValue() const;
55
56 static constexpr std::size_t kNativeIterSize = 48;
57 static constexpr std::size_t kNativeIterAlignment = alignof(void*);
58 utils::FastPimpl<typename iter_traits::native_iter, kNativeIterSize,
59 kNativeIterAlignment>
60 iter_pimpl_;
61 formats::yaml::Path path_;
62 // Temporary object replaced on every value access.
63 mutable int index_;
64 mutable std::optional<value_type> current_;
65};
66
67} // namespace formats::yaml
68
69USERVER_NAMESPACE_END