userver: userver/formats/bson/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/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 =
23 common::IteratorDirection::kForward>
24class Iterator final {
25 public:
26 using iterator_category = std::forward_iterator_tag;
27 using difference_type = ptrdiff_t;
28 using value_type = std::remove_const_t<ValueType>;
29 using reference = ValueType&;
30 using pointer = ValueType*;
31
32 /// @cond
33 using NativeIter = std::variant<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(Direction == common::IteratorDirection::kForward,
61 "Reverse iterator should be used only on arrays or null, "
62 "they do not have GetName()");
63 return GetNameImpl();
64 }
65
66 /// @brief Returns index of currently selected array element
67 /// @throws TypeMismatchException if iterated value is not an array
68 uint32_t GetIndex() const;
69
70 private:
71 std::string GetNameImpl() const;
72 void UpdateValue() const;
73
74 impl::ValueImpl* iterable_;
75 NativeIter it_;
76 mutable std::optional<value_type> current_;
77};
78
79} // namespace formats::bson
80
81USERVER_NAMESPACE_END