MongoDB C++ Driver mongocxx-4.0.0
Loading...
Searching...
No Matches
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_PUSH_WARNINGS();
33BSONCXX_DISABLE_WARNING(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,
75 k_binary_deprecated = 0x02,
76 k_uuid_deprecated = 0x03,
77 k_uuid = 0x04,
78 k_md5 = 0x05,
79 k_encrypted = 0x06,
80 k_column = 0x07,
81 k_sensitive = 0x08,
82 k_user = 0x80,
83};
84
94
104
105namespace types {
106
110struct b_double {
111 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_double;
112
113 double value;
114
118 operator double() const {
119 return value;
120 }
121};
122
128inline bool operator==(const b_double& lhs, const b_double& rhs) {
129 return lhs.value == rhs.value;
130}
131
135struct b_string {
136 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_string;
137
144 template <typename T, detail::requires_not_t<int, detail::is_alike<b_string, T>> = 0>
145 explicit b_string(T&& t) : value(std::forward<T>(t)) {}
146
147 stdx::string_view value;
148
152 operator stdx::string_view() const {
153 return value;
154 }
155};
156
162inline bool operator==(const b_string& lhs, const b_string& rhs) {
163 return lhs.value == rhs.value;
164}
165
170 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_document;
171
172 document::view value;
173
177 operator document::view() const {
178 return value;
179 }
180
185 return value;
186 }
187};
188
194inline bool operator==(const b_document& lhs, const b_document& rhs) {
195 return lhs.value == rhs.value;
196}
197
201struct b_array {
202 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_array;
203
204 array::view value;
205
209 operator array::view() const {
210 return value;
211 }
212};
213
219inline bool operator==(const b_array& lhs, const b_array& rhs) {
220 return lhs.value == rhs.value;
221}
222
226struct b_binary {
227 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_binary;
228
229 binary_sub_type sub_type;
230 uint32_t size;
231 const uint8_t* bytes;
232};
233
239inline bool operator==(const b_binary& lhs, const b_binary& rhs) {
240 return lhs.sub_type == rhs.sub_type && lhs.size == rhs.size &&
241 (!lhs.size || (std::memcmp(lhs.bytes, rhs.bytes, lhs.size) == 0));
242}
243
250 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_undefined;
251};
252
258inline bool operator==(const b_undefined&, const b_undefined&) {
259 return true;
260}
261
265struct b_oid {
266 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_oid;
267
268 oid value;
269};
270
276inline bool operator==(const b_oid& lhs, const b_oid& rhs) {
277 return lhs.value == rhs.value;
278}
279
283struct b_bool {
284 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_bool;
285
286 bool value;
287
291 operator bool() const {
292 return value;
293 }
294};
295
301inline bool operator==(const b_bool& lhs, const b_bool& rhs) {
302 return lhs.value == rhs.value;
303}
304
308struct b_date {
309 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_date;
310
317 explicit b_date(std::chrono::milliseconds value) : value(value) {}
318
325 explicit b_date(const std::chrono::system_clock::time_point& tp)
326 : value(std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch())) {}
327
328 std::chrono::milliseconds value;
329
333 operator int64_t() const {
334 return value.count();
335 }
336
340 int64_t to_int64() const {
341 return value.count();
342 }
343
347 operator std::chrono::system_clock::time_point() const {
348 return std::chrono::system_clock::time_point(
349 std::chrono::duration_cast<std::chrono::system_clock::duration>(value));
350 }
351};
352
358inline bool operator==(const b_date& lhs, const b_date& rhs) {
359 return lhs.value == rhs.value;
360}
361
365struct b_null {
366 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_null;
367};
368
374inline bool operator==(const b_null&, const b_null&) {
375 return true;
376}
377
381struct b_regex {
382 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_regex;
383
393 template <typename T,
394 typename U = stdx::string_view,
395 detail::requires_not_t<int, detail::is_alike<b_regex, T>> = 0>
396 explicit b_regex(T&& regex, U&& options = U{})
397 : regex(std::forward<T>(regex)), options(std::forward<U>(options)) {}
398
399 stdx::string_view regex;
400 stdx::string_view options;
401};
402
408inline bool operator==(const b_regex& lhs, const b_regex& rhs) {
409 return lhs.regex == rhs.regex && lhs.options == rhs.options;
410}
411
418 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_dbpointer;
419
420 stdx::string_view collection;
421 oid value;
422};
423
429inline bool operator==(const b_dbpointer& lhs, const b_dbpointer& rhs) {
430 return lhs.collection == rhs.collection && lhs.value == rhs.value;
431}
432
436struct b_code {
437 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_code;
438
445 template <typename T, detail::requires_not_t<int, detail::is_alike<b_code, T>> = 0>
446 explicit b_code(T&& t) : code(std::forward<T>(t)) {}
447
449
453 operator stdx::string_view() const {
454 return code;
455 }
456};
457
463inline bool operator==(const b_code& lhs, const b_code& rhs) {
464 return lhs.code == rhs.code;
465}
466
472struct b_symbol {
473 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_symbol;
474
481 template <typename T, detail::requires_not_t<int, detail::is_alike<b_symbol, T>> = 0>
482 explicit b_symbol(T&& t) : symbol(std::forward<T>(t)) {}
483
484 stdx::string_view symbol;
485
489 operator stdx::string_view() const {
490 return symbol;
491 }
492};
493
499inline bool operator==(const b_symbol& lhs, const b_symbol& rhs) {
500 return lhs.symbol == rhs.symbol;
501}
502
507 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_codewscope;
508
518 template <typename T,
519 typename U,
520 detail::requires_not_t<int, detail::is_alike<b_codewscope, T>> = 0>
521 explicit b_codewscope(T&& code, U&& scope)
522 : code(std::forward<T>(code)), scope(std::forward<U>(scope)) {}
523
525 document::view scope;
526};
527
533inline bool operator==(const b_codewscope& lhs, const b_codewscope& rhs) {
534 return lhs.code == rhs.code && lhs.scope == rhs.scope;
535}
536
540struct b_int32 {
541 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_int32;
542
543 int32_t value;
544
548 operator int32_t() const {
549 return value;
550 }
551};
552
558inline bool operator==(const b_int32& lhs, const b_int32& rhs) {
559 return lhs.value == rhs.value;
560}
561
566 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_timestamp;
567
568 uint32_t increment;
569 uint32_t timestamp;
570};
571
577inline bool operator==(const b_timestamp& lhs, const b_timestamp& rhs) {
578 return lhs.increment == rhs.increment && lhs.timestamp == rhs.timestamp;
579}
580
584struct b_int64 {
585 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_int64;
586
587 int64_t value;
588
592 operator int64_t() const {
593 return value;
594 }
595};
596
602inline bool operator==(const b_int64& lhs, const b_int64& rhs) {
603 return lhs.value == rhs.value;
604}
605
610 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_decimal128;
611
612 decimal128 value;
613
620 template <typename T, detail::requires_not_t<int, detail::is_alike<b_decimal128, T>> = 0>
621 explicit b_decimal128(T&& t) : value(std::forward<T>(t)) {}
622};
623
629inline bool operator==(const b_decimal128& lhs, const b_decimal128& rhs) {
630 return lhs.value == rhs.value;
631}
632
636struct b_minkey {
637 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_minkey;
638};
639
645inline bool operator==(const b_minkey&, const b_minkey&) {
646 return true;
647}
648
652struct b_maxkey {
653 BSONCXX_ABI_EXPORT static constexpr auto type_id = type::k_maxkey;
654};
655
661inline bool operator==(const b_maxkey&, const b_maxkey&) {
662 return true;
663}
664
670inline bool operator!=(const b_double& lhs, const b_double& rhs) {
671 return !(lhs == rhs);
672}
673
679inline bool operator!=(const b_string& lhs, const b_string& rhs) {
680 return !(lhs == rhs);
681}
682
688inline bool operator!=(const b_document& lhs, const b_document& rhs) {
689 return !(lhs == rhs);
690}
691
697inline bool operator!=(const b_array& lhs, const b_array& rhs) {
698 return !(lhs == rhs);
699}
700
706inline bool operator!=(const b_binary& lhs, const b_binary& rhs) {
707 return !(lhs == rhs);
708}
709
715inline bool operator!=(const b_undefined& lhs, const b_undefined& rhs) {
716 return !(lhs == rhs);
717}
718
724inline bool operator!=(const b_oid& lhs, const b_oid& rhs) {
725 return !(lhs == rhs);
726}
727
733inline bool operator!=(const b_bool& lhs, const b_bool& rhs) {
734 return !(lhs == rhs);
735}
736
742inline bool operator!=(const b_date& lhs, const b_date& rhs) {
743 return !(lhs == rhs);
744}
745
751inline bool operator!=(const b_null& lhs, const b_null& rhs) {
752 return !(lhs == rhs);
753}
754
760inline bool operator!=(const b_regex& lhs, const b_regex& rhs) {
761 return !(lhs == rhs);
762}
763
769inline bool operator!=(const b_dbpointer& lhs, const b_dbpointer& rhs) {
770 return !(lhs == rhs);
771}
772
778inline bool operator!=(const b_code& lhs, const b_code& rhs) {
779 return !(lhs == rhs);
780}
781
787inline bool operator!=(const b_symbol& lhs, const b_symbol& rhs) {
788 return !(lhs == rhs);
789}
790
796inline bool operator!=(const b_codewscope& lhs, const b_codewscope& rhs) {
797 return !(lhs == rhs);
798}
799
805inline bool operator!=(const b_int32& lhs, const b_int32& rhs) {
806 return !(lhs == rhs);
807}
808
814inline bool operator!=(const b_timestamp& lhs, const b_timestamp& rhs) {
815 return !(lhs == rhs);
816}
817
823inline bool operator!=(const b_int64& lhs, const b_int64& rhs) {
824 return !(lhs == rhs);
825}
826
832inline bool operator!=(const b_decimal128& lhs, const b_decimal128& rhs) {
833 return !(lhs == rhs);
834}
835
841inline bool operator!=(const b_minkey& lhs, const b_minkey& rhs) {
842 return !(lhs == rhs);
843}
844
850inline bool operator!=(const b_maxkey& lhs, const b_maxkey& rhs) {
851 return !(lhs == rhs);
852}
853
854} // namespace types
855} // namespace v_noabi
856} // namespace bsoncxx
857
858BSONCXX_POP_WARNINGS();
859
860namespace bsoncxx {
861
862using ::bsoncxx::v_noabi::to_string;
863
864} // namespace bsoncxx
865
866namespace bsoncxx {
867namespace types {
868
869using ::bsoncxx::v_noabi::types::operator==;
870using ::bsoncxx::v_noabi::types::operator!=;
871
872} // namespace types
873} // namespace bsoncxx
874
876
881
882#if defined(BSONCXX_PRIVATE_DOXYGEN_PREPROCESSOR)
883
884namespace bsoncxx {
885
887std::string to_string(v_noabi::type rhs);
888
891
892namespace types {
893
896
899
902
905
908
911
914
917
920
923
926
929
932
935
938
941
944
947
950
953
956
959
962
965
968
971
974
977
980
983
986
989
992
995
998
1001
1004
1007
1010
1013
1016
1019
1020} // namespace types
1021
1022} // namespace bsoncxx
1023
1024#endif // defined(BSONCXX_PRIVATE_DOXYGEN_PREPROCESSOR)
Provides bsoncxx::v_noabi::array::view.
The bsoncxx macro guard postlude header.
The bsoncxx macro guard prelude header.
A read-only, non-owning view of a BSON document.
Definition view.hpp:36
Represents an IEEE 754-2008 BSON Decimal128 value in a platform-independent way.
Definition decimal128.hpp:32
A read-only, non-owning view of a BSON document.
Definition view.hpp:35
Represents a MongoDB ObjectId. As this BSON type is used within the MongoDB server as a primary key f...
Definition oid.hpp:41
A polyfill for std::string_view.
Definition string_view.hpp:436
Provides bsoncxx::v_noabi::decimal128.
Provides bsoncxx::v_noabi::document::view.
#define BSONCXX_ABI_EXPORT
Exports the associated entity as part of the ABI.
Definition fwd.hpp:153
#define BSONCXX_ABI_EXPORT_CDECL(...)
Equivalent to BSONCXX_ABI_EXPORT with BSONCXX_ABI_CDECL.
Definition fwd.hpp:177
bool operator!=(const v_noabi::types::b_double &lhs, const v_noabi::types::b_double &rhs)
bsoncxx::v_noabi::types::operator!=(const v_noabi::types::b_double& lhs, const v_noabi::types::b_doub...
bool operator==(const v_noabi::types::b_double &lhs, const v_noabi::types::b_double &rhs)
bsoncxx::v_noabi::types::operator==(const v_noabi::types::b_double& lhs, const v_noabi::types::b_doub...
binary_sub_type
An enumeration of each BSON binary sub type.
Definition types.hpp:72
@ k_column
Compressed BSON column.
@ k_encrypted
Encrypted BSON value.
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.
@ k_double
64-bit binary floating point.
@ k_int64
64-bit integer.
@ k_codewscope
JavaScript code with scope.
@ k_regex
Regular expression.
@ k_string
UTF-8 string.
@ k_date
UTC datetime.
@ k_int32
32-bit integer.
@ k_undefined
Undefined value.
@ k_code
JavaScript code.
@ k_document
Embedded document.
@ k_binary
Binary data.
The top-level namespace within which all bsoncxx library entities are declared.
std::string to_string(v_noabi::type rhs)
bsoncxx::v_noabi::to_string(v_noabi::type rhs)
The top-level namespace reserved for the C++ standard library.
Provides bsoncxx::v_noabi::oid.
Provides std::string_view-related polyfills for library API usage.
A BSON array value.
Definition types.hpp:201
bool operator==(const b_array &lhs, const b_array &rhs)
free function comparator for b_array
Definition types.hpp:219
bool operator!=(const b_array &lhs, const b_array &rhs)
free function comparator for b_array
Definition types.hpp:697
A BSON binary data value.
Definition types.hpp:226
bool operator==(const b_binary &lhs, const b_binary &rhs)
free function comparator for b_binary
Definition types.hpp:239
bool operator!=(const b_binary &lhs, const b_binary &rhs)
free function comparator for b_binary
Definition types.hpp:706
A BSON boolean value.
Definition types.hpp:283
bool operator!=(const b_bool &lhs, const b_bool &rhs)
free function comparator for b_bool
Definition types.hpp:733
bool operator==(const b_bool &lhs, const b_bool &rhs)
free function comparator for b_bool
Definition types.hpp:301
A BSON JavaScript code value.
Definition types.hpp:436
bool operator==(const b_code &lhs, const b_code &rhs)
free function comparator for b_code
Definition types.hpp:463
bool operator!=(const b_code &lhs, const b_code &rhs)
free function comparator for b_code
Definition types.hpp:778
b_code(T &&t)
Constructor for b_code.
Definition types.hpp:446
A BSON JavaScript code with scope value.
Definition types.hpp:506
bool operator!=(const b_codewscope &lhs, const b_codewscope &rhs)
free function comparator for b_codewscope
Definition types.hpp:796
bool operator==(const b_codewscope &lhs, const b_codewscope &rhs)
free function comparator for b_codewscope
Definition types.hpp:533
b_codewscope(T &&code, U &&scope)
Constructor for b_codewscope.
Definition types.hpp:521
A BSON date value.
Definition types.hpp:308
bool operator==(const b_date &lhs, const b_date &rhs)
free function comparator for b_date
Definition types.hpp:358
b_date(const std::chrono::system_clock::time_point &tp)
Constructor for b_date.
Definition types.hpp:325
int64_t to_int64() const
Manually convert this b_date to an int64_t.
Definition types.hpp:340
bool operator!=(const b_date &lhs, const b_date &rhs)
free function comparator for b_date
Definition types.hpp:742
b_date(std::chrono::milliseconds value)
Constructor for b_date.
Definition types.hpp:317
A BSON DBPointer (aka DBRef) value.
Definition types.hpp:417
bool operator==(const b_dbpointer &lhs, const b_dbpointer &rhs)
free function comparator for b_dbpointer
Definition types.hpp:429
bool operator!=(const b_dbpointer &lhs, const b_dbpointer &rhs)
free function comparator for b_dbpointer
Definition types.hpp:769
A BSON Decimal128 value.
Definition types.hpp:609
bool operator!=(const b_decimal128 &lhs, const b_decimal128 &rhs)
free function comparator for b_decimal128
Definition types.hpp:832
bool operator==(const b_decimal128 &lhs, const b_decimal128 &rhs)
free function comparator for b_decimal128
Definition types.hpp:629
b_decimal128(T &&t)
Constructor for b_decimal128.
Definition types.hpp:621
A BSON document value.
Definition types.hpp:169
bool operator!=(const b_document &lhs, const b_document &rhs)
free function comparator for b_document
Definition types.hpp:688
document::view view()
Returns an unwrapped document::view.
Definition types.hpp:184
bool operator==(const b_document &lhs, const b_document &rhs)
free function comparator for b_document
Definition types.hpp:194
A BSON double value.
Definition types.hpp:110
bool operator==(const b_double &lhs, const b_double &rhs)
free function comparator for b_double
Definition types.hpp:128
bool operator!=(const b_double &lhs, const b_double &rhs)
free function comparator for b_double
Definition types.hpp:670
A BSON signed 32-bit integer value.
Definition types.hpp:540
bool operator==(const b_int32 &lhs, const b_int32 &rhs)
free function comparator for b_int32
Definition types.hpp:558
bool operator!=(const b_int32 &lhs, const b_int32 &rhs)
free function comparator for b_int32
Definition types.hpp:805
A BSON 64-bit signed integer value.
Definition types.hpp:584
bool operator==(const b_int64 &lhs, const b_int64 &rhs)
free function comparator for b_int64
Definition types.hpp:602
bool operator!=(const b_int64 &lhs, const b_int64 &rhs)
free function comparator for b_int64
Definition types.hpp:823
A BSON max-key value.
Definition types.hpp:652
bool operator!=(const b_maxkey &lhs, const b_maxkey &rhs)
free function comparator for b_maxkey
Definition types.hpp:850
bool operator==(const b_maxkey &, const b_maxkey &)
free function comparator for b_maxkey
Definition types.hpp:661
A BSON min-key value.
Definition types.hpp:636
bool operator!=(const b_minkey &lhs, const b_minkey &rhs)
free function comparator for b_minkey
Definition types.hpp:841
bool operator==(const b_minkey &, const b_minkey &)
free function comparator for b_minkey
Definition types.hpp:645
A BSON null value.
Definition types.hpp:365
bool operator!=(const b_null &lhs, const b_null &rhs)
free function comparator for b_null
Definition types.hpp:751
bool operator==(const b_null &, const b_null &)
free function comparator for b_null
Definition types.hpp:374
A BSON ObjectId value.
Definition types.hpp:265
bool operator==(const b_oid &lhs, const b_oid &rhs)
free function comparator for b_oid
Definition types.hpp:276
bool operator!=(const b_oid &lhs, const b_oid &rhs)
free function comparator for b_oid
Definition types.hpp:724
A BSON regex value.
Definition types.hpp:381
bool operator!=(const b_regex &lhs, const b_regex &rhs)
free function comparator for b_regex
Definition types.hpp:760
bool operator==(const b_regex &lhs, const b_regex &rhs)
free function comparator for b_regex
Definition types.hpp:408
b_regex(T &&regex, U &&options=U{})
Constructor for b_regex.
Definition types.hpp:396
A BSON UTF-8 encoded string value.
Definition types.hpp:135
b_string(T &&t)
Constructor for b_string.
Definition types.hpp:145
bool operator!=(const b_string &lhs, const b_string &rhs)
free function comparator for b_string
Definition types.hpp:679
bool operator==(const b_string &lhs, const b_string &rhs)
free function comparator for b_string
Definition types.hpp:162
A BSON Symbol value.
Definition types.hpp:472
b_symbol(T &&t)
Constructor for b_symbol.
Definition types.hpp:482
bool operator==(const b_symbol &lhs, const b_symbol &rhs)
free function comparator for b_symbol
Definition types.hpp:499
bool operator!=(const b_symbol &lhs, const b_symbol &rhs)
free function comparator for b_symbol
Definition types.hpp:787
A BSON replication timestamp value.
Definition types.hpp:565
bool operator!=(const b_timestamp &lhs, const b_timestamp &rhs)
free function comparator for b_timestamp
Definition types.hpp:814
bool operator==(const b_timestamp &lhs, const b_timestamp &rhs)
free function comparator for b_timestamp
Definition types.hpp:577
A BSON undefined value.
Definition types.hpp:249
bool operator!=(const b_undefined &lhs, const b_undefined &rhs)
free function comparator for b_undefined
Definition types.hpp:715
bool operator==(const b_undefined &, const b_undefined &)
free function comparator for b_undefined
Definition types.hpp:258
Provides <type_traits>-related polyfills for internal use.
Declares entities used to represent BSON types.