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/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
54class view {
55 public:
56 class const_iterator;
57
62
63 private:
64 enum : std::size_t { _empty_length = 5u };
65
66 std::uint8_t const* _data;
67
68 template <detail::endian e = detail::endian::native>
69 std::int32_t raw_size() const;
70
71 public:
81
89 explicit view(std::uint8_t const* data) : _data{data} {}
90
101 BSONCXX_ABI_EXPORT view(std::uint8_t const* data, std::size_t length);
102
106 std::uint8_t const* data() const {
107 return _data;
108 }
109
118 std::size_t size() const;
119
121 std::size_t length() const {
122 return this->size();
123 }
124
134 bool empty() const {
135 return this->size() == _empty_length && _data[4] == 0u;
136 }
137
143 explicit operator bool() const {
144 return this->size() >= _empty_length;
145 }
146
156
160 const_iterator cend() const;
161
163 const_iterator begin() const;
164
166 const_iterator end() const;
167
180
193
201 friend bool operator==(view lhs, view rhs) {
202 if (!lhs != !rhs) {
203 return false;
204 }
205
206 return !lhs || (lhs.length() == rhs.length() && std::memcmp(lhs.data(), rhs.data(), lhs.length()) == 0);
207 }
208
209 friend bool operator!=(view lhs, view rhs) {
210 return !(lhs == rhs);
211 }
212
214
225
231 static BSONCXX_ABI_EXPORT_CDECL(std::error_category const&) error_category();
232
238 friend std::error_code make_error_code(errc v) {
239 return {static_cast<int>(v), error_category()};
240 }
241};
242
243template <>
244inline std::int32_t view::raw_size<detail::endian::little>() const {
245 std::int32_t res;
246 std::memcpy(&res, _data, sizeof(res));
247 return res;
248}
249
250template <>
251inline std::int32_t view::raw_size<detail::endian::big>() const {
252 std::int32_t res;
253 auto const bytes = reinterpret_cast<unsigned char*>(&res);
254 bytes[0] = _data[3];
255 bytes[1] = _data[2];
256 bytes[2] = _data[1];
257 bytes[3] = _data[0];
258 return res;
259}
260
261inline std::size_t view::size() const {
262 return _data ? static_cast<std::uint32_t>(this->raw_size()) : 0u;
263}
264
275 private:
276 v1::element::view _element;
277
278 public:
282 using iterator_category = std::input_iterator_tag;
283
288
292 using difference_type = std::ptrdiff_t;
293
297 using pointer = value_type const*;
298
303
307 const_iterator() = default;
308
315 return _element;
316 }
317
324 return &_element;
325 }
326
333
340 const_iterator tmp = *this;
341 this->operator++();
342 return tmp;
343 }
344
351 friend bool operator==(const_iterator const& lhs, const_iterator const& rhs) {
352 if (!lhs._element != !rhs._element) {
353 return false;
354 }
355
356 return !lhs._element ||
357 (lhs._element.raw() == rhs._element.raw() && lhs._element.offset() == rhs._element.offset());
358 }
359
363 friend bool operator!=(const_iterator const& lhs, const_iterator const& rhs) {
364 return !(lhs == rhs);
365 }
366
367 class internal;
368
369 private:
371};
372
373inline view::const_iterator view::cend() const {
374 return {};
375}
376
378 return this->cbegin();
379}
380
382 return this->cend();
383}
384
386 return *(this->find(key));
387}
388
389} // namespace document
390} // namespace v1
391} // namespace bsoncxx
392
393template <>
394struct std::is_error_code_enum<bsoncxx::v1::document::view::errc> : true_type {};
395
397
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:274
const_iterator operator++(int)
Post-increment this iterator.
Definition view.hpp:339
friend bool operator!=(const_iterator const &lhs, const_iterator const &rhs)
Equivalent to !(lhs == rhs).
Definition view.hpp:363
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:351
const_iterator & operator++()
Pre-increment this iterator.
v1::element::view value_type
Provide std::iterator_traits<T>::value_type.
Definition view.hpp:287
pointer operator->() const
Access the current element.
Definition view.hpp:323
value_type reference
Provide std::iterator_traits<T>::reference.
Definition view.hpp:302
std::ptrdiff_t difference_type
Provide std::iterator_traits<T>::difference_type.
Definition view.hpp:292
value_type const * pointer
Provide std::iterator_traits<T>::pointer.
Definition view.hpp:297
reference operator*() const
Return the current element.
Definition view.hpp:314
std::input_iterator_tag iterator_category
Provide std::iterator_traits<T>::iterator_category.
Definition view.hpp:282
view()
Initialize as an empty view.
const_iterator iterator
Equivalent to const_iterator.
Definition view.hpp:61
friend bool operator!=(view lhs, view rhs)
Compare equal when the BSON bytes represented by lhs and rhs compare equal.
Definition view.hpp:209
friend bool operator==(view lhs, view rhs)
Compare equal when the BSON bytes represented by lhs and rhs compare equal.
Definition view.hpp:201
errc
Errors codes which may be returned by bsoncxx::v1::document::view.
Definition view.hpp:220
@ invalid_length
Length is invalid.
Definition view.hpp:222
@ zero
Zero.
Definition view.hpp:221
@ invalid_data
Data is invalid.
Definition view.hpp:223
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:385
bool empty() const
Return true when the BSON bytes represents an empty view:
Definition view.hpp:134
std::size_t length() const
Return the length of the BSON bytes being represented.
Definition view.hpp:121
friend std::error_code make_error_code(errc v)
Support implicit conversion to std::error_code.
Definition view.hpp:238
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:377
std::uint8_t const * data() const
Return a pointer to the BSON bytes being represented.
Definition view.hpp:106
const_iterator cend() const
Return a const iterator to the end of the range of BSON elements within this view.
Definition view.hpp:373
const_iterator end() const
Return a const iterator to the end of the range of BSON elements within this view.
Definition view.hpp:381
std::size_t size() const
Return the length of the BSON bytes being represented.
Definition view.hpp:261
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:84
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.