MongoDB C++ Driver 4.2.0
Loading...
Searching...
No Matches
view.hpp
Go to the documentation of this file.
1// Copyright 2009-present 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 <bsoncxx/document/view-fwd.hpp> // IWYU pragma: export
18
19//
20
21#include <bsoncxx/v1/document/view.hpp> // IWYU pragma: export
22
23#include <cstddef>
24#include <cstdint>
25#include <cstring>
26#include <iterator>
27
30
32
33namespace bsoncxx {
34namespace v_noabi {
35namespace document {
36
40class view {
41 private:
43 std::size_t _length;
44
45 public:
46 class const_iterator;
47
52
61 view() : _view{}, _length{_view.length()} {}
62
66 /* explicit(false) */ view(v1::document::view const& v) : _view{v}, _length{v.length()} {}
67
81 view(std::uint8_t const* data, std::size_t length) : _view{data}, _length{length} {}
82
94 explicit operator v1::document::view() const {
95 return _view;
96 }
97
102
106 const_iterator cend() const;
107
111 const_iterator begin() const;
112
116 const_iterator end() const;
117
134
146
152 std::uint8_t const* data() const {
153 return _view.data();
154 }
155
163 std::size_t size() const {
164 return _length; // Do NOT use _view.size().
165 }
166
168 std::size_t length() const {
169 return _length; // Do NOT use _view.length().
170 }
171
178 bool empty() const {
179 return _length == 5u; // Do NOT use _view.empty().
180 }
181
188 friend bool operator==(view lhs, view rhs) {
189 return (lhs._length == rhs._length) && (std::memcmp(lhs.data(), rhs.data(), lhs._length) == 0);
190 }
191
192 friend bool operator!=(view lhs, view rhs) {
193 return !(lhs == rhs);
194 }
195
197};
198
206 private:
208
209 public:
214 using reference = element&;
215 using pointer = element*;
216 using iterator_category = std::forward_iterator_tag;
217 using difference_type = std::ptrdiff_t;
218
219 const_iterator() = default;
220
221 explicit const_iterator(v_noabi::document::element const& element) : _element(element) {}
222
223 reference operator*() {
224 return _element;
225 }
226
227 pointer operator->() {
228 return &_element;
229 }
230
231 BSONCXX_ABI_EXPORT_CDECL(const_iterator&) operator++();
232
233 const_iterator operator++(int) {
234 const_iterator before(*this);
235 operator++();
236 return before;
237 }
238
245 friend bool operator==(const_iterator const& lhs, const_iterator const& rhs) {
246 return lhs._element.raw() == rhs._element.raw() && lhs._element.offset() == rhs._element.offset();
247 }
248
249 friend bool operator!=(const_iterator const& lhs, const_iterator const& rhs) {
250 return !(lhs == rhs);
251 }
252
254};
255
257 return *(this->find(key));
258}
259
261 return {};
262}
263
265 return this->cbegin();
266}
267
269 return this->cend();
270}
271
272} // namespace document
273} // namespace v_noabi
274} // namespace bsoncxx
275
276namespace bsoncxx {
277namespace v_noabi {
278
283 return {v};
284}
285
292
293} // namespace v_noabi
294} // namespace bsoncxx
295
297
#define BSONCXX_ABI_EXPORT_CDECL(...)
Equivalent to BSONCXX_ABI_EXPORT with BSONCXX_ABI_CDECL.
Definition export.hpp:52
The bsoncxx v_noabi macro guard postlude header.
The bsoncxx v_noabi macro guard prelude header.
A const iterator over the contents of a document view.
Definition view.hpp:205
view()
Default constructs a view.
Definition view.hpp:61
A non-owning, read-only BSON document.
Definition view.hpp:54
A polyfill for std::string_view.
Definition string_view.hpp:412
A variant view type that accesses values in serialized BSON documents.
Definition element.hpp:52
std::uint8_t const * raw() const
Getter for the raw bson bytes the element points to.
Definition element.hpp:90
std::uint32_t offset() const
Getter for the offset into the raw bson bytes the element points to.
Definition element.hpp:108
A const iterator over the contents of a document view.
Definition view.hpp:205
element value_type
std::iterator_traits
Definition view.hpp:213
friend bool operator!=(const_iterator const &lhs, const_iterator const &rhs)
Compares two const_iterators for (in)-equality.
Definition view.hpp:249
friend bool operator==(const_iterator const &lhs, const_iterator const &rhs)
Compares two const_iterators for (in)-equality.
Definition view.hpp:245
A read-only, non-owning view of a BSON document.
Definition view.hpp:40
const_iterator find(v1::stdx::string_view key) const
Finds the first element of the document with the provided key. If there is no such element,...
const_iterator cend() const
Definition view.hpp:260
bool empty() const
Return true when this->length() == 5.
Definition view.hpp:178
std::uint8_t const * data() const
Access the raw bytes of the underlying document.
Definition view.hpp:152
const_iterator begin() const
Definition view.hpp:264
view(v1::document::view const &v)
Construct with the bsoncxx::v1 equivalent.
Definition view.hpp:66
v_noabi::document::element operator[](v1::stdx::string_view key) const
Finds the first element of the document with the provided key. If there is no such element,...
Definition view.hpp:256
view(std::uint8_t const *data, std::size_t length)
Constructs a view from a buffer. The caller is responsible for ensuring that the lifetime of the resu...
Definition view.hpp:81
friend bool operator!=(view lhs, view rhs)
Compare two document views for (in)-equality.
Definition view.hpp:192
friend bool operator==(view lhs, view rhs)
Compare two document views for (in)-equality.
Definition view.hpp:188
std::size_t size() const
Gets the length of the underlying buffer in bytes.
Definition view.hpp:163
std::size_t length() const
Gets the length of the underlying buffer in bytes.
Definition view.hpp:168
view()
Default constructs a view.
Definition view.hpp:61
const_iterator end() const
Definition view.hpp:268
const_iterator cbegin() const
const_iterator iterator
Equivalent to const_iterator.
Definition view.hpp:51
Provides bsoncxx::v_noabi::document::element.
Declares entities representing a BSON document.
Declares entities whose ABI stability is NOT guaranteed.
v1::element::view to_v1(v_noabi::array::element const &v)
Convert to the bsoncxx::v1 equivalent of v.
Definition element.hpp:132
v_noabi::array::value from_v1(v1::array::value const &v)
Convert from the bsoncxx::v1 equivalent of v.
Definition value.hpp:267
The top-level namespace within which all bsoncxx library entities are declared.
Provides bsoncxx::v1::document::view.
Declares bsoncxx::v_noabi::document::view.
Provides std::string_view-related polyfills for library API usage.