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/document/value-fwd.hpp> // IWYU pragma: export
18
19//
20
22#include <bsoncxx/v1/document/value.hpp> // IWYU pragma: export
24
25#include <cstdint>
26#include <cstdlib>
27#include <memory>
28#include <type_traits>
29#include <utility>
30
34#include <bsoncxx/stdx/type_traits.hpp> // IWYU pragma: keep: backward compatibity, to be removed.
35
37
38namespace bsoncxx {
39namespace v_noabi {
40namespace document {
41
48class value {
49 private:
51 std::size_t _length;
52
53 public:
57 using deleter_type = void(BSONCXX_ABI_CDECL*)(std::uint8_t*);
58
62 using unique_ptr_type = std::unique_ptr<std::uint8_t[], deleter_type>;
63
66
69
70 ~value() = default;
71
72 value(value&&) = default;
73 value& operator=(value&&) = default;
74
75 value(value const& other) : value{other.view()} {}
76
77 value& operator=(value const& other) {
78 *this = value{other.view()};
79 return *this;
80 }
81
99 value(std::uint8_t* data, std::size_t length, deleter_type deleter)
100 : _value{data, std::move(deleter)}, _length{length} {}
101
115 value(unique_ptr_type ptr, std::size_t length) : _value{std::move(ptr)}, _length{length} {}
116
126
138 template <typename T, detail::requires_not_t<int, std::is_same<T, array::view>> = 0>
139 explicit value(T const& t) : value({}) {
140 to_bson(t, *this);
141 }
142
153 template <typename T>
154 value& operator=(T const& t) {
155 *this = value{t};
156 return *this;
157 }
158
170 explicit operator v1::document::value() const& {
171 return _value;
172 }
173
185 explicit operator v1::document::value() && {
186 _length = 0u;
187 return std::move(_value);
188 }
189
194 return this->view().cbegin();
195 }
196
201 return this->view().cend();
202 }
203
208 return this->view().begin();
209 }
210
215 return this->view().end();
216 }
217
220 return const_iterator{*_value.find(key)};
221 }
222
234 return _value.operator[](key);
235 }
236
242 std::uint8_t const* data() const {
243 return _value.data();
244 }
245
254 std::size_t size() const {
255 return _length; // Do NOT use _value.size().
256 }
257
259 std::size_t length() const {
260 return _length; // Do NOT use _value.length().
261 }
262
269 bool empty() const {
270 return _length == 5; // Do NOT use _value.empty().
271 }
272
276 v_noabi::document::view view() const noexcept {
277 return {_value.data(), _length}; // Do NOT use _value.view().
278 }
279
285 /* explicit(false) */ operator v_noabi::document::view() const noexcept {
286 return this->view();
287 }
288
296 template <typename T>
297 T get() {
298 T temp{};
299 from_bson(temp, this->view());
300 return temp;
301 }
302
312 template <typename T>
313 void get(T& t) {
314 from_bson(t, this->view());
315 }
316
327 auto ptr = _value.release();
328
329 // Invariant: the underlying deleter type MUST be `deleter_type`.
330 auto const deleter_ptr = ptr.get_deleter().target<deleter_type>();
331
332 // Invariant: `ptr` implies `deleter_ptr`, but not the reverse.
333 return {ptr.release(), deleter_ptr ? *deleter_ptr : nullptr};
334 }
335
341 _value.reset(to_v1(view));
342 }
343};
344
349
351inline bool operator==(value const& lhs, value const& rhs) {
352 return (lhs.view() == rhs.view());
353}
354
356inline bool operator!=(value const& lhs, value const& rhs) {
357 return !(lhs == rhs);
358}
359
362
363} // namespace document
364} // namespace v_noabi
365} // namespace bsoncxx
366
367namespace bsoncxx {
368namespace v_noabi {
369
376
381
388
389} // namespace v_noabi
390} // namespace bsoncxx
391
392namespace bsoncxx {
393namespace document {
394
395using v_noabi::document::operator==;
396using v_noabi::document::operator!=;
397
398} // namespace document
399} // namespace bsoncxx
400
402
#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 v_noabi macro guard postlude header.
The bsoncxx v_noabi macro guard prelude header.
A BSON document.
Definition value.hpp:46
v1::document::view view() const
Return a view of the BSON bytes as a document.
Definition value.hpp:377
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
A read-only BSON document that owns its underlying buffer.
Definition value.hpp:48
void( *)(std::uint8_t *) deleter_type
The type of the deleter used to free the underlying BSON binary data.
Definition value.hpp:57
std::uint8_t const * data() const
Access the raw bytes of the underlying document.
Definition value.hpp:242
T get()
Constructs an object of type T from this document object. This method uses argument-dependent lookup ...
Definition value.hpp:297
std::size_t length() const
Gets the length of the underlying buffer.
Definition value.hpp:259
value(unique_ptr_type ptr, std::size_t length)
Constructs a value from a std::unique_ptr to a buffer. The ownership of the buffer is transferred to ...
Definition value.hpp:115
bool operator==(value const &lhs, value const &rhs)
Compares two document values for (in)-equality.
Definition value.hpp:351
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,...
Definition value.hpp:219
v_noabi::document::view view() const noexcept
Get a view over the document owned by this value.
Definition value.hpp:276
const_iterator cbegin() const
Definition value.hpp:193
bool empty() const
Return true when this->length() == 5.
Definition value.hpp:269
const_iterator cend() const
Definition value.hpp:200
unique_ptr_type release()
Transfer ownership of the underlying buffer to the caller.
Definition value.hpp:326
void get(T &t)
Constructs an object of type T from this document object. This method uses argument-dependent lookup ...
Definition value.hpp:313
void reset(v_noabi::document::view view)
Replace the formerly-owned buffer with the new view. This will make a copy of the passed-in view.
Definition value.hpp:340
std::size_t size() const
Gets the length of the underlying buffer.
Definition value.hpp:254
std::unique_ptr< std::uint8_t[], deleter_type > unique_ptr_type
The type of the unique pointer used to manage the underlying BSON binary data.
Definition value.hpp:62
const_iterator begin() const
Definition value.hpp:207
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 value.hpp:233
value(std::uint8_t *data, std::size_t length, deleter_type deleter)
Constructs a value from a buffer.
Definition value.hpp:99
bool operator!=(value const &lhs, value const &rhs)
Compares two document values for (in)-equality.
Definition value.hpp:356
v_noabi::document::view::const_iterator const_iterator
A const iterator over the contents of a document view.
Definition value.hpp:65
const_iterator end() const
Definition value.hpp:214
value & operator=(T const &t)
Assignment used for serialization of user objects. This uses argument-dependent lookup to find the fu...
Definition value.hpp:154
const_iterator iterator
Equivalent to const_iterator.
Definition value.hpp:68
A const iterator over the contents of a document view.
Definition view.hpp:205
A read-only, non-owning view of a BSON document.
Definition view.hpp:40
const_iterator cend() const
Definition view.hpp:260
const_iterator begin() const
Definition view.hpp:264
const_iterator end() const
Definition view.hpp:268
const_iterator cbegin() const
Provides bsoncxx::v_noabi::document::element.
Declares entities representing a BSON document.
Declares entities representing a BSON array.
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.
For internal use only!
Provides bsoncxx::v1::document::value.
Provides std::string_view-related polyfills for library API usage.
Provides bsoncxx::v_noabi::array::view.
Declares bsoncxx::v_noabi::document::value.
Provides bsoncxx::v_noabi::document::view.
For internal use only!