MongoDB C++ Driver  mongocxx-3.7.0
change_stream.hpp
1 // Copyright 2018-present MongoDB Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <memory>
18 
19 #include <bsoncxx/document/view.hpp>
20 #include <bsoncxx/stdx/optional.hpp>
21 
22 #include <mongocxx/config/prelude.hpp>
23 
24 namespace mongocxx {
25 MONGOCXX_INLINE_NAMESPACE_BEGIN
26 
27 class client;
28 class collection;
29 class database;
30 
34 class MONGOCXX_API change_stream {
35  public:
37  class MONGOCXX_API iterator;
38 
42  change_stream(change_stream&& other) noexcept;
43 
47  change_stream& operator=(change_stream&& other) noexcept;
48 
52  ~change_stream();
53 
74  iterator begin() const;
75 
83  iterator end() const;
84 
108  bsoncxx::stdx::optional<bsoncxx::document::view> get_resume_token() const;
109 
110  private:
111  friend class client;
112  friend class collection;
113  friend class database;
114  friend class change_stream::iterator;
115 
116  MONGOCXX_PRIVATE change_stream(void* change_stream_ptr);
117 
118  class MONGOCXX_PRIVATE impl;
119  std::unique_ptr<impl> _impl;
120 };
121 
125 class MONGOCXX_API change_stream::iterator {
126  public:
127  // Support input-iterator (caveat of post-increment returning void)
128  using difference_type = std::int64_t;
129  using value_type = const bsoncxx::document::view;
130  using pointer = std::add_pointer<value_type>::type;
131  using reference = std::add_lvalue_reference<value_type>::type;
132  using iterator_category = std::input_iterator_tag;
133 
139  iterator();
140 
144  const bsoncxx::document::view& operator*() const;
145 
149  const bsoncxx::document::view* operator->() const;
150 
161  iterator& operator++();
162 
173  void operator++(int);
174 
175  private:
176  friend class change_stream;
177  enum class iter_type { k_tracking, k_default_constructed, k_end };
178 
179  MONGOCXX_PRIVATE explicit iterator(iter_type type, const change_stream* change_stream);
180 
189  friend MONGOCXX_API bool MONGOCXX_CALL operator==(const change_stream::iterator&,
190  const change_stream::iterator&) noexcept;
191 
192  friend MONGOCXX_API bool MONGOCXX_CALL operator!=(const change_stream::iterator&,
193  const change_stream::iterator&) noexcept;
197 
198  MONGOCXX_PRIVATE bool is_exhausted() const;
199 
200  // iter_type==k_default_constructed is equivalent to _change_stream==nullptr
201  iter_type _type;
202  const change_stream* _change_stream;
203 };
204 
205 MONGOCXX_INLINE_NAMESPACE_END
206 } // namespace mongocxx
207 
208 #include <mongocxx/config/postlude.hpp>
mongocxx::change_stream
Class representing a MongoDB change stream.
Definition: change_stream.hpp:34
mongocxx
Top level namespace for the MongoDB C++ driver.
Definition: bulk_write.hpp:24
mongocxx::client
Class representing a client connection to MongoDB.
Definition: client.hpp:58
mongocxx::change_stream::iterator
Class representing a MongoDB change stream iterator.
Definition: change_stream.hpp:125
mongocxx::database
Class representing a MongoDB database.
Definition: database.hpp:43
mongocxx::collection
Class representing server side document groupings within a MongoDB database.
Definition: collection.hpp:83
bsoncxx::document::view
A read-only, non-owning view of a BSON document.
Definition: view.hpp:33