|
| 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 collection; |
| 28 | + |
| 29 | +class MONGOCXX_API change_stream { |
| 30 | + public: |
| 31 | + class MONGOCXX_API iterator; |
| 32 | + |
| 33 | + /// |
| 34 | + /// Move constructs a change_stream. |
| 35 | + /// |
| 36 | + change_stream(change_stream&& other) noexcept; |
| 37 | + |
| 38 | + /// |
| 39 | + /// Move assigns a change_stream. |
| 40 | + /// |
| 41 | + change_stream& operator=(change_stream&& other) noexcept; |
| 42 | + |
| 43 | + /// |
| 44 | + /// Destroys a change_stream. |
| 45 | + /// |
| 46 | + ~change_stream(); |
| 47 | + |
| 48 | + /// |
| 49 | + /// A change_stream::iterator points to the beginning of any |
| 50 | + /// available notifications. Each call to begin() advances to the next |
| 51 | + /// available notification. The state of all iterators is tracked by the |
| 52 | + /// change_stream itself, so advancing one iterator advances all iterators. |
| 53 | + /// |
| 54 | + /// change_stream::begin() and the increment operators are blocking operations. |
| 55 | + /// They will not return until a notification is available, the max_await_time (from |
| 56 | + /// the options::change_stream) milliseconds have elapsed, or a server |
| 57 | + /// error is encountered. |
| 58 | + /// |
| 59 | + /// When change_stream.begin() == change_stream.end(), no notifications |
| 60 | + /// are available. Each call to change_stream.begin() checks again for |
| 61 | + /// newly-available notifications. |
| 62 | + /// |
| 63 | + /// @return |
| 64 | + /// The change_stream::iterator |
| 65 | + /// @exception |
| 66 | + /// Throws mongocxx::query_exception if the query failed. |
| 67 | + /// |
| 68 | + iterator begin(); |
| 69 | + |
| 70 | + /// |
| 71 | + /// A change_stream::iterator indicating stream exhaustion, meaning that |
| 72 | + /// no notifications are available from the stream. |
| 73 | + /// |
| 74 | + /// @return |
| 75 | + /// The change_stream::iterator indicating exhaustion |
| 76 | + /// |
| 77 | + iterator end(); |
| 78 | + |
| 79 | + private: |
| 80 | + friend class collection; |
| 81 | + friend class change_stream::iterator; |
| 82 | + |
| 83 | + MONGOCXX_PRIVATE change_stream(void* change_stream_ptr); |
| 84 | + |
| 85 | + class MONGOCXX_PRIVATE impl; |
| 86 | + std::unique_ptr<impl> _impl; |
| 87 | +}; |
| 88 | + |
| 89 | +class MONGOCXX_API change_stream::iterator { |
| 90 | + public: |
| 91 | + // Support input-iterator (caveat of post-increment returning void) |
| 92 | + using difference_type = long; |
| 93 | + using value_type = const bsoncxx::document::view; |
| 94 | + using pointer = std::add_pointer<value_type>::type; |
| 95 | + using reference = std::add_lvalue_reference<value_type>::type; |
| 96 | + using iterator_category = std::input_iterator_tag; |
| 97 | + |
| 98 | + /// |
| 99 | + /// Default-construct an iterator. |
| 100 | + /// This is equivalent to change_stream::end() |
| 101 | + /// |
| 102 | + iterator(); |
| 103 | + |
| 104 | + /// |
| 105 | + /// Dereferences the view for the document currently being pointed to. |
| 106 | + /// |
| 107 | + const bsoncxx::document::view& operator*() const noexcept; |
| 108 | + |
| 109 | + /// |
| 110 | + /// Accesses a member of the dereferenced document currently being pointed to. |
| 111 | + /// |
| 112 | + const bsoncxx::document::view* operator->() const noexcept; |
| 113 | + |
| 114 | + /// |
| 115 | + /// Pre-increments the iterator to move to the next document. |
| 116 | + /// |
| 117 | + /// change_stream::begin() and increment operators are blocking operations. |
| 118 | + /// They will not return until a notification is available, the max_await_time (from |
| 119 | + /// the options::change_stream) miliseconds have elapsed, or a server |
| 120 | + /// error is encountered. |
| 121 | + /// |
| 122 | + /// @throws mongocxx::query_exception if the query failed |
| 123 | + /// |
| 124 | + iterator& operator++(); |
| 125 | + |
| 126 | + /// |
| 127 | + /// Post-increments the iterator to move to the next document. |
| 128 | + /// |
| 129 | + /// change_stream::begin() and increment operators are blocking operations. |
| 130 | + /// They will not return until a notification is available, the max_await_time (from |
| 131 | + /// the options::change_stream) miliseconds have elapsed, or a server |
| 132 | + /// error is encountered. |
| 133 | + /// |
| 134 | + /// @throws mongocxx::query_exception if the query failed |
| 135 | + /// |
| 136 | + void operator++(int); |
| 137 | + |
| 138 | + private: |
| 139 | + friend class change_stream; |
| 140 | + |
| 141 | + MONGOCXX_PRIVATE explicit iterator(change_stream* change_stream); |
| 142 | + |
| 143 | + /// |
| 144 | + /// @{ |
| 145 | + /// |
| 146 | + /// Compare two iterators for (in)-equality. Iterators compare equal if |
| 147 | + /// they point to the same underlying change_stream or if both are exhausted. |
| 148 | + /// |
| 149 | + /// @relates iterator |
| 150 | + /// |
| 151 | + friend MONGOCXX_API bool MONGOCXX_CALL operator==(const change_stream::iterator&, |
| 152 | + const change_stream::iterator&) noexcept; |
| 153 | + |
| 154 | + friend MONGOCXX_API bool MONGOCXX_CALL operator!=(const change_stream::iterator&, |
| 155 | + const change_stream::iterator&) noexcept; |
| 156 | + /// |
| 157 | + /// @} |
| 158 | + /// |
| 159 | + |
| 160 | + MONGOCXX_PRIVATE bool is_exhausted() const; |
| 161 | + |
| 162 | + change_stream* _change_stream; |
| 163 | +}; |
| 164 | + |
| 165 | +MONGOCXX_INLINE_NAMESPACE_END |
| 166 | +} // namespace mongocxx |
| 167 | + |
| 168 | +#include <mongocxx/config/postlude.hpp> |
0 commit comments