MongoDB C++ Driver 4.1.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
types.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 <chrono>
18#include <cstring>
19#include <string>
20
21#include <bsoncxx/types-fwd.hpp>
22
26#include <bsoncxx/oid.hpp>
29
31
32BSONCXX_PRIVATE_WARNINGS_PUSH();
33BSONCXX_PRIVATE_WARNINGS_DISABLE(GNU("-Wfloat-equal"));
34
35namespace bsoncxx {
36namespace v_noabi {
37
43enum class type : std::uint8_t {
44 k_double = 0x01,
45 k_string = 0x02,
46 k_document = 0x03,
47 k_array = 0x04,
48 k_binary = 0x05,
49 k_undefined = 0x06,
50 k_oid = 0x07,
51 k_bool = 0x08,
52 k_date = 0x09,
53 k_null = 0x0A,
54 k_regex = 0x0B,
55 k_dbpointer = 0x0C,
56 k_code = 0x0D,
57 k_symbol = 0x0E,
58 k_codewscope = 0x0F,
59 k_int32 = 0x10,
60 k_timestamp = 0x11,
61 k_int64 = 0x12,
62 k_decimal128 = 0x13,
63 k_maxkey = 0x7F,
64 k_minkey = 0xFF,
65};
66
72enum class binary_sub_type : std::uint8_t {
73 k_binary = 0x00,
74 k_function = 0x01,
77 k_uuid = 0x04,
78 k_md5 = 0x05,
79 k_encrypted = 0x06,
80 k_column = 0x07,
81 k_sensitive = 0x08,
82 k_vector = 0x09,
83 k_user = 0x80,
84};
85
95
105
106namespace types {
107
111struct b_double {
112 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_double;
113
114 double value;
115
119 operator double() const {
120 return value;
121 }
122};
123
129inline bool operator==(b_double const& lhs, b_double const& rhs) {
130 return lhs.value == rhs.value;
131}
132
136struct b_string {
137 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_string;
138
145 template <typename T, detail::requires_not_t<int, detail::is_alike<b_string, T>> = 0>
146 explicit b_string(T&& t) : value(std::forward<T>(t)) {}
147
148 stdx::string_view value;
149
153 operator stdx::string_view() const {
154 return value;
155 }
156};
157
163inline bool operator==(b_string const& lhs, b_string const& rhs) {
164 return lhs.value == rhs.value;
165}
166
171 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_document;
172
173 document::view value;
174
178 operator document::view() const {
179 return value;
180 }
181
186 return value;
187 }
188};
189
195inline bool operator==(b_document const& lhs, b_document const& rhs) {
196 return lhs.value == rhs.value;
197}
198
202struct b_array {
203 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_array;
204
205 array::view value;
206
210 operator array::view() const {
211 return value;
212 }
213};
214
220inline bool operator==(b_array const& lhs, b_array const& rhs) {
221 return lhs.value == rhs.value;
222}
223
227struct b_binary {
228 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_binary;
229
230 binary_sub_type sub_type;
231 uint32_t size;
232 uint8_t const* bytes;
233};
234
240inline bool operator==(b_binary const& lhs, b_binary const& rhs) {
241 return lhs.sub_type == rhs.sub_type && lhs.size == rhs.size &&
242 (!lhs.size || (std::memcmp(lhs.bytes, rhs.bytes, lhs.size) == 0));
243}
244
251 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_undefined;
252};
253
259inline bool operator==(b_undefined const&, b_undefined const&) {
260 return true;
261}
262
266struct b_oid {
267 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_oid;
268
269 oid value;
270};
271
277inline bool operator==(b_oid const& lhs, b_oid const& rhs) {
278 return lhs.value == rhs.value;
279}
280
284struct b_bool {
285 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_bool;
286
287 bool value;
288
292 operator bool() const {
293 return value;
294 }
295};
296
302inline bool operator==(b_bool const& lhs, b_bool const& rhs) {
303 return lhs.value == rhs.value;
304}
305
309struct b_date {
310 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_date;
311
318 explicit b_date(std::chrono::milliseconds value) : value(value) {}
319
326 explicit b_date(std::chrono::system_clock::time_point const& tp)
327 : value(std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch())) {}
328
329 std::chrono::milliseconds value;
330
334 operator int64_t() const {
335 return value.count();
336 }
337
341 int64_t to_int64() const {
342 return value.count();
343 }
344
348 operator std::chrono::system_clock::time_point() const {
349 return std::chrono::system_clock::time_point(
350 std::chrono::duration_cast<std::chrono::system_clock::duration>(value));
351 }
352};
353
359inline bool operator==(b_date const& lhs, b_date const& rhs) {
360 return lhs.value == rhs.value;
361}
362
366struct b_null {
367 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_null;
368};
369
375inline bool operator==(b_null const&, b_null const&) {
376 return true;
377}
378
382struct b_regex {
383 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_regex;
384
394 template <typename T, typename U = stdx::string_view, detail::requires_not_t<int, detail::is_alike<b_regex, T>> = 0>
395 explicit b_regex(T&& regex, U&& options = U{}) : regex(std::forward<T>(regex)), options(std::forward<U>(options)) {}
396
397 stdx::string_view regex;
398 stdx::string_view options;
399};
400
406inline bool operator==(b_regex const& lhs, b_regex const& rhs) {
407 return lhs.regex == rhs.regex && lhs.options == rhs.options;
408}
409
416 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_dbpointer;
417
418 stdx::string_view collection;
419 oid value;
420};
421
427inline bool operator==(b_dbpointer const& lhs, b_dbpointer const& rhs) {
428 return lhs.collection == rhs.collection && lhs.value == rhs.value;
429}
430
434struct b_code {
435 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_code;
436
443 template <typename T, detail::requires_not_t<int, detail::is_alike<b_code, T>> = 0>
444 explicit b_code(T&& t) : code(std::forward<T>(t)) {}
445
447
451 operator stdx::string_view() const {
452 return code;
453 }
454};
455
461inline bool operator==(b_code const& lhs, b_code const& rhs) {
462 return lhs.code == rhs.code;
463}
464
470struct b_symbol {
471 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_symbol;
472
479 template <typename T, detail::requires_not_t<int, detail::is_alike<b_symbol, T>> = 0>
480 explicit b_symbol(T&& t) : symbol(std::forward<T>(t)) {}
481
482 stdx::string_view symbol;
483
487 operator stdx::string_view() const {
488 return symbol;
489 }
490};
491
497inline bool operator==(b_symbol const& lhs, b_symbol const& rhs) {
498 return lhs.symbol == rhs.symbol;
499}
500
505 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_codewscope;
506
516 template <typename T, typename U, detail::requires_not_t<int, detail::is_alike<b_codewscope, T>> = 0>
517 explicit b_codewscope(T&& code, U&& scope) : code(std::forward<T>(code)), scope(std::forward<U>(scope)) {}
518
520 document::view scope;
521};
522
528inline bool operator==(b_codewscope const& lhs, b_codewscope const& rhs) {
529 return lhs.code == rhs.code && lhs.scope == rhs.scope;
530}
531
535struct b_int32 {
536 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_int32;
537
538 int32_t value;
539
543 operator int32_t() const {
544 return value;
545 }
546};
547
553inline bool operator==(b_int32 const& lhs, b_int32 const& rhs) {
554 return lhs.value == rhs.value;
555}
556
561 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_timestamp;
562
563 uint32_t increment;
564 uint32_t timestamp;
565};
566
572inline bool operator==(b_timestamp const& lhs, b_timestamp const& rhs) {
573 return lhs.increment == rhs.increment && lhs.timestamp == rhs.timestamp;
574}
575
579struct b_int64 {
580 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_int64;
581
582 int64_t value;
583
587 operator int64_t() const {
588 return value;
589 }
590};
591
597inline bool operator==(b_int64 const& lhs, b_int64 const& rhs) {
598 return lhs.value == rhs.value;
599}
600
605 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_decimal128;
606
607 decimal128 value;
608
615 template <typename T, detail::requires_not_t<int, detail::is_alike<b_decimal128, T>> = 0>
616 explicit b_decimal128(T&& t) : value(std::forward<T>(t)) {}
617};
618
624inline bool operator==(b_decimal128 const& lhs, b_decimal128 const& rhs) {
625 return lhs.value == rhs.value;
626}
627
631struct b_minkey {
632 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_minkey;
633};
634
640inline bool operator==(b_minkey const&, b_minkey const&) {
641 return true;
642}
643
647struct b_maxkey {
648 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_maxkey;
649};
650
656inline bool operator==(b_maxkey const&, b_maxkey const&) {
657 return true;
658}
659
665inline bool operator!=(b_double const& lhs, b_double const& rhs) {
666 return !(lhs == rhs);
667}
668
674inline bool operator!=(b_string const& lhs, b_string const& rhs) {
675 return !(lhs == rhs);
676}
677
683inline bool operator!=(b_document const& lhs, b_document const& rhs) {
684 return !(lhs == rhs);
685}
686
692inline bool operator!=(b_array const& lhs, b_array const& rhs) {
693 return !(lhs == rhs);
694}
695
701inline bool operator!=(b_binary const& lhs, b_binary const& rhs) {
702 return !(lhs == rhs);
703}
704
710inline bool operator!=(b_undefined const& lhs, b_undefined const& rhs) {
711 return !(lhs == rhs);
712}
713
719inline bool operator!=(b_oid const& lhs, b_oid const& rhs) {
720 return !(lhs == rhs);
721}
722
728inline bool operator!=(b_bool const& lhs, b_bool const& rhs) {
729 return !(lhs == rhs);
730}
731
737inline bool operator!=(b_date const& lhs, b_date const& rhs) {
738 return !(lhs == rhs);
739}
740
746inline bool operator!=(b_null const& lhs, b_null const& rhs) {
747 return !(lhs == rhs);
748}
749
755inline bool operator!=(b_regex const& lhs, b_regex const& rhs) {
756 return !(lhs == rhs);
757}
758
764inline bool operator!=(b_dbpointer const& lhs, b_dbpointer const& rhs) {
765 return !(lhs == rhs);
766}
767
773inline bool operator!=(b_code const& lhs, b_code const& rhs) {
774 return !(lhs == rhs);
775}
776
782inline bool operator!=(b_symbol const& lhs, b_symbol const& rhs) {
783 return !(lhs == rhs);
784}
785
791inline bool operator!=(b_codewscope const& lhs, b_codewscope const& rhs) {
792 return !(lhs == rhs);
793}
794
800inline bool operator!=(b_int32 const& lhs, b_int32 const& rhs) {
801 return !(lhs == rhs);
802}
803
809inline bool operator!=(b_timestamp const& lhs, b_timestamp const& rhs) {
810 return !(lhs == rhs);
811}
812
818inline bool operator!=(b_int64 const& lhs, b_int64 const& rhs) {
819 return !(lhs == rhs);
820}
821
827inline bool operator!=(b_decimal128 const& lhs, b_decimal128 const& rhs) {
828 return !(lhs == rhs);
829}
830
836inline bool operator!=(b_minkey const& lhs, b_minkey const& rhs) {
837 return !(lhs == rhs);
838}
839
845inline bool operator!=(b_maxkey const& lhs, b_maxkey const& rhs) {
846 return !(lhs == rhs);
847}
848
849} // namespace types
850} // namespace v_noabi
851} // namespace bsoncxx
852
853BSONCXX_PRIVATE_WARNINGS_POP();
854
855namespace bsoncxx {
856
858
859} // namespace bsoncxx
860
861namespace bsoncxx {
862namespace types {
863
864using ::bsoncxx::v_noabi::types::operator==;
865using ::bsoncxx::v_noabi::types::operator!=;
866
867} // namespace types
868} // namespace bsoncxx
869
871
Provides bsoncxx::v_noabi::array::view.
#define BSONCXX_ABI_EXPORT
Exports the associated entity as part of the ABI.
Definition export.hpp:15
#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 polyfill for std::string_view.
Definition string_view.hpp:411
A read-only, non-owning view of a BSON document.
Definition view.hpp:36
Represents a MongoDB BSON Decimal128.
Definition decimal128.hpp:40
A read-only, non-owning view of a BSON document.
Definition view.hpp:35
Represents a MongoDB BSON ObjectId.
Definition oid.hpp:36
Provides bsoncxx::v_noabi::decimal128.
Provides bsoncxx::v_noabi::document::view.
Declares entities representing BSON value types.
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:72
@ k_uuid
UUID.
Definition types.hpp:77
@ k_binary_deprecated
Binary (Old).
Definition types.hpp:75
@ k_sensitive
Sensitive.
Definition types.hpp:81
@ k_md5
MD5.
Definition types.hpp:78
@ k_user
User defined.
Definition types.hpp:83
@ k_function
Function.
Definition types.hpp:74
@ k_column
Compressed BSON column.
Definition types.hpp:80
@ k_uuid_deprecated
UUID (Old).
Definition types.hpp:76
@ k_encrypted
Encrypted BSON value.
Definition types.hpp:79
@ k_vector
BSON Binary Vector.
Definition types.hpp:82
std::string to_string(type rhs)
Returns a stringification of the given type.
type
An enumeration of each BSON type.
Definition types.hpp:43
@ k_decimal128
128-bit decimal floating point.
Definition types.hpp:62
@ k_minkey
Max key.
Definition types.hpp:64
@ k_double
64-bit binary floating point.
Definition types.hpp:44
@ k_dbpointer
DBPointer.
Definition types.hpp:55
@ k_int64
64-bit integer.
Definition types.hpp:61
@ k_codewscope
JavaScript code with scope.
Definition types.hpp:58
@ k_regex
Regular expression.
Definition types.hpp:54
@ k_string
UTF-8 string.
Definition types.hpp:45
@ k_array
Array.
Definition types.hpp:47
@ k_bool
Boolean.
Definition types.hpp:51
@ k_symbol
Symbol.
Definition types.hpp:57
@ k_oid
ObjectId.
Definition types.hpp:50
@ k_date
UTC datetime.
Definition types.hpp:52
@ k_int32
32-bit integer.
Definition types.hpp:59
@ k_timestamp
Timestamp.
Definition types.hpp:60
@ k_undefined
Undefined value.
Definition types.hpp:49
@ k_code
JavaScript code.
Definition types.hpp:56
@ k_document
Embedded document.
Definition types.hpp:46
@ k_maxkey
Min key.
Definition types.hpp:63
@ k_binary
Binary data.
Definition types.hpp:48
@ k_null
Null value.
Definition types.hpp:53
The top-level namespace within which all bsoncxx library entities are declared.
Provides bsoncxx::v_noabi::oid.
b_code(T &&t)
Constructor for b_code.
Definition types.hpp:444
b_codewscope(T &&code, U &&scope)
Constructor for b_codewscope.
Definition types.hpp:517
b_date(std::chrono::milliseconds value)
Constructor for b_date.
Definition types.hpp:318
b_decimal128(T &&t)
Constructor for b_decimal128.
Definition types.hpp:616
b_regex(T &&regex, U &&options=U{})
Constructor for b_regex.
Definition types.hpp:395
b_string(T &&t)
Constructor for b_string.
Definition types.hpp:146
b_symbol(T &&t)
Constructor for b_symbol.
Definition types.hpp:480
A BSON array value.
Definition types.hpp:202
bool operator==(b_array const &lhs, b_array const &rhs)
free function comparator for b_array
Definition types.hpp:220
bool operator!=(b_array const &lhs, b_array const &rhs)
free function comparator for b_array
Definition types.hpp:692
A BSON binary data value.
Definition types.hpp:227
bool operator==(b_binary const &lhs, b_binary const &rhs)
free function comparator for b_binary
Definition types.hpp:240
bool operator!=(b_binary const &lhs, b_binary const &rhs)
free function comparator for b_binary
Definition types.hpp:701
A BSON boolean value.
Definition types.hpp:284
bool operator!=(b_bool const &lhs, b_bool const &rhs)
free function comparator for b_bool
Definition types.hpp:728
bool operator==(b_bool const &lhs, b_bool const &rhs)
free function comparator for b_bool
Definition types.hpp:302
bool operator!=(b_code const &lhs, b_code const &rhs)
free function comparator for b_code
Definition types.hpp:773
bool operator==(b_code const &lhs, b_code const &rhs)
free function comparator for b_code
Definition types.hpp:461
b_code(T &&t)
Constructor for b_code.
Definition types.hpp:444
bool operator==(b_codewscope const &lhs, b_codewscope const &rhs)
free function comparator for b_codewscope
Definition types.hpp:528
bool operator!=(b_codewscope const &lhs, b_codewscope const &rhs)
free function comparator for b_codewscope
Definition types.hpp:791
b_codewscope(T &&code, U &&scope)
Constructor for b_codewscope.
Definition types.hpp:517
bool operator==(b_date const &lhs, b_date const &rhs)
free function comparator for b_date
Definition types.hpp:359
bool operator!=(b_date const &lhs, b_date const &rhs)
free function comparator for b_date
Definition types.hpp:737
int64_t to_int64() const
Manually convert this b_date to an int64_t.
Definition types.hpp:341
b_date(std::chrono::system_clock::time_point const &tp)
Constructor for b_date.
Definition types.hpp:326
b_date(std::chrono::milliseconds value)
Constructor for b_date.
Definition types.hpp:318
A BSON DBPointer (aka DBRef) value.
Definition types.hpp:415
bool operator==(b_dbpointer const &lhs, b_dbpointer const &rhs)
free function comparator for b_dbpointer
Definition types.hpp:427
bool operator!=(b_dbpointer const &lhs, b_dbpointer const &rhs)
free function comparator for b_dbpointer
Definition types.hpp:764
bool operator!=(b_decimal128 const &lhs, b_decimal128 const &rhs)
free function comparator for b_decimal128
Definition types.hpp:827
bool operator==(b_decimal128 const &lhs, b_decimal128 const &rhs)
free function comparator for b_decimal128
Definition types.hpp:624
b_decimal128(T &&t)
Constructor for b_decimal128.
Definition types.hpp:616
A BSON document value.
Definition types.hpp:170
bool operator==(b_document const &lhs, b_document const &rhs)
free function comparator for b_document
Definition types.hpp:195
document::view view()
Returns an unwrapped document::view.
Definition types.hpp:185
bool operator!=(b_document const &lhs, b_document const &rhs)
free function comparator for b_document
Definition types.hpp:683
A BSON double value.
Definition types.hpp:111
bool operator!=(b_double const &lhs, b_double const &rhs)
free function comparator for b_double
Definition types.hpp:665
bool operator==(b_double const &lhs, b_double const &rhs)
free function comparator for b_double
Definition types.hpp:129
A BSON signed 32-bit integer value.
Definition types.hpp:535
bool operator==(b_int32 const &lhs, b_int32 const &rhs)
free function comparator for b_int32
Definition types.hpp:553
bool operator!=(b_int32 const &lhs, b_int32 const &rhs)
free function comparator for b_int32
Definition types.hpp:800
A BSON 64-bit signed integer value.
Definition types.hpp:579
bool operator!=(b_int64 const &lhs, b_int64 const &rhs)
free function comparator for b_int64
Definition types.hpp:818
bool operator==(b_int64 const &lhs, b_int64 const &rhs)
free function comparator for b_int64
Definition types.hpp:597
A BSON max-key value.
Definition types.hpp:647
bool operator!=(b_maxkey const &lhs, b_maxkey const &rhs)
free function comparator for b_maxkey
Definition types.hpp:845
bool operator==(b_maxkey const &, b_maxkey const &)
free function comparator for b_maxkey
Definition types.hpp:656
A BSON min-key value.
Definition types.hpp:631
bool operator==(b_minkey const &, b_minkey const &)
free function comparator for b_minkey
Definition types.hpp:640
bool operator!=(b_minkey const &lhs, b_minkey const &rhs)
free function comparator for b_minkey
Definition types.hpp:836
A BSON null value.
Definition types.hpp:366
bool operator==(b_null const &, b_null const &)
free function comparator for b_null
Definition types.hpp:375
bool operator!=(b_null const &lhs, b_null const &rhs)
free function comparator for b_null
Definition types.hpp:746
A BSON ObjectId value.
Definition types.hpp:266
bool operator==(b_oid const &lhs, b_oid const &rhs)
free function comparator for b_oid
Definition types.hpp:277
bool operator!=(b_oid const &lhs, b_oid const &rhs)
free function comparator for b_oid
Definition types.hpp:719
bool operator!=(b_regex const &lhs, b_regex const &rhs)
free function comparator for b_regex
Definition types.hpp:755
b_regex(T &&regex, U &&options=U{})
Constructor for b_regex.
Definition types.hpp:395
bool operator==(b_regex const &lhs, b_regex const &rhs)
free function comparator for b_regex
Definition types.hpp:406
b_string(T &&t)
Constructor for b_string.
Definition types.hpp:146
bool operator==(b_string const &lhs, b_string const &rhs)
free function comparator for b_string
Definition types.hpp:163
bool operator!=(b_string const &lhs, b_string const &rhs)
free function comparator for b_string
Definition types.hpp:674
b_symbol(T &&t)
Constructor for b_symbol.
Definition types.hpp:480
bool operator==(b_symbol const &lhs, b_symbol const &rhs)
free function comparator for b_symbol
Definition types.hpp:497
bool operator!=(b_symbol const &lhs, b_symbol const &rhs)
free function comparator for b_symbol
Definition types.hpp:782
A BSON replication timestamp value.
Definition types.hpp:560
bool operator==(b_timestamp const &lhs, b_timestamp const &rhs)
free function comparator for b_timestamp
Definition types.hpp:572
bool operator!=(b_timestamp const &lhs, b_timestamp const &rhs)
free function comparator for b_timestamp
Definition types.hpp:809
A BSON undefined value.
Definition types.hpp:250
bool operator!=(b_undefined const &lhs, b_undefined const &rhs)
free function comparator for b_undefined
Definition types.hpp:710
bool operator==(b_undefined const &, b_undefined const &)
free function comparator for b_undefined
Definition types.hpp:259
Declares entities used to represent BSON types.
Provides std::string_view-related polyfills for library API usage.
For internal use only!