view.hpp 5.1 KB

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