MongoDB C++ Driver  mongocxx-3.10.2
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/view-fwd.hpp>
22 
23 #include <bsoncxx/document/element.hpp>
24 #include <bsoncxx/stdx/string_view.hpp>
25 
26 #include <bsoncxx/config/prelude.hpp>
27 
28 namespace bsoncxx {
29 namespace v_noabi {
30 namespace document {
31 
35 class view {
36  public:
37  class BSONCXX_API const_iterator;
38  using iterator = const_iterator;
39 
44  view();
45 
55  view(const std::uint8_t* data, std::size_t length);
56 
61 
66 
71 
76 
92  const_iterator find(stdx::string_view key) const;
93 
104  element operator[](stdx::string_view key) const;
105 
111  const std::uint8_t* data() const;
112 
121  std::size_t length() const;
122 
129  bool empty() const;
130 
138  friend BSONCXX_API bool BSONCXX_CALL operator==(view, view);
139  friend BSONCXX_API bool BSONCXX_CALL operator!=(view, view);
143 
144  private:
145  const std::uint8_t* _data;
146  std::size_t _length;
147 };
148 
156  public:
161  using reference = element&;
162  using pointer = element*;
163  using iterator_category = std::forward_iterator_tag;
164  using difference_type = std::ptrdiff_t;
165 
166  const_iterator();
167  explicit const_iterator(const element& element);
168 
169  reference operator*();
170  pointer operator->();
171 
172  const_iterator& operator++();
173  const_iterator operator++(int);
174 
182  friend BSONCXX_API bool BSONCXX_CALL operator==(const const_iterator&, const const_iterator&);
183  friend BSONCXX_API bool BSONCXX_CALL operator!=(const const_iterator&, const const_iterator&);
187 
188  private:
189  element _element;
190 };
191 
192 } // namespace document
193 } // namespace v_noabi
194 } // namespace bsoncxx
195 
196 #include <bsoncxx/config/postlude.hpp>
A variant view type that accesses values in serialized BSON documents.
Definition: element.hpp:45
A const iterator over the contents of a document view.
Definition: view.hpp:155
friend bool operator!=(const const_iterator &, const const_iterator &)
Compares two const_iterators for (in)-equality.
friend bool operator==(const const_iterator &, const const_iterator &)
Compares two const_iterators for (in)-equality.
A read-only, non-owning view of a BSON document.
Definition: view.hpp:35
bool empty() const
Checks if the underlying document is empty, i.e.
const_iterator cend() const
friend bool operator==(view, view)
Compare two document views for (in)-equality.
std::size_t length() const
Gets the length of the underlying buffer.
view()
Default constructs a view.
const_iterator begin() const
const_iterator cbegin() const
const_iterator end() const
friend bool operator!=(view, view)
Compare two document views for (in)-equality.
element operator[](stdx::string_view key) const
Finds the first element of the document with the provided key.
const_iterator find(stdx::string_view key) const
Finds the first element of the document with the provided key.
const std::uint8_t * data() const
Access the raw bytes of the underlying document.
view(const std::uint8_t *data, std::size_t length)
Constructs a view from a buffer.
The top-level namespace for bsoncxx library entities.
Definition: element-fwd.hpp:19