userver: userver/storages/mongo/cursor.hpp Source File
Loading...
Searching...
No Matches
cursor.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/mongo/cursor.hpp
4/// @brief @copybrief storages::mongo::Cursor
5
6#include <iterator>
7#include <memory>
8
9#include <userver/formats/bson/document.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace storages::mongo {
14namespace impl {
15class CursorImpl;
16} // namespace impl
17
18/// Interface for MongoDB query cursors
19class Cursor {
20public:
21 explicit Cursor(std::unique_ptr<impl::CursorImpl>&&);
22 ~Cursor();
23
24 Cursor(Cursor&&) noexcept;
25 Cursor& operator=(Cursor&&) noexcept;
26
27 class Iterator {
28 public:
29 using iterator_category = std::input_iterator_tag;
31 using value_type = formats::bson::Document;
32 using reference = const value_type&;
33 using pointer = const value_type*;
34
35 explicit Iterator(Cursor*);
36
37 class DocHolder {
38 public:
39 explicit DocHolder(value_type doc) : doc_(std::move(doc)) {}
40
41 value_type& operator*() { return doc_; }
42
43 private:
44 value_type doc_;
45 };
46 DocHolder operator++(int);
47 Iterator& operator++();
48 reference operator*() const;
49 pointer operator->() const;
50
51 bool operator==(const Iterator&) const;
52 bool operator!=(const Iterator&) const;
53
54 private:
55 Cursor* cursor_;
56 };
57
58 bool HasMore() const;
59 explicit operator bool() const { return HasMore(); }
60
61 Iterator begin();
62 Iterator end();
63
64private:
65 std::unique_ptr<impl::CursorImpl> impl_;
66};
67
68} // namespace storages::mongo
69
70USERVER_NAMESPACE_END