MongoDB C++ Driver mongocxx-3.10.1
Loading...
Searching...
No Matches
types.hpp
1// Copyright 2014 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
20#include <bsoncxx/types-fwd.hpp>
21
22#include <bsoncxx/array/view.hpp>
23#include <bsoncxx/decimal128.hpp>
24#include <bsoncxx/document/view.hpp>
25#include <bsoncxx/oid.hpp>
26#include <bsoncxx/stdx/string_view.hpp>
27#include <bsoncxx/stdx/type_traits.hpp>
28
29#include <bsoncxx/config/prelude.hpp>
30
31#pragma push_macro("BSONCXX_ENUM")
32#undef BSONCXX_ENUM
33
34BSONCXX_PUSH_WARNINGS();
35BSONCXX_DISABLE_WARNING(GNU("-Wfloat-equal"));
36
37namespace bsoncxx {
38namespace v_noabi {
39
48enum class type : std::uint8_t {
49#define BSONCXX_ENUM(name, val) k_##name = val,
50#include <bsoncxx/enums/type.hpp>
51#undef BSONCXX_ENUM
52 k_utf8 = 0x02,
53};
54
68enum class binary_sub_type : std::uint8_t {
69#define BSONCXX_ENUM(name, val) k_##name = val,
70#include <bsoncxx/enums/binary_sub_type.hpp>
71#undef BSONCXX_ENUM
72};
73
82BSONCXX_API std::string BSONCXX_CALL to_string(type rhs);
83
92BSONCXX_API std::string BSONCXX_CALL to_string(binary_sub_type rhs);
93
94namespace types {
95
99struct b_double {
100 static constexpr auto type_id = type::k_double;
101
102 double value;
103
107 BSONCXX_INLINE operator double() const {
108 return value;
109 }
110};
111
117BSONCXX_INLINE bool operator==(const b_double& lhs, const b_double& rhs) {
118 return lhs.value == rhs.value;
119}
120
124struct b_string {
125 static constexpr auto type_id = type::k_string;
126
133 template <typename T, detail::requires_not_t<int, detail::is_alike<b_string, T>> = 0>
134 BSONCXX_INLINE explicit b_string(T&& t) : value(std::forward<T>(t)) {}
135
136 stdx::string_view value;
137
141 BSONCXX_INLINE operator stdx::string_view() const {
142 return value;
143 }
144};
145
151BSONCXX_INLINE bool operator==(const b_string& lhs, const b_string& rhs) {
152 return lhs.value == rhs.value;
153}
154
160BSONCXX_DEPRECATED typedef b_string b_utf8;
161
166 static constexpr auto type_id = type::k_document;
167
169
173 BSONCXX_INLINE operator document::view() const {
174 return value;
175 }
176
180 BSONCXX_INLINE document::view view() {
181 return value;
182 }
183};
184
190BSONCXX_INLINE bool operator==(const b_document& lhs, const b_document& rhs) {
191 return lhs.value == rhs.value;
192}
193
197struct b_array {
198 static constexpr auto type_id = type::k_array;
199
201
205 BSONCXX_INLINE operator array::view() const {
206 return value;
207 }
208};
209
215BSONCXX_INLINE bool operator==(const b_array& lhs, const b_array& rhs) {
216 return lhs.value == rhs.value;
217}
218
222struct b_binary {
223 static constexpr auto type_id = type::k_binary;
224
225 binary_sub_type sub_type;
226 uint32_t size;
227 const uint8_t* bytes;
228};
229
235BSONCXX_INLINE bool operator==(const b_binary& lhs, const b_binary& rhs) {
236 return lhs.sub_type == rhs.sub_type && lhs.size == rhs.size &&
237 (!lhs.size || (std::memcmp(lhs.bytes, rhs.bytes, lhs.size) == 0));
238}
239
247 static constexpr auto type_id = type::k_undefined;
248};
249
255BSONCXX_INLINE bool operator==(const b_undefined&, const b_undefined&) {
256 return true;
257}
258
262struct b_oid {
263 static constexpr auto type_id = type::k_oid;
264
265 oid value;
266};
267
273BSONCXX_INLINE bool operator==(const b_oid& lhs, const b_oid& rhs) {
274 return lhs.value == rhs.value;
275}
276
280struct b_bool {
281 static constexpr auto type_id = type::k_bool;
282
283 bool value;
284
288 BSONCXX_INLINE operator bool() const {
289 return value;
290 }
291};
292
298BSONCXX_INLINE bool operator==(const b_bool& lhs, const b_bool& rhs) {
299 return lhs.value == rhs.value;
300}
301
305struct b_date {
306 static constexpr auto type_id = type::k_date;
307
314 BSONCXX_INLINE
315 explicit b_date(std::chrono::milliseconds value) : value(value) {}
316
323 BSONCXX_INLINE
324 explicit b_date(const std::chrono::system_clock::time_point& tp)
325 : value(std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch())) {}
326
327 std::chrono::milliseconds value;
328
332 BSONCXX_INLINE operator int64_t() const {
333 return value.count();
334 }
335
339 BSONCXX_INLINE int64_t to_int64() const {
340 return value.count();
341 }
342
346 BSONCXX_INLINE operator std::chrono::system_clock::time_point() const {
347 return std::chrono::system_clock::time_point(
348 std::chrono::duration_cast<std::chrono::system_clock::duration>(value));
349 }
350};
351
357BSONCXX_INLINE bool operator==(const b_date& lhs, const b_date& rhs) {
358 return lhs.value == rhs.value;
359}
360
364struct b_null {
365 static constexpr auto type_id = type::k_null;
366};
367
373BSONCXX_INLINE bool operator==(const b_null&, const b_null&) {
374 return true;
375}
376
380struct b_regex {
381 static constexpr auto type_id = type::k_regex;
382
392 template <typename T,
393 typename U = stdx::string_view,
394 detail::requires_not_t<int, detail::is_alike<b_regex, T>> = 0>
395 BSONCXX_INLINE explicit b_regex(T&& regex, U&& options = U{})
396 : regex(std::forward<T>(regex)), options(std::forward<U>(options)) {}
397
398 stdx::string_view regex;
399 stdx::string_view options;
400};
401
407BSONCXX_INLINE bool operator==(const b_regex& lhs, const b_regex& rhs) {
408 return lhs.regex == rhs.regex && lhs.options == rhs.options;
409}
410
418 static constexpr auto type_id = type::k_dbpointer;
419
420 stdx::string_view collection;
421 oid value;
422};
423
429BSONCXX_INLINE 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 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 BSONCXX_INLINE explicit b_code(T&& t) : code(std::forward<T>(t)) {}
447
448 stdx::string_view code;
449
453 BSONCXX_INLINE operator stdx::string_view() const {
454 return code;
455 }
456};
457
463BSONCXX_INLINE bool operator==(const b_code& lhs, const b_code& rhs) {
464 return lhs.code == rhs.code;
465}
466
473struct b_symbol {
474 static constexpr auto type_id = type::k_symbol;
475
482 template <typename T, detail::requires_not_t<int, detail::is_alike<b_symbol, T>> = 0>
483 BSONCXX_INLINE explicit b_symbol(T&& t) : symbol(std::forward<T>(t)) {}
484
485 stdx::string_view symbol;
486
490 BSONCXX_INLINE operator stdx::string_view() const {
491 return symbol;
492 }
493};
494
500BSONCXX_INLINE bool operator==(const b_symbol& lhs, const b_symbol& rhs) {
501 return lhs.symbol == rhs.symbol;
502}
503
508 static constexpr auto type_id = type::k_codewscope;
509
519 template <typename T,
520 typename U,
521 detail::requires_not_t<int, detail::is_alike<b_codewscope, T>> = 0>
522 BSONCXX_INLINE explicit b_codewscope(T&& code, U&& scope)
523 : code(std::forward<T>(code)), scope(std::forward<U>(scope)) {}
524
525 stdx::string_view code;
526 document::view scope;
527};
528
534BSONCXX_INLINE bool operator==(const b_codewscope& lhs, const b_codewscope& rhs) {
535 return lhs.code == rhs.code && lhs.scope == rhs.scope;
536}
537
541struct b_int32 {
542 static constexpr auto type_id = type::k_int32;
543
544 int32_t value;
545
549 BSONCXX_INLINE operator int32_t() const {
550 return value;
551 }
552};
553
559BSONCXX_INLINE bool operator==(const b_int32& lhs, const b_int32& rhs) {
560 return lhs.value == rhs.value;
561}
562
567 static constexpr auto type_id = type::k_timestamp;
568
569 uint32_t increment;
570 uint32_t timestamp;
571};
572
578BSONCXX_INLINE bool operator==(const b_timestamp& lhs, const b_timestamp& rhs) {
579 return lhs.increment == rhs.increment && lhs.timestamp == rhs.timestamp;
580}
581
585struct b_int64 {
586 static constexpr auto type_id = type::k_int64;
587
588 int64_t value;
589
593 BSONCXX_INLINE operator int64_t() const {
594 return value;
595 }
596};
597
603BSONCXX_INLINE bool operator==(const b_int64& lhs, const b_int64& rhs) {
604 return lhs.value == rhs.value;
605}
606
611 static constexpr auto type_id = type::k_decimal128;
612
614
621 template <typename T, detail::requires_not_t<int, detail::is_alike<b_decimal128, T>> = 0>
622 BSONCXX_INLINE explicit b_decimal128(T&& t) : value(std::forward<T>(t)) {}
623};
624
630BSONCXX_INLINE bool operator==(const b_decimal128& lhs, const b_decimal128& rhs) {
631 return lhs.value == rhs.value;
632}
633
637struct b_minkey {
638 static constexpr auto type_id = type::k_minkey;
639};
640
646BSONCXX_INLINE bool operator==(const b_minkey&, const b_minkey&) {
647 return true;
648}
649
653struct b_maxkey {
654 static constexpr auto type_id = type::k_maxkey;
655};
656
662BSONCXX_INLINE bool operator==(const b_maxkey&, const b_maxkey&) {
663 return true;
664}
665
666#define BSONCXX_ENUM(name, val) \
667 BSONCXX_INLINE bool operator!=(const b_##name& lhs, const b_##name& rhs) { \
668 return !(lhs == rhs); \
669 }
670#include <bsoncxx/enums/type.hpp>
671#undef BSONCXX_ENUM
672
673} // namespace types
674} // namespace v_noabi
675} // namespace bsoncxx
676
677BSONCXX_POP_WARNINGS();
678
679namespace bsoncxx {
680
681using ::bsoncxx::v_noabi::to_string;
682
683} // namespace bsoncxx
684
685namespace bsoncxx {
686namespace types {
687
688using ::bsoncxx::v_noabi::types::b_utf8; // Deprecated.
689
690using ::bsoncxx::v_noabi::types::operator==;
691using ::bsoncxx::v_noabi::types::operator!=;
692
693} // namespace types
694} // namespace bsoncxx
695
696#ifdef BSONCXX_ENUM
697static_assert(false, "BSONCXX_ENUM must be undef'ed");
698#endif
699#pragma pop_macro("BSONCXX_ENUM")
700
701#include <bsoncxx/config/postlude.hpp>
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.
Definition oid.hpp:40
A view-only variant that can contain any BSON type.
Definition view.hpp:41
binary_sub_type
An enumeration of each BSON binary sub type.
Definition types.hpp:68
std::string to_string(type rhs)
Returns a stringification of the given type.
type
An enumeration of each BSON type.
Definition types.hpp:48
The top-level namespace for bsoncxx library entities.
Definition element-fwd.hpp:19
A BSON array value.
Definition types.hpp:197
bool operator==(const b_array &lhs, const b_array &rhs)
free function comparator for b_array
Definition types.hpp:215
A BSON binary data value.
Definition types.hpp:222
bool operator==(const b_binary &lhs, const b_binary &rhs)
free function comparator for b_binary
Definition types.hpp:235
A BSON boolean value.
Definition types.hpp:280
bool operator==(const b_bool &lhs, const b_bool &rhs)
free function comparator for b_bool
Definition types.hpp:298
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
b_code(T &&t)
Constructor for b_code.
Definition types.hpp:446
A BSON JavaScript code with scope value.
Definition types.hpp:507
bool operator==(const b_codewscope &lhs, const b_codewscope &rhs)
free function comparator for b_codewscope
Definition types.hpp:534
b_codewscope(T &&code, U &&scope)
Constructor for b_codewscope.
Definition types.hpp:522
A BSON date value.
Definition types.hpp:305
bool operator==(const b_date &lhs, const b_date &rhs)
free function comparator for b_date
Definition types.hpp:357
b_date(const std::chrono::system_clock::time_point &tp)
Constructor for b_date.
Definition types.hpp:324
int64_t to_int64() const
Manually convert this b_date to an int64_t.
Definition types.hpp:339
b_date(std::chrono::milliseconds value)
Constructor for b_date.
Definition types.hpp:315
A BSON DBPointer 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
A BSON Decimal128 value.
Definition types.hpp:610
bool operator==(const b_decimal128 &lhs, const b_decimal128 &rhs)
free function comparator for b_decimal128
Definition types.hpp:630
b_decimal128(T &&t)
Constructor for b_decimal128.
Definition types.hpp:622
A BSON document value.
Definition types.hpp:165
document::view view()
Returns an unwrapped document::view.
Definition types.hpp:180
bool operator==(const b_document &lhs, const b_document &rhs)
free function comparator for b_document
Definition types.hpp:190
A BSON double value.
Definition types.hpp:99
bool operator==(const b_double &lhs, const b_double &rhs)
free function comparator for b_double
Definition types.hpp:117
A BSON signed 32-bit integer value.
Definition types.hpp:541
bool operator==(const b_int32 &lhs, const b_int32 &rhs)
free function comparator for b_int32
Definition types.hpp:559
A BSON 64-bit signed integer value.
Definition types.hpp:585
bool operator==(const b_int64 &lhs, const b_int64 &rhs)
free function comparator for b_int64
Definition types.hpp:603
A BSON max-key value.
Definition types.hpp:653
bool operator==(const b_maxkey &, const b_maxkey &)
free function comparator for b_maxkey
Definition types.hpp:662
A BSON min-key value.
Definition types.hpp:637
bool operator==(const b_minkey &, const b_minkey &)
free function comparator for b_minkey
Definition types.hpp:646
A BSON null value.
Definition types.hpp:364
bool operator==(const b_null &, const b_null &)
free function comparator for b_null
Definition types.hpp:373
A BSON ObjectId value.
Definition types.hpp:262
bool operator==(const b_oid &lhs, const b_oid &rhs)
free function comparator for b_oid
Definition types.hpp:273
A BSON regex value.
Definition types.hpp:380
bool operator==(const b_regex &lhs, const b_regex &rhs)
free function comparator for b_regex
Definition types.hpp:407
b_regex(T &&regex, U &&options=U{})
Constructor for b_regex.
Definition types.hpp:395
A BSON UTF-8 encoded string value.
Definition types.hpp:124
b_string(T &&t)
Constructor for b_string.
Definition types.hpp:134
bool operator==(const b_string &lhs, const b_string &rhs)
free function comparator for b_string
Definition types.hpp:151
A BSON Symbol value.
Definition types.hpp:473
b_symbol(T &&t)
Constructor for b_symbol.
Definition types.hpp:483
bool operator==(const b_symbol &lhs, const b_symbol &rhs)
free function comparator for b_symbol
Definition types.hpp:500
A BSON replication timestamp value.
Definition types.hpp:566
bool operator==(const b_timestamp &lhs, const b_timestamp &rhs)
free function comparator for b_timestamp
Definition types.hpp:578
A BSON undefined value.
Definition types.hpp:246
bool operator==(const b_undefined &, const b_undefined &)
free function comparator for b_undefined
Definition types.hpp:255