MongoDB C++ Driver 4.2.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
46class value {
47 public:
64 using deleter_type = std::function<void BSONCXX_ABI_CDECL(std::uint8_t*)>;
65
69 using default_deleter_type = std::default_delete<std::uint8_t[]>;
70
74 using noop_deleter_type = void(BSONCXX_ABI_CDECL*)(std::uint8_t*);
75
79 static BSONCXX_ABI_EXPORT_CDECL(void) noop_deleter(std::uint8_t*);
80
84 using unique_ptr_type = std::unique_ptr<std::uint8_t[], deleter_type>;
85
86 private:
87 unique_ptr_type _data;
88
89 template <typename T>
90 using is_value = detail::is_alike<T, value>;
91
92 template <typename T>
93 using to_bson_expr = decltype(to_bson(std::declval<T const&>(), std::declval<value&>()));
94
95 template <typename T>
96 struct has_to_bson : detail::conjunction<detail::negation<is_value<T>>, detail::is_detected<to_bson_expr, T>> {};
97
98 template <typename T>
99 using from_bson_expr = decltype(from_bson(std::declval<T&>(), std::declval<v1::document::view>()));
100
101 template <typename T>
102 struct has_from_bson : detail::conjunction<detail::negation<is_value<T>>, detail::is_detected<from_bson_expr, T>> {
103 };
104
105 template <typename T>
106 struct is_valid_deleter
107 : detail::disjunction<
108 // std::function<T> may not be nothrow move constructible across all pre-C++20 implementations.
109 // Deliberately allow an exemption that assumes it is de facto nothrow move constructible.
110 std::is_same<T, deleter_type>,
111 // Same requirements for D in std::unique_ptr<T, D> but without (nothrow) move assignability which is not
112 // required for T in std::function<T>.
113 detail::conjunction<
114 std::is_convertible<T, deleter_type>,
115 std::is_nothrow_destructible<T>,
116 std::is_nothrow_move_constructible<T>>> {};
117
118 public:
121
124
128 ~value() = default;
129
137 value(value&& other) noexcept = default;
138
146 value& operator=(value&& other) noexcept = default;
147
154 value(value const& other) : value{other.view()} {}
155
162 value& operator=(value const& other) {
163 *this = value{other.view()};
164 return *this;
165 }
166
175 value() : _data{const_cast<std::uint8_t*>(v1::document::view{}.data()), &noop_deleter} {}
176
188 template <typename Deleter, detail::enable_if_t<is_valid_deleter<Deleter>::value>* = nullptr>
189 value(std::uint8_t* data, Deleter deleter) : _data{data, std::move(deleter)} {}
190
202 template <typename Deleter, detail::enable_if_t<is_valid_deleter<Deleter>::value>* = nullptr>
203 value(std::uint8_t* data, std::size_t length, Deleter deleter) : value{data, std::move(deleter)} {
204 (void)v1::document::view{data, length}; // May throw.
205 }
206
214 explicit value(std::uint8_t* data) : value{data, default_deleter_type{}} {}
215
226 explicit value(std::uint8_t* data, std::size_t length) : value{data} {
227 (void)v1::document::view{data, length}; // May throw.
228 }
229
237 explicit value(unique_ptr_type ptr) : _data{std::move(ptr)} {}
238
249 explicit value(unique_ptr_type ptr, std::size_t length) : value{std::move(ptr)} {
250 (void)v1::document::view{_data.get(), length}; // May throw.
251 }
252
265 if (!view) {
266 return;
267 }
268
270
271 if (view.data() == empty.data()) {
272 _data = unique_ptr_type{const_cast<std::uint8_t*>(empty.data()), &noop_deleter};
273 } else {
274 _data = unique_ptr_type{new std::uint8_t[view.size()], default_deleter_type{}};
275 std::memcpy(_data.get(), view.data(), view.size());
276 }
277 }
278
286 template <typename T, detail::enable_if_t<has_to_bson<T>::value>* = nullptr>
287 explicit value(T const& v) : value{} {
288 to_bson(v, *this); // ADL.
289 }
290
295 this->reset(view);
296 return *this;
297 }
298
306 template <typename T, detail::enable_if_t<has_to_bson<T>::value>* = nullptr>
307 value& operator=(T const& v) {
308 *this = value{v};
309 return *this;
310 }
311
319 template <typename T, detail::enable_if_t<has_from_bson<T>::value>* = nullptr>
320 void get(T& v) const {
321 from_bson(v, this->view()); // ADL.
322 }
323
332 template <
333 typename T,
334 detail::enable_if_t<detail::conjunction<std::is_default_constructible<T>, has_from_bson<T>>::value>* = nullptr>
335 T get() const {
336 T res;
337 this->get(res);
338 return res;
339 }
340
344 deleter_type const& get_deleter() const {
345 return _data.get_deleter();
346 }
347
352 return std::move(_data);
353 }
354
358 void reset(value v) {
359 *this = std::move(v);
360 }
361
371 *this = value{v};
372 }
373
378 return v1::document::view{_data.get()};
379 }
380
384 /* explicit(false) */ operator v1::document::view() const {
385 return this->view();
386 }
387
390 return this->view().cbegin();
391 }
392
395 return this->view().cend();
396 }
397
400 return this->view().begin();
401 }
402
405 return this->view().end();
406 }
407
410 return this->view().find(key);
411 }
412
415 return this->view()[key];
416 }
417
419 std::uint8_t const* data() const {
420 return _data.get();
421 }
422
424 std::size_t size() const {
425 return this->view().size();
426 }
427
429 std::size_t length() const {
430 return this->view().length();
431 }
432
434 bool empty() const {
435 return this->view().empty();
436 }
437
439 explicit operator bool() const {
440 return this->view().operator bool();
441 }
442
444 friend bool operator==(value const& lhs, value const& rhs) {
445 return lhs.view() == rhs.view();
446 }
447
449 friend bool operator!=(value const& lhs, value const& rhs) {
450 return !(lhs == rhs);
451 }
452};
453
454} // namespace document
455} // namespace v1
456} // namespace bsoncxx
457
459
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:46
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:377
void reset(value v)
Replace the underlying BSON bytes with v.
Definition value.hpp:358
value(unique_ptr_type ptr)
Initialize as owning ptr.
Definition value.hpp:237
const_iterator cend() const
Return a const iterator to the end of the range of BSON elements within this view.
Definition value.hpp:394
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:249
void reset(v1::document::view v)
Replace the underlying BSON bytes with a copy of v.
Definition value.hpp:370
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:409
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:444
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:414
value & operator=(T const &v)
Equivalent to *this = value{v}.
Definition value.hpp:307
value(v1::document::view view)
Initialize with a copy of the BSON bytes referenced by view.
Definition value.hpp:264
void get(T &v) const
Equivalent to from_bson(v, this->view()).
Definition value.hpp:320
deleter_type const & get_deleter() const
Return the current deleter.
Definition value.hpp:344
bool empty() const
Return true when the BSON bytes represents an empty view:
Definition value.hpp:434
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:84
std::function< void(std::uint8_t *)> deleter_type
The type of the deleter used to free the underlying BSON bytes.
Definition value.hpp:64
const_iterator begin() const
Return a const iterator to the beginning of the range of BSON elements within this view.
Definition value.hpp:399
void( *)(std::uint8_t *) noop_deleter_type
The type of a pointer to noop_deleter.
Definition value.hpp:74
value(value &&other) noexcept=default
Move construction.
value & operator=(value const &other)
Copy assignment.
Definition value.hpp:162
value(T const &v)
Equivalent to to_bson(v, *this); after default-initialization.
Definition value.hpp:287
std::size_t length() const
Return the length of the BSON bytes being represented.
Definition value.hpp:429
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:69
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:226
const_iterator cbegin() const
Return a const iterator to the beginning of the range of BSON elements within this view.
Definition value.hpp:389
value(std::uint8_t *data, Deleter deleter)
Initialize as owning data which will be freed with deleter.
Definition value.hpp:189
~value()=default
Destroy this object.
const_iterator iterator
Equivalent to const_iterator.
Definition value.hpp:123
std::uint8_t const * data() const
Return a pointer to the BSON bytes being represented.
Definition value.hpp:419
value & operator=(v1::document::view view)
Equivalent to this->reset(view).
Definition value.hpp:294
unique_ptr_type release()
Release ownership of the underlying BSON bytes.
Definition value.hpp:351
value(value const &other)
Copy construction.
Definition value.hpp:154
T get() const
Equivalent to T value; this->get(value); return value;.
Definition value.hpp:335
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:449
std::size_t size() const
Return the length of the BSON bytes being represented.
Definition value.hpp:424
value()
Initialize as an empty document (or array).
Definition value.hpp:175
v1::document::view::const_iterator const_iterator
A const iterator over the elements of a view.
Definition value.hpp:120
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:203
const_iterator end() const
Return a const iterator to the end of the range of BSON elements within this view.
Definition value.hpp:404
value(std::uint8_t *data)
Initialize as owning data which will be freed with default_deleter_type.
Definition value.hpp:214
A const iterator over the elements of a view.
Definition view.hpp:274
A non-owning, read-only BSON document.
Definition view.hpp:54
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
const_iterator begin() const
Return a const iterator to the beginning of the range of BSON elements within this view.
Definition view.hpp:377
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 ...
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
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.