cursor.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright 2014 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. #pragma once
  15. #include <memory>
  16. #include <bsoncxx/document/view.hpp>
  17. #include <bsoncxx/stdx/optional.hpp>
  18. #include <mongocxx/config/prelude.hpp>
  19. namespace mongocxx {
  20. MONGOCXX_INLINE_NAMESPACE_BEGIN
  21. class collection;
  22. ///
  23. /// Class representing a pointer to the result set of a query on a MongoDB server.
  24. ///
  25. /// Clients can iterate through a cursor::iterator to retrieve results.
  26. ///
  27. /// @note By default, cursors timeout after 10 minutes of inactivity.
  28. ///
  29. class MONGOCXX_API cursor {
  30. public:
  31. enum class type { k_non_tailable, k_tailable, k_tailable_await };
  32. class MONGOCXX_API iterator;
  33. ///
  34. /// Move constructs a cursor.
  35. ///
  36. cursor(cursor&&) noexcept;
  37. ///
  38. /// Move assigns a cursor.
  39. ///
  40. cursor& operator=(cursor&&) noexcept;
  41. ///
  42. /// Destroys a cursor.
  43. ///
  44. ~cursor();
  45. ///
  46. /// A cursor::iterator that points to the beginning of any available
  47. /// results. If begin() is called more than once, the cursor::iterator
  48. /// returned points to the next remaining result, not the result of
  49. /// the original call to begin().
  50. ///
  51. /// For a tailable cursor, when cursor.begin() == cursor.end(), no
  52. /// documents are available. Each call to cursor.begin() checks again
  53. /// for newly-available documents.
  54. ///
  55. /// @return the cursor::iterator
  56. ///
  57. /// @throws mongocxx::query_exception if the query failed
  58. ///
  59. iterator begin();
  60. ///
  61. /// A cursor::iterator indicating cursor exhaustion, meaning that
  62. /// no documents are available from the cursor.
  63. ///
  64. /// @return the cursor::iterator
  65. ///
  66. iterator end();
  67. private:
  68. friend class collection;
  69. friend class client;
  70. friend class database;
  71. friend class index_view;
  72. friend class cursor::iterator;
  73. MONGOCXX_PRIVATE cursor(void* cursor_ptr,
  74. bsoncxx::stdx::optional<type> cursor_type = bsoncxx::stdx::nullopt);
  75. class MONGOCXX_PRIVATE impl;
  76. std::unique_ptr<impl> _impl;
  77. };
  78. ///
  79. /// Class representing an input iterator of documents in a MongoDB cursor
  80. /// result set.
  81. ///
  82. /// All non-end iterators derived from the same mongocxx::cursor move in
  83. /// lock-step. Dereferencing any non-end() iterator always gives the first
  84. /// remaining document in the cursor. Incrementing one non-end iterator is
  85. /// equivalent to incrementing them all.
  86. ///
  87. /// An iterator is 'exhausted' when no documents are available. An
  88. /// end-iterator is always exhausted. A non-end iterator is exhausted when the
  89. /// originating mongocxx::cursor has no more documents. When an iterator is
  90. /// exhausted, it must not be dereferenced or incremented.
  91. ///
  92. /// For iterators of a tailable cursor, calling cursor.begin() may revive an
  93. /// exhausted iterator so that it no longer compares equal to the
  94. /// end-iterator.
  95. ///
  96. class MONGOCXX_API cursor::iterator
  97. : public std::iterator<std::input_iterator_tag, bsoncxx::document::view> {
  98. public:
  99. ///
  100. /// Dereferences the view for the document currently being pointed to.
  101. ///
  102. const bsoncxx::document::view& operator*() const;
  103. ///
  104. /// Accesses a member of the dereferenced document currently being pointed to.
  105. ///
  106. const bsoncxx::document::view* operator->() const;
  107. ///
  108. /// Pre-increments the iterator to move to the next document.
  109. ///
  110. /// @throws mongocxx::query_exception if the query failed
  111. ///
  112. iterator& operator++();
  113. ///
  114. /// Post-increments the iterator to move to the next document.
  115. ///
  116. /// @throws mongocxx::query_exception if the query failed
  117. ///
  118. void operator++(int);
  119. private:
  120. friend class cursor;
  121. ///
  122. /// @{
  123. ///
  124. /// Compare two iterators for (in)-equality. Iterators compare equal if
  125. /// they point to the same underlying cursor or if both are exhausted.
  126. ///
  127. /// @relates iterator
  128. ///
  129. friend MONGOCXX_API bool MONGOCXX_CALL operator==(const iterator&, const iterator&);
  130. friend MONGOCXX_API bool MONGOCXX_CALL operator!=(const iterator&, const iterator&);
  131. ///
  132. /// @}
  133. ///
  134. MONGOCXX_PRIVATE bool is_exhausted() const;
  135. MONGOCXX_PRIVATE explicit iterator(cursor* cursor);
  136. // If this pointer is null, the iterator is considered "past-the-end".
  137. cursor* _cursor;
  138. };
  139. MONGOCXX_INLINE_NAMESPACE_END
  140. } // namespace mongocxx
  141. #include <mongocxx/config/postlude.hpp>