userver: userver/formats/bson/iterator.hpp Source File
Loading...
Searching...
No Matches
iterator.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <iterator>
7#include <optional>
8#include <string>
9#include <type_traits>
10#include <unordered_map>
11#include <variant>
12#include <vector>
13
16
17USERVER_NAMESPACE_BEGIN
18
19namespace formats::bson {
20
22template <typename ValueType, common::IteratorDirection Direction = common::IteratorDirection::kForward>
23class Iterator final {
24public:
25 using iterator_category = std::forward_iterator_tag;
26 using difference_type = ptrdiff_t;
27 using value_type = std::remove_const_t<ValueType>;
28 using reference = ValueType&;
29 using pointer = ValueType*;
30
32 using NativeIter = std::variant<
33 impl::ParsedArray::const_iterator,
34 impl::ParsedArray::const_reverse_iterator,
35 impl::ParsedDocument::const_iterator>;
36
37 Iterator();
38 Iterator(impl::ValueImpl&, NativeIter);
40
41 Iterator(const Iterator&);
42 Iterator(Iterator&&) noexcept;
43 Iterator& operator=(const Iterator&);
44 Iterator& operator=(Iterator&&) noexcept;
45
48 Iterator operator++(int);
49 Iterator& operator++();
50 reference operator*() const;
51 pointer operator->() const;
52
53 bool operator==(const Iterator&) const;
54 bool operator!=(const Iterator&) const;
56
60 template <typename T = void>
61 std::string GetName() const {
62 static_assert(
63 Direction == common::IteratorDirection::kForward,
64 "Reverse iterator should be used only on arrays or null, "
65 "they do not have GetName()"
66 );
67 return GetNameImpl();
68 }
69
74 uint32_t GetIndex() const;
75
76private:
77 std::string GetNameImpl() const;
78 void UpdateValue() const;
79
80 impl::ValueImpl* iterable_;
81 NativeIter it_;
82 mutable std::optional<value_type> current_;
83};
84
85} // namespace formats::bson
86
87USERVER_NAMESPACE_END