MongoDB C++ Driver current
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/types/bson_value/value-fwd.hpp> // IWYU pragma: export
18
19//
20
22#include <bsoncxx/v1/types/value.hpp> // IWYU pragma: export
23
24#include <iostream> // IWYU pragma: keep: backward compatibility, to be removed.
25#include <memory> // IWYU pragma: keep: backward compatibility, to be removed.
26#include <string>
27#include <utility>
28#include <vector>
29
30#include <bsoncxx/document/element-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
31
32#include <bsoncxx/array/view_or_value.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
35
37
38namespace bsoncxx {
39namespace v_noabi {
40namespace types {
41namespace bson_value {
42
57class value {
58 private:
59 v1::types::value _value;
60
61 template <typename T>
62 using is_value = detail::is_alike<T, value>;
63
64 template <typename T>
65 using from_v1_expr = decltype(from_v1(std::declval<T>()));
66
67 template <typename T>
68 struct has_from_v1 : detail::conjunction<detail::negation<is_value<T>>, detail::is_detected<from_v1_expr, T>> {};
69
70 public:
72 value() = default;
73
77 /* explicit(false) */ value(v1::types::value v) : _value{std::move(v)} {}
78
85 template <typename T, detail::enable_if_t<has_from_v1<T>::value>* = nullptr>
86 /* explicit(false) */ value(T v) : value{from_v1(v)} {}
87
88#pragma push_macro("X")
89#undef X
90#define X(_name, _unused) \
91 /* explicit(false) */ value(v_noabi::types::b_##_name v) : _value{to_v1(v)} {}
92
100#pragma pop_macro("X")
101
105 /* explicit(false) */ value(char const* v) : _value{v} {}
106
110 /* explicit(false) */ value(std::string v) : _value{std::move(v)} {}
111
115 /* explicit(false) */ value(v1::stdx::string_view v) : _value{v} {}
116
120 /* explicit(false) */ value(std::int32_t v) : _value{v} {}
121
125 /* explicit(false) */ value(std::int64_t v) : _value{v} {}
126
130 /* explicit(false) */ value(double v) : _value{v} {}
131
135 /* explicit(false) */ value(bool v) : _value{v} {}
136
140 /* explicit(false) */ value(v_noabi::oid v) : _value{to_v1(v)} {}
141
145 /* explicit(false) */ value(v_noabi::decimal128 v) : _value{to_v1(v)} {}
146
150 /* explicit(false) */ value(std::chrono::milliseconds v) : _value{v} {}
151
155 /* explicit(false) */ value(std::nullptr_t) : _value{nullptr} {}
156
160 /* explicit(false) */ value(v_noabi::document::view v) : _value{to_v1(v)} {}
161
165 /* explicit(false) */ value(v_noabi::array::view v) : _value{to_v1(v)} {}
166
175 /* explicit(false) */
176 value(std::vector<unsigned char> v, v_noabi::binary_sub_type const sub_type = {})
177 : _value{reinterpret_cast<std::uint8_t const*>(v.data()), v.size(), to_v1(sub_type)} {}
178
189 /* explicit(false) */
190 value(std::uint8_t const* data, std::size_t size, v_noabi::binary_sub_type const sub_type = {})
191 : _value{data, size, to_v1(sub_type)} {}
192
203 /* explicit(false) */ value(v1::stdx::string_view collection, v_noabi::oid value)
204 : _value{collection, to_v1(value)} {}
205
215 : _value{code, to_v1(scope.view())} {}
216
225 /* explicit(false) */ value(v1::stdx::string_view regex, v1::stdx::string_view options)
226 : _value{v1::types::b_regex{regex, options}} {}
227
245 /* explicit(false) */ BSONCXX_ABI_EXPORT_CDECL_UNSTABLE() value(v_noabi::type const id, v1::stdx::string_view v);
246
259 /* explicit(false) */ BSONCXX_ABI_EXPORT_CDECL_UNSTABLE() value(v_noabi::type const id);
260
281 BSONCXX_ABI_EXPORT_CDECL_UNSTABLE() value(v_noabi::type const id, std::uint64_t a, std::uint64_t b);
282
286 explicit value(v_noabi::types::bson_value::view const& v) : _value{to_v1(v)} {}
287
291 explicit operator v1::types::value() && {
292 return std::move(_value);
293 }
294
298 explicit operator v1::types::value() const& {
299 return _value;
300 }
301
308 return this->view().type();
309 }
310
317 return this->view().type_id();
318 }
319
324 return _value.view();
325 }
326
330 /* explicit(false) */ operator v_noabi::types::bson_value::view() const noexcept {
331 return _value.view();
332 }
333
334#pragma push_macro("X")
335#undef X
336#define X(_name, _value) \
337 v_noabi::types::b_##_name const& get_##_name() const { \
338 return this->view().get_##_name(); \
339 }
340
351#pragma pop_macro("X")
352};
353
358
360inline bool operator==(value const& lhs, value const& rhs) {
361 return (lhs.view() == rhs.view());
362}
363
365inline bool operator!=(value const& lhs, value const& rhs) {
366 return !(lhs == rhs);
367}
368
371
376
378inline bool operator==(value const& lhs, view const& rhs) {
379 return (lhs.view() == rhs);
380}
381
383inline bool operator==(view const& lhs, value const& rhs) {
384 return (rhs == lhs);
385}
386
388inline bool operator!=(value const& lhs, view const& rhs) {
389 return !(lhs == rhs);
390}
391
393inline bool operator!=(view const& lhs, value const& rhs) {
394 return !(lhs == rhs);
395}
396
399
400} // namespace bson_value
401} // namespace types
402} // namespace v_noabi
403} // namespace bsoncxx
404
405namespace bsoncxx {
406namespace v_noabi {
407
412 return {std::move(v)};
413}
414
421
422} // namespace v_noabi
423} // namespace bsoncxx
424
425namespace bsoncxx {
426namespace types {
427namespace bson_value {
428
429using v_noabi::types::bson_value::operator==;
430using v_noabi::types::bson_value::operator!=;
431
432} // namespace bson_value
433} // namespace types
434} // namespace bsoncxx
435
437
Provides bsoncxx::v_noabi::array::view_or_value.
#define BSONCXX_ABI_EXPORT_CDECL_UNSTABLE(...)
Equivalent to BSONCXX_ABI_EXPORT_UNSTABLE with BSONCXX_ABI_CDECL.
Definition config.hpp:28
The bsoncxx v_noabi macro guard postlude header.
The bsoncxx v_noabi macro guard prelude header.
value()=default
Initialize with bsoncxx::v1::types::b_null.
A polyfill for std::string_view.
Definition string_view.hpp:412
A union of BSON type values.
Definition value.hpp:52
A read-only, non-owning view of a BSON document.
Definition view.hpp:41
Represents a MongoDB BSON Decimal128.
Definition decimal128.hpp:45
A read-only, non-owning view of a BSON document.
Definition view.hpp:40
Represents a MongoDB BSON ObjectId.
Definition oid.hpp:41
An owning variant type that represents any BSON type.
Definition value.hpp:57
value(T v)
Construct with the bsoncxx::v1 equivalent.
Definition value.hpp:86
bool operator!=(value const &lhs, view const &rhs)
Compares a value with a view for (in)equality.
Definition value.hpp:388
value(v_noabi::document::view v)
Constructs a BSON document value.
Definition value.hpp:160
value(v_noabi::decimal128 v)
Constructs a BSON Decimal128 value.
Definition value.hpp:145
value(v1::stdx::string_view v)
Constructs a BSON UTF-8 string value.
Definition value.hpp:115
bool operator==(value const &lhs, value const &rhs)
Compares values for (in)-equality.
Definition value.hpp:360
value(v_noabi::array::view v)
Constructs a BSON array value.
Definition value.hpp:165
value(v1::stdx::string_view collection, v_noabi::oid value)
Constructs a BSON DBPointer value.
Definition value.hpp:203
value(char const *v)
Constructs a BSON UTF-8 string value.
Definition value.hpp:105
bool operator!=(view const &lhs, value const &rhs)
Compares a value with a view for (in)equality.
Definition value.hpp:393
value(std::uint8_t const *data, std::size_t size, v_noabi::binary_sub_type const sub_type={})
Constructs a BSON binary data value.
Definition value.hpp:190
value(v1::types::value v)
Construct with the bsoncxx::v1 equivalent.
Definition value.hpp:77
value(double v)
Constructs a BSON double value.
Definition value.hpp:130
v_noabi::type type_id() const
Equivalent to type() const.
Definition value.hpp:316
value(std::string v)
Constructs a BSON UTF-8 string value.
Definition value.hpp:110
bool operator==(value const &lhs, view const &rhs)
Compares a value with a view for (in)equality.
Definition value.hpp:378
bool operator==(view const &lhs, value const &rhs)
Compares a value with a view for (in)equality.
Definition value.hpp:383
v_noabi::types::bson_value::view view() const noexcept
Get a view over the bson_value owned by this object.
Definition value.hpp:323
value()=default
Initialize with bsoncxx::v1::types::b_null.
value(std::chrono::milliseconds v)
Constructs a BSON date value.
Definition value.hpp:150
v_noabi::type type() const
Returns the type of the underlying BSON value stored in this object.
Definition value.hpp:307
value(std::int32_t v)
Constructs a BSON 32-bit signed integer value.
Definition value.hpp:120
value(v1::stdx::string_view code, v_noabi::document::view_or_value scope)
Constructs a BSON JavaScript code with scope value.
Definition value.hpp:214
value(bool v)
Constructs a BSON boolean value.
Definition value.hpp:135
value(std::vector< unsigned char > v, v_noabi::binary_sub_type const sub_type={})
Constructs a BSON binary data value.
Definition value.hpp:176
bool operator!=(value const &lhs, value const &rhs)
Compares values for (in)-equality.
Definition value.hpp:365
value(v1::stdx::string_view regex, v1::stdx::string_view options)
Constructs a BSON regex value with options.
Definition value.hpp:225
value(v_noabi::oid v)
Constructs a BSON ObjectId value.
Definition value.hpp:140
value(std::int64_t v)
Constructs a BSON 64-bit signed integer value.
Definition value.hpp:125
value(std::nullptr_t)
Constructs a BSON null value.
Definition value.hpp:155
A non-owning variant that can contain any BSON type.
Definition view.hpp:62
v_noabi::type type_id() const
Equivalent to type() const.
Definition view.hpp:144
v_noabi::type type() const
Returns the type of the underlying BSON value stored in this object.
Definition view.hpp:135
view() noexcept
Default constructs a bson_value::view. The resulting view will be initialized to point at a bson_valu...
Definition view.hpp:80
Declares bsoncxx::v_noabi::document::element.
Provides bsoncxx::v_noabi::document::view_or_value.
Declares entities representing any BSON value type.
Declares entities representing BSON value types.
Declares entities whose ABI stability is guaranteed for documented symbols.
v_noabi::view_or_value< v_noabi::document::view, v_noabi::document::value > view_or_value
Equivalent to v_noabi::view_or_value<v_noabi::document::view, v_noabi::document::value>.
Definition view_or_value.hpp:30
Declares C++17 standard library polyfills.
Declares entities representing any BSON value type.
Declares entities representing BSON value types.
Declares entities whose ABI stability is NOT guaranteed.
binary_sub_type
An enumeration of each BSON binary sub type.
Definition types.hpp:78
type
An enumeration of each BSON type.
Definition types.hpp:47
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:263
The top-level namespace within which all bsoncxx library entities are declared.
A BSON regex value.
Definition types.hpp:423
For internal use only!
#define BSONCXX_V1_TYPES_XMACRO(X)
X-macro over the name and value of BSON types.
Definition id-fwd.hpp:44
Provides bsoncxx::v1::types::value.
Declares bsoncxx::v_noabi::types::bson_value::value.
Provides bsoncxx::v_noabi::types::bson_value::view.