MongoDB C++ Driver  mongocxx-3.7.0
view.hpp
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 
15 #pragma once
16 
17 #include <cstddef>
18 #include <cstdint>
19 #include <iterator>
20 
21 #include <bsoncxx/document/element.hpp>
22 #include <bsoncxx/stdx/string_view.hpp>
23 
24 #include <bsoncxx/config/prelude.hpp>
25 
26 namespace bsoncxx {
27 BSONCXX_INLINE_NAMESPACE_BEGIN
28 namespace document {
29 
33 class BSONCXX_API view {
34  public:
35  class BSONCXX_API const_iterator;
36  using iterator = const_iterator;
37 
42  view();
43 
53  view(const std::uint8_t* data, std::size_t length);
54 
58  const_iterator cbegin() const;
59 
63  const_iterator cend() const;
64 
68  const_iterator begin() const;
69 
73  const_iterator end() const;
74 
90  const_iterator find(stdx::string_view key) const;
91 
102  element operator[](stdx::string_view key) const;
103 
109  const std::uint8_t* data() const;
110 
119  std::size_t length() const;
120 
127  bool empty() const;
128 
136  friend BSONCXX_API bool BSONCXX_CALL operator==(view, view);
137  friend BSONCXX_API bool BSONCXX_CALL operator!=(view, view);
141 
142  private:
143  const std::uint8_t* _data;
144  std::size_t _length;
145 };
146 
153 class BSONCXX_API view::const_iterator {
154  public:
159  using reference = element&;
160  using pointer = element*;
161  using iterator_category = std::forward_iterator_tag;
162  using difference_type = std::ptrdiff_t;
163 
164  const_iterator();
165  explicit const_iterator(const element& element);
166 
167  reference operator*();
168  pointer operator->();
169 
170  const_iterator& operator++();
171  const_iterator operator++(int);
172 
180  friend BSONCXX_API bool BSONCXX_CALL operator==(const const_iterator&, const const_iterator&);
181  friend BSONCXX_API bool BSONCXX_CALL operator!=(const const_iterator&, const const_iterator&);
185 
186  private:
187  element _element;
188 };
189 
190 } // namespace document
191 BSONCXX_INLINE_NAMESPACE_END
192 } // namespace bsoncxx
193 
194 #include <bsoncxx/config/postlude.hpp>
bsoncxx::document::view::const_iterator
A const iterator over the contents of a document view.
Definition: view.hpp:153
bsoncxx
Top level namespace for MongoDB C++ BSON functionality.
Definition: element.hpp:24
bsoncxx::document::element
A variant view type that accesses values in serialized BSON documents.
Definition: element.hpp:76
bsoncxx::document::view
A read-only, non-owning view of a BSON document.
Definition: view.hpp:33