MongoDB C++ Driver current
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#if defined(__clang__)
17#pragma clang diagnostic push
18#pragma clang diagnostic ignored "-Wfloat-equal"
19#elif defined(__GNUC__)
20#pragma GCC diagnostic push
21#pragma GCC diagnostic ignored "-Wfloat-equal"
22#endif
23
24#include <chrono>
25#include <cstring>
26
27#include <bsoncxx/array/view.hpp>
28#include <bsoncxx/decimal128.hpp>
29#include <bsoncxx/document/view.hpp>
30#include <bsoncxx/oid.hpp>
31#include <bsoncxx/stdx/string_view.hpp>
32
33#include <bsoncxx/config/prelude.hpp>
34
35namespace bsoncxx {
36BSONCXX_INLINE_NAMESPACE_BEGIN
37
46enum class type : std::uint8_t {
47#define BSONCXX_ENUM(name, val) k_##name = val,
48#include <bsoncxx/enums/type.hpp>
49#undef BSONCXX_ENUM
50 k_utf8 = 0x02,
51};
52
66enum class binary_sub_type : std::uint8_t {
67#define BSONCXX_ENUM(name, val) k_##name = val,
68#include <bsoncxx/enums/binary_sub_type.hpp>
69#undef BSONCXX_ENUM
70};
71
80BSONCXX_API std::string BSONCXX_CALL to_string(type rhs);
81
90BSONCXX_API std::string BSONCXX_CALL to_string(binary_sub_type rhs);
91
92namespace types {
93
97struct BSONCXX_API b_double {
98 static constexpr auto type_id = type::k_double;
99
100 double value;
101
105 BSONCXX_INLINE operator double() const {
106 return value;
107 }
108};
109
115BSONCXX_INLINE bool operator==(const b_double& lhs, const b_double& rhs) {
116 return lhs.value == rhs.value;
117}
118
122struct BSONCXX_API b_string {
123 static constexpr auto type_id = type::k_string;
124
131 template <typename T,
132 typename std::enable_if<!std::is_same<b_string, typename std::decay<T>::type>::value,
133 int>::type = 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
165struct BSONCXX_API b_document {
166 static constexpr auto type_id = type::k_document;
167
168 document::view value;
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 BSONCXX_API b_array {
198 static constexpr auto type_id = type::k_array;
199
200 array::view value;
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 BSONCXX_API 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
246struct BSONCXX_API b_undefined {
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 BSONCXX_API 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 BSONCXX_API 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 BSONCXX_API 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 BSONCXX_API 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 BSONCXX_API b_regex {
381 static constexpr auto type_id = type::k_regex;
382
392 template <typename T,
393 typename U = stdx::string_view,
394 typename std::enable_if<!std::is_same<b_regex, typename std::decay<T>::type>::value,
395 int>::type = 0>
396 BSONCXX_INLINE 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
408BSONCXX_INLINE bool operator==(const b_regex& lhs, const b_regex& rhs) {
409 return lhs.regex == rhs.regex && lhs.options == rhs.options;
410}
411
418struct BSONCXX_API b_dbpointer {
419 static constexpr auto type_id = type::k_dbpointer;
420
421 stdx::string_view collection;
422 oid value;
423};
424
430BSONCXX_INLINE bool operator==(const b_dbpointer& lhs, const b_dbpointer& rhs) {
431 return lhs.collection == rhs.collection && lhs.value == rhs.value;
432}
433
437struct BSONCXX_API b_code {
438 static constexpr auto type_id = type::k_code;
439
446 template <typename T,
447 typename std::enable_if<!std::is_same<b_code, typename std::decay<T>::type>::value,
448 int>::type = 0>
449 BSONCXX_INLINE explicit b_code(T&& t) : code(std::forward<T>(t)) {}
450
451 stdx::string_view code;
452
456 BSONCXX_INLINE operator stdx::string_view() const {
457 return code;
458 }
459};
460
466BSONCXX_INLINE bool operator==(const b_code& lhs, const b_code& rhs) {
467 return lhs.code == rhs.code;
468}
469
476struct BSONCXX_API b_symbol {
477 static constexpr auto type_id = type::k_symbol;
478
485 template <typename T,
486 typename std::enable_if<!std::is_same<b_symbol, typename std::decay<T>::type>::value,
487 int>::type = 0>
488 BSONCXX_INLINE explicit b_symbol(T&& t) : symbol(std::forward<T>(t)) {}
489
490 stdx::string_view symbol;
491
495 BSONCXX_INLINE operator stdx::string_view() const {
496 return symbol;
497 }
498};
499
505BSONCXX_INLINE bool operator==(const b_symbol& lhs, const b_symbol& rhs) {
506 return lhs.symbol == rhs.symbol;
507}
508
512struct BSONCXX_API b_codewscope {
513 static constexpr auto type_id = type::k_codewscope;
514
524 template <
525 typename T,
526 typename U,
527 typename std::enable_if<!std::is_same<b_codewscope, typename std::decay<T>::type>::value,
528 int>::type = 0>
529 BSONCXX_INLINE explicit b_codewscope(T&& code, U&& scope)
530 : code(std::forward<T>(code)), scope(std::forward<U>(scope)) {}
531
532 stdx::string_view code;
533 document::view scope;
534};
535
541BSONCXX_INLINE bool operator==(const b_codewscope& lhs, const b_codewscope& rhs) {
542 return lhs.code == rhs.code && lhs.scope == rhs.scope;
543}
544
548struct BSONCXX_API b_int32 {
549 static constexpr auto type_id = type::k_int32;
550
551 int32_t value;
552
556 BSONCXX_INLINE operator int32_t() const {
557 return value;
558 }
559};
560
566BSONCXX_INLINE bool operator==(const b_int32& lhs, const b_int32& rhs) {
567 return lhs.value == rhs.value;
568}
569
577struct BSONCXX_API b_timestamp {
578 static constexpr auto type_id = type::k_timestamp;
579
580 uint32_t increment;
581 uint32_t timestamp;
582};
583
589BSONCXX_INLINE bool operator==(const b_timestamp& lhs, const b_timestamp& rhs) {
590 return lhs.increment == rhs.increment && lhs.timestamp == rhs.timestamp;
591}
592
596struct BSONCXX_API b_int64 {
597 static constexpr auto type_id = type::k_int64;
598
599 int64_t value;
600
604 BSONCXX_INLINE operator int64_t() const {
605 return value;
606 }
607};
608
614BSONCXX_INLINE bool operator==(const b_int64& lhs, const b_int64& rhs) {
615 return lhs.value == rhs.value;
616}
617
621struct BSONCXX_API b_decimal128 {
622 static constexpr auto type_id = type::k_decimal128;
623
624 decimal128 value;
625
632 template <
633 typename T,
634 typename std::enable_if<!std::is_same<b_decimal128, typename std::decay<T>::type>::value,
635 int>::type = 0>
636 BSONCXX_INLINE explicit b_decimal128(T&& t) : value(std::forward<T>(t)) {}
637};
638
644BSONCXX_INLINE bool operator==(const b_decimal128& lhs, const b_decimal128& rhs) {
645 return lhs.value == rhs.value;
646}
647
651struct BSONCXX_API b_minkey {
652 static constexpr auto type_id = type::k_minkey;
653};
654
660BSONCXX_INLINE bool operator==(const b_minkey&, const b_minkey&) {
661 return true;
662}
663
667struct BSONCXX_API b_maxkey {
668 static constexpr auto type_id = type::k_maxkey;
669};
670
676BSONCXX_INLINE bool operator==(const b_maxkey&, const b_maxkey&) {
677 return true;
678}
679
680#define BSONCXX_ENUM(name, val) \
681 BSONCXX_INLINE bool operator!=(const b_##name& lhs, const b_##name& rhs) { \
682 return !(lhs == rhs); \
683 }
684#include <bsoncxx/enums/type.hpp>
685#undef BSONCXX_ENUM
686
687} // namespace types
688BSONCXX_INLINE_NAMESPACE_END
689} // namespace bsoncxx
690
691#include <bsoncxx/config/postlude.hpp>
692
693#if defined(__clang__)
694#pragma clang diagnostic pop
695#elif defined(__GNUC__)
696#pragma GCC diagnostic pop
697#endif
A read-only, non-owning view of a BSON document.
Definition: view.hpp:40
Represents an IEEE 754-2008 BSON Decimal128 value in a platform-independent way.
Definition: decimal128.hpp:30
A read-only, non-owning view of a BSON document.
Definition: view.hpp:33
Represents a MongoDB ObjectId.
Definition: oid.hpp:38
Top level namespace for MongoDB C++ BSON functionality.
Definition: element.hpp:24
type
An enumeration of each BSON type.
Definition: types.hpp:46
std::string to_string(type rhs)
Returns a stringification of the given type.
binary_sub_type
An enumeration of each BSON binary sub type.
Definition: types.hpp:66
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:437
bool operator==(const b_code &lhs, const b_code &rhs)
free function comparator for b_code
Definition: types.hpp:466
b_code(T &&t)
Constructor for b_code.
Definition: types.hpp:449
A BSON JavaScript code with scope value.
Definition: types.hpp:512
b_codewscope(T &&code, U &&scope)
Constructor for b_codewscope.
Definition: types.hpp:529
bool operator==(const b_codewscope &lhs, const b_codewscope &rhs)
free function comparator for b_codewscope
Definition: types.hpp:541
A BSON date value.
Definition: types.hpp:305
int64_t to_int64() const
Manually convert this b_date to an int64_t.
Definition: types.hpp:339
b_date(const std::chrono::system_clock::time_point &tp)
Constructor for b_date.
Definition: types.hpp:324
bool operator==(const b_date &lhs, const b_date &rhs)
free function comparator for b_date
Definition: types.hpp:357
b_date(std::chrono::milliseconds value)
Constructor for b_date.
Definition: types.hpp:315
A BSON DBPointer value.
Definition: types.hpp:418
bool operator==(const b_dbpointer &lhs, const b_dbpointer &rhs)
free function comparator for b_dbpointer
Definition: types.hpp:430
A BSON Decimal128 value.
Definition: types.hpp:621
bool operator==(const b_decimal128 &lhs, const b_decimal128 &rhs)
free function comparator for b_decimal128
Definition: types.hpp:644
b_decimal128(T &&t)
Constructor for b_decimal128.
Definition: types.hpp:636
A BSON document value.
Definition: types.hpp:165
bool operator==(const b_document &lhs, const b_document &rhs)
free function comparator for b_document
Definition: types.hpp:190
document::view view()
Returns an unwrapped document::view.
Definition: types.hpp:180
A BSON double value.
Definition: types.hpp:97
bool operator==(const b_double &lhs, const b_double &rhs)
free function comparator for b_double
Definition: types.hpp:115
A BSON signed 32-bit integer value.
Definition: types.hpp:548
bool operator==(const b_int32 &lhs, const b_int32 &rhs)
free function comparator for b_int32
Definition: types.hpp:566
A BSON 64-bit signed integer value.
Definition: types.hpp:596
bool operator==(const b_int64 &lhs, const b_int64 &rhs)
free function comparator for b_int64
Definition: types.hpp:614
A BSON max-key value.
Definition: types.hpp:667
bool operator==(const b_maxkey &, const b_maxkey &)
free function comparator for b_maxkey
Definition: types.hpp:676
A BSON min-key value.
Definition: types.hpp:651
bool operator==(const b_minkey &, const b_minkey &)
free function comparator for b_minkey
Definition: types.hpp:660
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: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:122
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:476
bool operator==(const b_symbol &lhs, const b_symbol &rhs)
free function comparator for b_symbol
Definition: types.hpp:505
b_symbol(T &&t)
Constructor for b_symbol.
Definition: types.hpp:488
A BSON replication timestamp value.
Definition: types.hpp:577
bool operator==(const b_timestamp &lhs, const b_timestamp &rhs)
free function comparator for b_timestamp
Definition: types.hpp:589
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