MongoDB C++ Driver 4.4.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/v1/document/view-fwd.hpp> // IWYU pragma: export
18
19//
20
22
25#include <bsoncxx/v1/element/view.hpp> // IWYU pragma: export
27
28#include <cstddef>
29#include <cstdint>
30#include <cstring>
31#include <iterator>
32#include <system_error>
33#include <type_traits>
34
35namespace bsoncxx {
36namespace v1 {
37namespace document {
38
52class view {
53 public:
54 class const_iterator;
55
60
61 private:
62 enum : std::size_t { _empty_length = 5u };
63
64 std::uint8_t const* _data;
65
66 template <detail::endian e = detail::endian::native>
67 std::int32_t raw_size() const;
68
69 public:
79
87 explicit view(std::uint8_t const* data) : _data{data} {}
88
99 BSONCXX_ABI_EXPORT view(std::uint8_t const* data, std::size_t length);
100
104 std::uint8_t const* data() const {
105 return _data;
106 }
107
116 std::size_t size() const;
117
119 std::size_t length() const {
120 return this->size();
121 }
122
132 bool empty() const {
133 return this->size() == _empty_length && _data[4] == 0u;
134 }
135
141 explicit operator bool() const {
142 return this->size() >= _empty_length;
143 }
144
154
158 const_iterator cend() const;
159
161 const_iterator begin() const;
162
164 const_iterator end() const;
165
178
191
199 friend bool operator==(view lhs, view rhs) {
200 if (!lhs != !rhs) {
201 return false;
202 }
203
204 return !lhs || (lhs.length() == rhs.length() && std::memcmp(lhs.data(), rhs.data(), lhs.length()) == 0);
205 }
206
207 friend bool operator!=(view lhs, view rhs) {
208 return !(lhs == rhs);
209 }
210
212
221
225 static BSONCXX_ABI_EXPORT_CDECL(std::error_category const&) error_category();
226
230 friend std::error_code make_error_code(errc v) {
231 return {static_cast<int>(v), error_category()};
232 }
233};
234
235template <>
236inline std::int32_t view::raw_size<detail::endian::little>() const {
237 std::int32_t res;
238 std::memcpy(&res, _data, sizeof(res));
239 return res;
240}
241
242template <>
243inline std::int32_t view::raw_size<detail::endian::big>() const {
244 std::int32_t res;
245 auto const bytes = reinterpret_cast<unsigned char*>(&res);
246 bytes[0] = _data[3];
247 bytes[1] = _data[2];
248 bytes[2] = _data[1];
249 bytes[3] = _data[0];
250 return res;
251}
252
253inline std::size_t view::size() const {
254 return _data ? static_cast<std::uint32_t>(this->raw_size()) : 0u;
255}
256
265 private:
266 v1::element::view _element;
267
268 public:
272 using iterator_category = std::input_iterator_tag;
273
278
282 using difference_type = std::ptrdiff_t;
283
287 using pointer = value_type const*;
288
293
297 const_iterator() = default;
298
305 return _element;
306 }
307
314 return &_element;
315 }
316
323
330 const_iterator tmp = *this;
331 this->operator++();
332 return tmp;
333 }
334
341 friend bool operator==(const_iterator const& lhs, const_iterator const& rhs) {
342 if (!lhs._element != !rhs._element) {
343 return false;
344 }
345
346 return !lhs._element ||
347 (lhs._element.raw() == rhs._element.raw() && lhs._element.offset() == rhs._element.offset());
348 }
349
353 friend bool operator!=(const_iterator const& lhs, const_iterator const& rhs) {
354 return !(lhs == rhs);
355 }
356
357 class internal;
358
359 private:
361};
362
363inline view::const_iterator view::cend() const {
364 return {};
365}
366
368 return this->cbegin();
369}
370
372 return this->cend();
373}
374
376 return *(this->find(key));
377}
378
379} // namespace document
380} // namespace v1
381} // namespace bsoncxx
382
383template <>
384struct std::is_error_code_enum<bsoncxx::v1::document::view::errc> : true_type {};
385
387
For internal use only!
Provides macros to control the set of symbols exported in the ABI.
#define BSONCXX_ABI_EXPORT
Exports the associated entity as part of the ABI.
Definition export.hpp:15
#define BSONCXX_ABI_EXPORT_CDECL(...)
Equivalent to BSONCXX_ABI_EXPORT with BSONCXX_ABI_CDECL.
Definition export.hpp:52
The bsoncxx v1 macro guard postlude header.
The bsoncxx v1 macro guard prelude header.
A const iterator over the elements of a view.
Definition view.hpp:264
const_iterator operator++(int)
Post-increment this iterator.
Definition view.hpp:329
friend bool operator!=(const_iterator const &lhs, const_iterator const &rhs)
Equivalent to !(lhs == rhs).
Definition view.hpp:353
const_iterator()=default
Initialize as an end iterator.
friend bool operator==(const_iterator const &lhs, const_iterator const &rhs)
Compare equal when lhs and rhs point to the same element within the same range of BSON bytes.
Definition view.hpp:341
const_iterator & operator++()
Pre-increment this iterator.
v1::element::view value_type
Provide std::iterator_traits<T>::value_type.
Definition view.hpp:277
pointer operator->() const
Access the current element.
Definition view.hpp:313
value_type reference
Provide std::iterator_traits<T>::reference.
Definition view.hpp:292
std::ptrdiff_t difference_type
Provide std::iterator_traits<T>::difference_type.
Definition view.hpp:282
value_type const * pointer
Provide std::iterator_traits<T>::pointer.
Definition view.hpp:287
reference operator*() const
Return the current element.
Definition view.hpp:304
std::input_iterator_tag iterator_category
Provide std::iterator_traits<T>::iterator_category.
Definition view.hpp:272
view()
Initialize as an empty view.
const_iterator iterator
Equivalent to const_iterator.
Definition view.hpp:59
friend bool operator!=(view lhs, view rhs)
Compare equal when the BSON bytes represented by lhs and rhs compare equal.
Definition view.hpp:207
friend bool operator==(view lhs, view rhs)
Compare equal when the BSON bytes represented by lhs and rhs compare equal.
Definition view.hpp:199
errc
Errors codes which may be returned by bsoncxx::v1::document::view.
Definition view.hpp:216
@ invalid_length
Length is invalid.
Definition view.hpp:218
@ zero
Zero.
Definition view.hpp:217
@ invalid_data
Data is invalid.
Definition view.hpp:219
v1::element::view operator[](v1::stdx::string_view key) const
Return the first element within the represented BSON document whose key compares equal to key.
Definition view.hpp:375
bool empty() const
Return true when the BSON bytes represents an empty view:
Definition view.hpp:132
std::size_t length() const
Return the length of the BSON bytes being represented.
Definition view.hpp:119
friend std::error_code make_error_code(errc v)
Support implicit conversion to std::error_code.
Definition view.hpp:230
view(std::uint8_t const *data, std::size_t length)
Equivalent to view(std::uint8_t const* data), but validates the embedded length against length.
const_iterator begin() const
Return a const iterator to the beginning of the range of BSON elements within this view.
Definition view.hpp:367
std::uint8_t const * data() const
Return a pointer to the BSON bytes being represented.
Definition view.hpp:104
const_iterator cend() const
Return a const iterator to the end of the range of BSON elements within this view.
Definition view.hpp:363
const_iterator end() const
Return a const iterator to the end of the range of BSON elements within this view.
Definition view.hpp:371
std::size_t size() const
Return the length of the BSON bytes being represented.
Definition view.hpp:253
const_iterator find(v1::stdx::string_view key) const
Return a const iterator to the element within the represented BSON document whose key compares equal ...
static std::error_category const & error_category()
The error category for bsoncxx::v1::document::view::errc.
const_iterator cbegin() const
Return a const iterator to the beginning of the range of BSON elements within this view.
A non-owning, read-only BSON element.
Definition view.hpp:82
std::uint8_t const * raw() const
Return the "raw" component of the underlying BSON bytes.
std::uint32_t offset() const
Return the "offset" component of the underlying BSON bytes.
A polyfill for std::string_view.
Definition string_view.hpp:412
Declares entities representing a BSON document.
Declares entities representing a BSON element.
Declares entities whose ABI stability is guaranteed for documented symbols.
The top-level namespace within which all bsoncxx library entities are declared.
Declares bsoncxx::v1::document::view.
Provides bsoncxx::v1::element::view.
Provides std::string_view-related polyfills for library API usage.