MongoDB C++ Driver 4.4.0
Loading...
Searching...
No Matches
value.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/value-fwd.hpp> // IWYU pragma: export
18
19//
20
22
25#include <bsoncxx/v1/document/view.hpp> // IWYU pragma: export
26#include <bsoncxx/v1/element/view.hpp> // IWYU pragma: export
28
29#include <cstddef>
30#include <cstdint>
31#include <cstring>
32#include <functional>
33#include <memory>
34#include <type_traits>
35#include <utility>
36
37namespace bsoncxx {
38namespace v1 {
39namespace document {
40
44class value {
45 public:
62 using deleter_type = std::function<void BSONCXX_ABI_CDECL(std::uint8_t*)>;
63
67 using default_deleter_type = std::default_delete<std::uint8_t[]>;
68
72 using noop_deleter_type = void(BSONCXX_ABI_CDECL*)(std::uint8_t*);
73
77 static BSONCXX_ABI_EXPORT_CDECL(void) noop_deleter(std::uint8_t*);
78
82 using unique_ptr_type = std::unique_ptr<std::uint8_t[], deleter_type>;
83
84 private:
85 unique_ptr_type _data;
86
87 template <typename T>
88 using is_value = detail::is_alike<T, value>;
89
90 template <typename T>
91 using to_bson_expr = decltype(to_bson(std::declval<T const&>(), std::declval<value&>()));
92
93 template <typename T>
94 struct has_to_bson : detail::conjunction<detail::negation<is_value<T>>, detail::is_detected<to_bson_expr, T>> {};
95
96 template <typename T>
97 using from_bson_expr = decltype(from_bson(std::declval<T&>(), std::declval<v1::document::view>()));
98
99 template <typename T>
100 struct has_from_bson : detail::conjunction<detail::negation<is_value<T>>, detail::is_detected<from_bson_expr, T>> {
101 };
102
103 template <typename T>
104 struct is_valid_deleter
105 : detail::disjunction<
106 // std::function<T> may not be nothrow move constructible across all pre-C++20 implementations.
107 // Deliberately allow an exemption that assumes it is de facto nothrow move constructible.
108 std::is_same<T, deleter_type>,
109 // Same requirements for D in std::unique_ptr<T, D> but without (nothrow) move assignability which is not
110 // required for T in std::function<T>.
111 detail::conjunction<
112 std::is_convertible<T, deleter_type>,
113 std::is_nothrow_destructible<T>,
114 std::is_nothrow_move_constructible<T>>> {};
115
116 public:
119
122
126 ~value() = default;
127
135 value(value&& other) noexcept = default;
136
144 value& operator=(value&& other) noexcept = default;
145
152 value(value const& other) : value{other.view()} {}
153
160 value& operator=(value const& other) {
161 *this = value{other.view()};
162 return *this;
163 }
164
173 value() : _data{const_cast<std::uint8_t*>(v1::document::view{}.data()), &noop_deleter} {}
174
186 template <typename Deleter, detail::enable_if_t<is_valid_deleter<Deleter>::value>* = nullptr>
187 value(std::uint8_t* data, Deleter deleter) : _data{data, std::move(deleter)} {}
188
200 template <typename Deleter, detail::enable_if_t<is_valid_deleter<Deleter>::value>* = nullptr>
201 value(std::uint8_t* data, std::size_t length, Deleter deleter) : value{data, std::move(deleter)} {
202 (void)v1::document::view{data, length}; // May throw.
203 }
204
212 explicit value(std::uint8_t* data) : value{data, default_deleter_type{}} {}
213
224 explicit value(std::uint8_t* data, std::size_t length) : value{data} {
225 (void)v1::document::view{data, length}; // May throw.
226 }
227
235 explicit value(unique_ptr_type ptr) : _data{std::move(ptr)} {}
236
247 explicit value(unique_ptr_type ptr, std::size_t length) : value{std::move(ptr)} {
248 (void)v1::document::view{_data.get(), length}; // May throw.
249 }
250
263 if (!view) {
264 return;
265 }
266
268
269 if (view.data() == empty.data()) {
270 _data = unique_ptr_type{const_cast<std::uint8_t*>(empty.data()), &noop_deleter};
271 } else {
272 _data = unique_ptr_type{new std::uint8_t[view.size()], default_deleter_type{}};
273 std::memcpy(_data.get(), view.data(), view.size());
274 }
275 }
276
284 template <typename T, detail::enable_if_t<has_to_bson<T>::value>* = nullptr>
285 explicit value(T const& v) : value{} {
286 to_bson(v, *this); // ADL.
287 }
288
293 this->reset(view);
294 return *this;
295 }
296
304 template <typename T, detail::enable_if_t<has_to_bson<T>::value>* = nullptr>
305 value& operator=(T const& v) {
306 *this = value{v};
307 return *this;
308 }
309
317 template <typename T, detail::enable_if_t<has_from_bson<T>::value>* = nullptr>
318 void get(T& v) const {
319 from_bson(v, this->view()); // ADL.
320 }
321
330 template <
331 typename T,
332 detail::enable_if_t<detail::conjunction<std::is_default_constructible<T>, has_from_bson<T>>::value>* = nullptr>
333 T get() const {
334 T res;
335 this->get(res);
336 return res;
337 }
338
342 deleter_type const& get_deleter() const {
343 return _data.get_deleter();
344 }
345
350 return std::move(_data);
351 }
352
356 void reset(value v) {
357 *this = std::move(v);
358 }
359
369 *this = value{v};
370 }
371
376 return v1::document::view{_data.get()};
377 }
378
382 /* explicit(false) */ operator v1::document::view() const {
383 return this->view();
384 }
385
388 return this->view().cbegin();
389 }
390
393 return this->view().cend();
394 }
395
398 return this->view().begin();
399 }
400
403 return this->view().end();
404 }
405
408 return this->view().find(key);
409 }
410
413 return this->view()[key];
414 }
415
417 std::uint8_t const* data() const {
418 return _data.get();
419 }
420
422 std::size_t size() const {
423 return this->view().size();
424 }
425
427 std::size_t length() const {
428 return this->view().length();
429 }
430
432 bool empty() const {
433 return this->view().empty();
434 }
435
437 explicit operator bool() const {
438 return this->view().operator bool();
439 }
440
442 friend bool operator==(value const& lhs, value const& rhs) {
443 return lhs.view() == rhs.view();
444 }
445
447 friend bool operator!=(value const& lhs, value const& rhs) {
448 return !(lhs == rhs);
449 }
450};
451
452} // namespace document
453} // namespace v1
454} // namespace bsoncxx
455
457
Provides macros to control the set of symbols exported in the ABI.
#define BSONCXX_ABI_CDECL
Expands to __cdecl when built with MSVC on Windows.
Definition export.hpp:49
#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 BSON document.
Definition value.hpp:44
static void noop_deleter(std::uint8_t *)
The deleter used to avoid freeing preallocated storage representing an empty BSON document.
v1::document::view view() const
Return a view of the BSON bytes as a document.
Definition value.hpp:375
void reset(value v)
Replace the underlying BSON bytes with v.
Definition value.hpp:356
value(unique_ptr_type ptr)
Initialize as owning ptr.
Definition value.hpp:235
const_iterator cend() const
Return a const iterator to the end of the range of BSON elements within this view.
Definition value.hpp:392
value(unique_ptr_type ptr, std::size_t length)
Equivalent to value(unique_ptr_type ptr), but validates the embedded length against length.
Definition value.hpp:247
void reset(v1::document::view v)
Replace the underlying BSON bytes with a copy of v.
Definition value.hpp:368
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 ...
Definition value.hpp:407
friend bool operator==(value const &lhs, value const &rhs)
Compare equal when the BSON bytes represented by lhs and rhs compare equal.
Definition value.hpp:442
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 value.hpp:412
value & operator=(T const &v)
Equivalent to *this = value{v}.
Definition value.hpp:305
value(v1::document::view view)
Initialize with a copy of the BSON bytes referenced by view.
Definition value.hpp:262
void get(T &v) const
Equivalent to from_bson(v, this->view()).
Definition value.hpp:318
deleter_type const & get_deleter() const
Return the current deleter.
Definition value.hpp:342
bool empty() const
Return true when the BSON bytes represents an empty view:
Definition value.hpp:432
std::unique_ptr< std::uint8_t[], deleter_type > unique_ptr_type
The type of the unique pointer used to manage the underlying BSON bytes.
Definition value.hpp:82
std::function< void(std::uint8_t *)> deleter_type
The type of the deleter used to free the underlying BSON bytes.
Definition value.hpp:62
const_iterator begin() const
Return a const iterator to the beginning of the range of BSON elements within this view.
Definition value.hpp:397
void( *)(std::uint8_t *) noop_deleter_type
The type of a pointer to noop_deleter.
Definition value.hpp:72
value(value &&other) noexcept=default
Move construction.
value & operator=(value const &other)
Copy assignment.
Definition value.hpp:160
value(T const &v)
Equivalent to to_bson(v, *this); after default-initialization.
Definition value.hpp:285
std::size_t length() const
Return the length of the BSON bytes being represented.
Definition value.hpp:427
std::default_delete< std::uint8_t[]> default_deleter_type
The deleter used to free copied BSON bytes when a user-provided deleter is not specified.
Definition value.hpp:67
value & operator=(value &&other) noexcept=default
Move assignment.
value(std::uint8_t *data, std::size_t length)
Equivalent to value(std::uint8_t* data), but validates the embedded length against length.
Definition value.hpp:224
const_iterator cbegin() const
Return a const iterator to the beginning of the range of BSON elements within this view.
Definition value.hpp:387
value(std::uint8_t *data, Deleter deleter)
Initialize as owning data which will be freed with deleter.
Definition value.hpp:187
~value()=default
Destroy this object.
const_iterator iterator
Equivalent to const_iterator.
Definition value.hpp:121
std::uint8_t const * data() const
Return a pointer to the BSON bytes being represented.
Definition value.hpp:417
value & operator=(v1::document::view view)
Equivalent to this->reset(view).
Definition value.hpp:292
unique_ptr_type release()
Release ownership of the underlying BSON bytes.
Definition value.hpp:349
value(value const &other)
Copy construction.
Definition value.hpp:152
T get() const
Equivalent to T value; this->get(value); return value;.
Definition value.hpp:333
friend bool operator!=(value const &lhs, value const &rhs)
Compare equal when the BSON bytes represented by lhs and rhs compare equal.
Definition value.hpp:447
std::size_t size() const
Return the length of the BSON bytes being represented.
Definition value.hpp:422
value()
Initialize as an empty document (or array).
Definition value.hpp:173
v1::document::view::const_iterator const_iterator
A const iterator over the elements of a view.
Definition value.hpp:118
value(std::uint8_t *data, std::size_t length, Deleter deleter)
Equivalent to value(std::uint8_t* data, Deleter deleter), but validates the embedded length against l...
Definition value.hpp:201
const_iterator end() const
Return a const iterator to the end of the range of BSON elements within this view.
Definition value.hpp:402
value(std::uint8_t *data)
Initialize as owning data which will be freed with default_deleter_type.
Definition value.hpp:212
A const iterator over the elements of a view.
Definition view.hpp:264
A non-owning, read-only BSON document.
Definition view.hpp:52
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
const_iterator begin() const
Return a const iterator to the beginning of the range of BSON elements within this view.
Definition view.hpp:367
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 ...
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
A polyfill for std::string_view.
Definition string_view.hpp:412
Declares entities representing a BSON document.
Declares entities whose ABI stability is guaranteed for documented symbols.
The top-level namespace within which all bsoncxx library entities are declared.
For internal use only!
Declares bsoncxx::v1::document::value.
Provides bsoncxx::v1::document::view.
Provides bsoncxx::v1::element::view.
Provides std::string_view-related polyfills for library API usage.