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/types/value-fwd.hpp> // IWYU pragma: export
18
19//
20
22
28
32#include <bsoncxx/v1/types/view.hpp> // IWYU pragma: export
33
34#include <array>
35#include <chrono>
36#include <cstddef>
37#include <cstdint>
38#include <string>
39#include <system_error>
40#include <type_traits>
41#include <vector>
42
43namespace bsoncxx {
44namespace v1 {
45namespace types {
46
54class value {
55 private:
56 class impl;
57
58 alignas(BSONCXX_PRIVATE_MAX_ALIGN_T) std::array<unsigned char, 32> _storage;
59
60 public:
65
73
80 BSONCXX_ABI_EXPORT_CDECL(value&) operator=(value&& other) noexcept;
81
89
96 BSONCXX_ABI_EXPORT_CDECL(value&) operator=(value const& other);
97
102
115
116#pragma push_macro("X")
117#undef X
118#define X(_name, _value) explicit BSONCXX_ABI_EXPORT_CDECL() value(v1::types::b_##_name v);
119
134#pragma pop_macro("X")
135
139 explicit value(std::nullptr_t) : value{v1::types::b_null{}} {}
140
145
149 explicit value(std::int32_t v) : value{v1::types::b_int32{v}} {}
150
154 explicit value(std::int64_t v) : value{v1::types::b_int64{v}} {}
155
159 explicit value(double v) : value{v1::types::b_double{v}} {}
160
164 explicit value(bool v) : value{v1::types::b_bool{v}} {}
165
169 explicit value(v1::oid v) : value{v1::types::b_oid{v}} {}
170
175
179 explicit value(std::chrono::milliseconds v) : value{v1::types::b_date{v}} {}
180
185
189 explicit value(v1::array::view v) : value{v1::types::b_array{v}} {}
190
195 explicit value(std::vector<std::uint8_t> const& v)
196 : value{v.data(), v.size(), v1::types::binary_subtype::k_binary} {}
197
201 explicit value(std::vector<std::uint8_t> const& v, v1::types::binary_subtype subtype)
202 : value{v.data(), v.size(), subtype} {}
203
208
213 : value{v1::types::b_codewscope{code, scope}} {}
214
219 : value{v1::types::b_regex{regex, options}} {}
220
224 explicit value(char const* v) : value{v1::stdx::string_view{v}} {}
225
229 explicit value(std::string const& v) : value{v1::stdx::string_view{v}} {}
230
235 explicit value(std::uint8_t const* data, std::size_t size)
236 : value{data, size, v1::types::binary_subtype::k_binary} {}
237
241 explicit BSONCXX_ABI_EXPORT_CDECL()
242 value(std::uint8_t const* data, std::size_t size, v1::types::binary_subtype const sub_type);
243
248
253
257 /* explicit(false) */ operator v1::types::view() const {
258 return this->view();
259 }
260
261#pragma push_macro("X")
262#undef X
263#define X(_name, _value) \
264 v1::types::b_##_name get_##_name() const { \
265 return this->view().get_##_name(); \
266 }
267
279#pragma pop_macro("X")
280
284 friend bool operator==(value const& lhs, value const& rhs) {
285 return lhs.view() == rhs.view();
286 }
287
291 friend bool operator!=(value const& lhs, value const& rhs) {
292 return !(lhs == rhs);
293 }
294
305
311 static BSONCXX_ABI_EXPORT_CDECL(std::error_category const&) error_category();
312
318 friend std::error_code make_error_code(errc v) {
319 return {static_cast<int>(v), error_category()};
320 }
321
322 class internal;
323};
324
325} // namespace types
326} // namespace v1
327} // namespace bsoncxx
328
329template <>
330struct std::is_error_code_enum<bsoncxx::v1::types::value::errc> : true_type {};
331
333
Provides macros to control the set of symbols exported in the ABI.
#define BSONCXX_ABI_EXPORT_CDECL(...)
Equivalent to BSONCXX_ABI_EXPORT with BSONCXX_ABI_CDECL.
Definition export.hpp:52
For internal use only!
The bsoncxx v1 macro guard postlude header.
The bsoncxx v1 macro guard prelude header.
A non-owning, read-only BSON array.
Definition view.hpp:49
A BSON Decimal128.
Definition decimal128.hpp:39
A non-owning, read-only BSON document.
Definition view.hpp:54
A BSON ObjectID.
Definition oid.hpp:43
A polyfill for std::string_view.
Definition string_view.hpp:412
value(std::vector< std::uint8_t > const &v, v1::types::binary_subtype subtype)
Initialize with v as a bsoncxx::v1::types::b_binary with subtype.
Definition value.hpp:201
value(std::int64_t v)
Initialize as a bsoncxx::v1::types::b_int64.
Definition value.hpp:154
v1::types::id type_id() const
Return the type of the underlying BSON type value.
value(v1::oid v)
Initialize as a bsoncxx::v1::types::b_oid.
Definition value.hpp:169
value(std::nullptr_t)
Initialize as a bsoncxx::v1::types::b_null.
Definition value.hpp:139
errc
Errors codes which may be returned by bsoncxx::v1::types::value.
Definition value.hpp:300
@ invalid_type
Requested BSON type is not supported.
Definition value.hpp:302
@ invalid_length_u32
Length is too long (exceeds UINT32_MAX).
Definition value.hpp:303
@ zero
Zero.
Definition value.hpp:301
friend bool operator==(value const &lhs, value const &rhs)
Equivalent to lhs.view() == rhs.view().
Definition value.hpp:284
value(v1::decimal128 v)
Initialize as a bsoncxx::v1::types::b_decimal128.
Definition value.hpp:174
value(bool v)
Initialize as a bsoncxx::v1::types::b_bool.
Definition value.hpp:164
value(char const *v)
Initialize as a bsoncxx::v1::types::b_string.
Definition value.hpp:224
value(v1::stdx::string_view c, v1::oid v)
Initialize as a bsoncxx::v1::types::b_dbpointer.
Definition value.hpp:207
value(v1::stdx::string_view regex, v1::stdx::string_view options)
Initialize as a bsoncxx::v1::types::b_regex.
Definition value.hpp:218
value(v1::document::view v)
Initialize as a bsoncxx::v1::types::b_document.
Definition value.hpp:184
v1::types::view view() const
Return a view of the underlying BSON type value.
value(double v)
Initialize as a bsoncxx::v1::types::b_double.
Definition value.hpp:159
value(std::int32_t v)
Initialize as a bsoncxx::v1::types::b_int32.
Definition value.hpp:149
value(v1::stdx::string_view code, v1::document::view scope)
Initialize as a bsoncxx::v1::types::b_codewscope.
Definition value.hpp:212
friend std::error_code make_error_code(errc v)
Support implicit conversion to std::error_code.
Definition value.hpp:318
value(value &&other) noexcept
Move construction.
value(v1::stdx::string_view v)
Initialize as a bsoncxx::v1::types::b_string.
Definition value.hpp:144
value(std::uint8_t const *data, std::size_t size)
Initialize as a bsoncxx::v1::types::b_binary with bsoncxx::v1::types::binary_subtype::k_binary.
Definition value.hpp:235
value(std::string const &v)
Initialize as a bsoncxx::v1::types::b_string.
Definition value.hpp:229
value(std::chrono::milliseconds v)
Initialize as a bsoncxx::v1::types::b_date.
Definition value.hpp:179
static std::error_category const & error_category()
The error category for bsoncxx::v1::types::value::errc.
friend bool operator!=(value const &lhs, value const &rhs)
Equivalent to !(lhs == rhs).
Definition value.hpp:291
~value()
Destroy this object.
value(std::vector< std::uint8_t > const &v)
Initialize as a bsoncxx::v1::types::b_binary with bsoncxx::v1::types::binary_subtype::k_binary.
Definition value.hpp:195
value(v1::array::view v)
Initialize as a bsoncxx::v1::types::b_array.
Definition value.hpp:189
Declares C++17 standard library polyfills.
Declares entities representing a BSON type value.
binary_subtype
Enumeration identifying a BSON binary subtype.
Definition id.hpp:77
@ k_binary
Binary data.
Definition id.hpp:45
Declares entities whose ABI stability is guaranteed for documented symbols.
The top-level namespace within which all bsoncxx library entities are declared.
BSON type value "Array".
Definition view.hpp:201
BSON type value "Boolean".
Definition view.hpp:392
BSON type value "JavaScript Code With Scope".
Definition view.hpp:724
BSON type value "UTC Datetime".
Definition view.hpp:440
BSON type value "DBPointer".
Definition view.hpp:578
BSON type value "Decimal128".
Definition view.hpp:912
BSON type value "Embedded Document".
Definition view.hpp:149
BSON type value "64-bit Binary Floating Point".
Definition view.hpp:50
BSON type value "32-bit Integer".
Definition view.hpp:770
BSON type value "64-bit Integer".
Definition view.hpp:864
BSON type value "Null Value".
Definition view.hpp:499
BSON type value "ObjectID".
Definition view.hpp:344
BSON type value "Regular Expression".
Definition view.hpp:525
BSON type value "UTF-8 String".
Definition view.hpp:101
Declares bsoncxx::v1::array::view.
Declares bsoncxx::v1::decimal128.
Declares bsoncxx::v1::document::view.
Declares bsoncxx::v1::oid.
Provides std::string_view-related polyfills for library API usage.
Declares enumerations identifying the type of a BSON element.
#define BSONCXX_V1_TYPES_XMACRO(X)
X-macro over the name and value of BSON types.
Definition id-fwd.hpp:44
Declares bsoncxx::v1::types::value.
Provides non-owning, read-only entities representing a BSON type value.