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/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
52class value {
53 private:
54 class impl;
55
56 alignas(BSONCXX_PRIVATE_MAX_ALIGN_T) std::array<unsigned char, 32> _storage;
57
58 public:
63
71
78 BSONCXX_ABI_EXPORT_CDECL(value&) operator=(value&& other) noexcept;
79
87
94 BSONCXX_ABI_EXPORT_CDECL(value&) operator=(value const& other);
95
100
113
114#pragma push_macro("X")
115#undef X
116#define X(_name, _value) explicit BSONCXX_ABI_EXPORT_CDECL() value(v1::types::b_##_name v);
117
132#pragma pop_macro("X")
133
137 explicit value(std::nullptr_t) : value{v1::types::b_null{}} {}
138
143
147 explicit value(std::int32_t v) : value{v1::types::b_int32{v}} {}
148
152 explicit value(std::int64_t v) : value{v1::types::b_int64{v}} {}
153
157 explicit value(double v) : value{v1::types::b_double{v}} {}
158
162 explicit value(bool v) : value{v1::types::b_bool{v}} {}
163
167 explicit value(v1::oid v) : value{v1::types::b_oid{v}} {}
168
173
177 explicit value(std::chrono::milliseconds v) : value{v1::types::b_date{v}} {}
178
183
187 explicit value(v1::array::view v) : value{v1::types::b_array{v}} {}
188
193 explicit value(std::vector<std::uint8_t> const& v)
194 : value{v.data(), v.size(), v1::types::binary_subtype::k_binary} {}
195
199 explicit value(std::vector<std::uint8_t> const& v, v1::types::binary_subtype subtype)
200 : value{v.data(), v.size(), subtype} {}
201
206
211 : value{v1::types::b_codewscope{code, scope}} {}
212
217 : value{v1::types::b_regex{regex, options}} {}
218
222 explicit value(char const* v) : value{v1::stdx::string_view{v}} {}
223
227 explicit value(std::string const& v) : value{v1::stdx::string_view{v}} {}
228
233 explicit value(std::uint8_t const* data, std::size_t size)
234 : value{data, size, v1::types::binary_subtype::k_binary} {}
235
239 explicit BSONCXX_ABI_EXPORT_CDECL()
240 value(std::uint8_t const* data, std::size_t size, v1::types::binary_subtype const sub_type);
241
246
251
255 /* explicit(false) */ operator v1::types::view() const {
256 return this->view();
257 }
258
259#pragma push_macro("X")
260#undef X
261#define X(_name, _value) \
262 v1::types::b_##_name get_##_name() const { \
263 return this->view().get_##_name(); \
264 }
265
277#pragma pop_macro("X")
278
282 friend bool operator==(value const& lhs, value const& rhs) {
283 return lhs.view() == rhs.view();
284 }
285
289 friend bool operator!=(value const& lhs, value const& rhs) {
290 return !(lhs == rhs);
291 }
292
301
305 static BSONCXX_ABI_EXPORT_CDECL(std::error_category const&) error_category();
306
310 friend std::error_code make_error_code(errc v) {
311 return {static_cast<int>(v), error_category()};
312 }
313
314 class internal;
315};
316
317} // namespace types
318} // namespace v1
319} // namespace bsoncxx
320
321template <>
322struct std::is_error_code_enum<bsoncxx::v1::types::value::errc> : true_type {};
323
325
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:47
A BSON Decimal128.
Definition decimal128.hpp:37
A non-owning, read-only BSON document.
Definition view.hpp:52
A BSON ObjectID.
Definition oid.hpp:41
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:199
value(std::int64_t v)
Initialize as a bsoncxx::v1::types::b_int64.
Definition value.hpp:152
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:167
value(std::nullptr_t)
Initialize as a bsoncxx::v1::types::b_null.
Definition value.hpp:137
errc
Errors codes which may be returned by bsoncxx::v1::types::value.
Definition value.hpp:296
@ invalid_type
Requested BSON type is not supported.
Definition value.hpp:298
@ invalid_length_u32
Length is too long (exceeds UINT32_MAX).
Definition value.hpp:299
@ zero
Zero.
Definition value.hpp:297
friend bool operator==(value const &lhs, value const &rhs)
Equivalent to lhs.view() == rhs.view().
Definition value.hpp:282
value(v1::decimal128 v)
Initialize as a bsoncxx::v1::types::b_decimal128.
Definition value.hpp:172
value(bool v)
Initialize as a bsoncxx::v1::types::b_bool.
Definition value.hpp:162
value(char const *v)
Initialize as a bsoncxx::v1::types::b_string.
Definition value.hpp:222
value(v1::stdx::string_view c, v1::oid v)
Initialize as a bsoncxx::v1::types::b_dbpointer.
Definition value.hpp:205
value(v1::stdx::string_view regex, v1::stdx::string_view options)
Initialize as a bsoncxx::v1::types::b_regex.
Definition value.hpp:216
value(v1::document::view v)
Initialize as a bsoncxx::v1::types::b_document.
Definition value.hpp:182
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:157
value(std::int32_t v)
Initialize as a bsoncxx::v1::types::b_int32.
Definition value.hpp:147
value(v1::stdx::string_view code, v1::document::view scope)
Initialize as a bsoncxx::v1::types::b_codewscope.
Definition value.hpp:210
friend std::error_code make_error_code(errc v)
Support implicit conversion to std::error_code.
Definition value.hpp:310
value(value &&other) noexcept
Move construction.
value(v1::stdx::string_view v)
Initialize as a bsoncxx::v1::types::b_string.
Definition value.hpp:142
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:233
value(std::string const &v)
Initialize as a bsoncxx::v1::types::b_string.
Definition value.hpp:227
value(std::chrono::milliseconds v)
Initialize as a bsoncxx::v1::types::b_date.
Definition value.hpp:177
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:289
~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:193
value(v1::array::view v)
Initialize as a bsoncxx::v1::types::b_array.
Definition value.hpp:187
Declares C++17 standard library polyfills.
Declares entities representing a BSON type value.
binary_subtype
Enumeration identifying a BSON binary subtype.
Definition id.hpp:71
@ k_binary
Binary data.
Definition id.hpp:43
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:193
BSON type value "Boolean".
Definition view.hpp:376
BSON type value "JavaScript Code With Scope".
Definition view.hpp:694
BSON type value "UTC Datetime".
Definition view.hpp:422
BSON type value "DBPointer".
Definition view.hpp:554
BSON type value "Decimal128".
Definition view.hpp:874
BSON type value "Embedded Document".
Definition view.hpp:143
BSON type value "64-bit Binary Floating Point".
Definition view.hpp:48
BSON type value "32-bit Integer".
Definition view.hpp:738
BSON type value "64-bit Integer".
Definition view.hpp:828
BSON type value "Null Value".
Definition view.hpp:479
BSON type value "ObjectID".
Definition view.hpp:330
BSON type value "Regular Expression".
Definition view.hpp:503
BSON type value "UTF-8 String".
Definition view.hpp:97
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.