view.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 <cstddef>
  16. #include <cstdint>
  17. #include <iterator>
  18. #include <bsoncxx/document/element.hpp>
  19. #include <bsoncxx/stdx/string_view.hpp>
  20. #include <bsoncxx/config/prelude.hpp>
  21. namespace bsoncxx {
  22. BSONCXX_INLINE_NAMESPACE_BEGIN
  23. namespace document {
  24. ///
  25. /// A read-only, non-owning view of a BSON document.
  26. ///
  27. class BSONCXX_API view {
  28. public:
  29. class BSONCXX_API const_iterator;
  30. using iterator = const_iterator;
  31. ///
  32. /// Default constructs a view. The resulting view will be initialized to point at
  33. /// an empty BSON document.
  34. ///
  35. view();
  36. ///
  37. /// Constructs a view from a buffer. The caller is responsible for ensuring that
  38. /// the lifetime of the resulting view is a subset of the buffer's.
  39. ///
  40. /// @param data
  41. /// A buffer containing a valid BSON document.
  42. /// @param length
  43. /// The size of the buffer, in bytes.
  44. ///
  45. view(const std::uint8_t* data, std::size_t length);
  46. ///
  47. /// @returns A const_iterator to the first element of the document.
  48. ///
  49. const_iterator cbegin() const;
  50. ///
  51. /// @returns A const_iterator to the past-the-end element of the document.
  52. ///
  53. const_iterator cend() const;
  54. ///
  55. /// @returns A const_iterator to the first element of the document.
  56. ///
  57. const_iterator begin() const;
  58. ///
  59. /// @returns A const_iterator to the past-the-end element of the document.
  60. ///
  61. const_iterator end() const;
  62. ///
  63. /// Finds the first element of the document with the provided key. If there is
  64. /// no such element, the past-the-end iterator will be returned. The runtime of
  65. /// find() is linear in the length of the document. This method only searches
  66. /// the top-level document, and will not recurse to any subdocuments.
  67. ///
  68. /// @remark In BSON, keys are not required to be unique. If there are multiple
  69. /// elements with a matching key in the document, the first matching element from
  70. /// the start will be returned.
  71. ///
  72. /// @param key
  73. /// The key to search for.
  74. ///
  75. /// @return An iterator to the matching element, if found, or the past-the-end iterator.
  76. ///
  77. const_iterator find(stdx::string_view key) const;
  78. ///
  79. /// Finds the first element of the document with the provided key. If there is no
  80. /// such element, the invalid document::element will be returned. The runtime of operator[]
  81. /// is linear in the length of the document.
  82. ///
  83. /// @param key
  84. /// The key to search for.
  85. ///
  86. /// @return The matching element, if found, or the invalid element.
  87. ///
  88. element operator[](stdx::string_view key) const;
  89. ///
  90. /// Access the raw bytes of the underlying document.
  91. ///
  92. /// @return A (non-owning) pointer to the view's buffer.
  93. ///
  94. const std::uint8_t* data() const;
  95. ///
  96. /// Gets the length of the underlying buffer.
  97. ///
  98. /// @remark This is not the number of elements in the document.
  99. /// To compute the number of elements, use std::distance.
  100. ///
  101. /// @return The length of the document, in bytes.
  102. ///
  103. std::size_t length() const;
  104. ///
  105. /// Checks if the underlying document is empty, i.e. it is equivalent to
  106. /// the trivial document '{}'.
  107. ///
  108. /// @return true if the underlying document is empty.
  109. ///
  110. bool empty() const;
  111. ///
  112. /// @{
  113. ///
  114. /// Compare two document views for (in)-equality
  115. ///
  116. /// @relates view
  117. ///
  118. friend BSONCXX_API bool BSONCXX_CALL operator==(view, view);
  119. friend BSONCXX_API bool BSONCXX_CALL operator!=(view, view);
  120. ///
  121. /// @}
  122. ///
  123. private:
  124. const std::uint8_t* _data;
  125. std::size_t _length;
  126. };
  127. ///
  128. /// A const iterator over the contents of a document view.
  129. ///
  130. /// This iterator type provides a const forward iterator interface to document
  131. /// view elements.
  132. ///
  133. class BSONCXX_API view::const_iterator : public std::iterator<std::forward_iterator_tag,
  134. element,
  135. std::ptrdiff_t,
  136. const element*,
  137. const element&> {
  138. public:
  139. const_iterator();
  140. explicit const_iterator(const element& element);
  141. reference operator*();
  142. pointer operator->();
  143. const_iterator& operator++();
  144. const_iterator operator++(int);
  145. ///
  146. /// @{
  147. ///
  148. /// Compares two const_iterators for (in)-equality
  149. ///
  150. /// @relates view::const_iterator
  151. ///
  152. friend BSONCXX_API bool BSONCXX_CALL operator==(const const_iterator&, const const_iterator&);
  153. friend BSONCXX_API bool BSONCXX_CALL operator!=(const const_iterator&, const const_iterator&);
  154. ///
  155. /// @}
  156. ///
  157. private:
  158. element _element;
  159. };
  160. } // namespace document
  161. BSONCXX_INLINE_NAMESPACE_END
  162. } // namespace bsoncxx
  163. #include <bsoncxx/config/postlude.hpp>