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
3/// @file userver/formats/bson/iterator.hpp
4/// @brief @copybrief formats::bson::Iterator
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
14#include <userver/formats/bson/types.hpp>
15#include <userver/formats/common/iterator_direction.hpp>
16
17USERVER_NAMESPACE_BEGIN
18
19namespace formats::bson {
20
21/// Iterator for BSON values
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
31 /// @cond
32 using NativeIter = std::variant<
33 impl::ParsedArray::const_iterator,
34 impl::ParsedArray::const_reverse_iterator,
35 impl::ParsedDocument::const_iterator>;
36
37 Iterator(impl::ValueImpl&, NativeIter);
38 /// @endcond
39
40 Iterator(const Iterator&);
41 Iterator(Iterator&&) noexcept;
42 Iterator& operator=(const Iterator&);
43 Iterator& operator=(Iterator&&) noexcept;
44
45 /// @name Forward iterator requirements
46 /// @{
47 Iterator operator++(int);
48 Iterator& operator++();
49 reference operator*() const;
50 pointer operator->() const;
51
52 bool operator==(const Iterator&) const;
53 bool operator!=(const Iterator&) const;
54 /// @}
55
56 /// @brief Returns name of currently selected document field
57 /// @throws TypeMismatchException if iterated value is not a document
58 template <typename T = void>
59 std::string GetName() const {
60 static_assert(
61 Direction == common::IteratorDirection::kForward,
62 "Reverse iterator should be used only on arrays or null, "
63 "they do not have GetName()"
64 );
65 return GetNameImpl();
66 }
67
68 /// @brief Returns index of currently selected array element
69 /// @throws TypeMismatchException if iterated value is not an array
70 uint32_t GetIndex() const;
71
72private:
73 std::string GetNameImpl() const;
74 void UpdateValue() const;
75
76 impl::ValueImpl* iterable_;
77 NativeIter it_;
78 mutable std::optional<value_type> current_;
79};
80
81} // namespace formats::bson
82
83USERVER_NAMESPACE_END