MongoDB C++ Driver  mongocxx-3.9.0
All Classes Namespaces Functions Typedefs Enumerations Enumerator Friends Pages
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/array/view.hpp>
21 #include <bsoncxx/decimal128.hpp>
22 #include <bsoncxx/document/view.hpp>
23 #include <bsoncxx/oid.hpp>
24 #include <bsoncxx/stdx/string_view.hpp>
25 #include <bsoncxx/stdx/type_traits.hpp>
26 
27 #include <bsoncxx/config/prelude.hpp>
28 
29 #pragma push_macro("BSONCXX_ENUM")
30 #undef BSONCXX_ENUM
31 
32 #if defined(__clang__)
33 #pragma clang diagnostic push
34 #pragma clang diagnostic ignored "-Wfloat-equal"
35 #elif defined(__GNUC__)
36 #pragma GCC diagnostic push
37 #pragma GCC diagnostic ignored "-Wfloat-equal"
38 #endif
39 
40 namespace bsoncxx {
41 inline namespace v_noabi {
50 enum class type : std::uint8_t {
51 #define BSONCXX_ENUM(name, val) k_##name = val,
52 #include <bsoncxx/enums/type.hpp>
53 #undef BSONCXX_ENUM
54  k_utf8 = 0x02,
55 };
56 
70 enum class binary_sub_type : std::uint8_t {
71 #define BSONCXX_ENUM(name, val) k_##name = val,
72 #include <bsoncxx/enums/binary_sub_type.hpp>
73 #undef BSONCXX_ENUM
74 };
75 
84 BSONCXX_API std::string BSONCXX_CALL to_string(type rhs);
85 
94 BSONCXX_API std::string BSONCXX_CALL to_string(binary_sub_type rhs);
95 
96 namespace types {
97 
101 struct BSONCXX_API b_double {
102  static constexpr auto type_id = type::k_double;
103 
104  double value;
105 
109  BSONCXX_INLINE operator double() const {
110  return value;
111  }
112 };
113 
119 BSONCXX_INLINE bool operator==(const b_double& lhs, const b_double& rhs) {
120  return lhs.value == rhs.value;
121 }
122 
126 struct BSONCXX_API b_string {
127  static constexpr auto type_id = type::k_string;
128 
135  template <typename T, detail::requires_not_t<int, detail::is_alike<b_string, T>> = 0>
136  BSONCXX_INLINE explicit b_string(T&& t) : value(std::forward<T>(t)) {}
137 
138  stdx::string_view value;
139 
143  BSONCXX_INLINE operator stdx::string_view() const {
144  return value;
145  }
146 };
147 
153 BSONCXX_INLINE bool operator==(const b_string& lhs, const b_string& rhs) {
154  return lhs.value == rhs.value;
155 }
156 
162 BSONCXX_DEPRECATED typedef b_string b_utf8;
163 
167 struct BSONCXX_API b_document {
168  static constexpr auto type_id = type::k_document;
169 
171 
175  BSONCXX_INLINE operator document::view() const {
176  return value;
177  }
178 
182  BSONCXX_INLINE document::view view() {
183  return value;
184  }
185 };
186 
192 BSONCXX_INLINE bool operator==(const b_document& lhs, const b_document& rhs) {
193  return lhs.value == rhs.value;
194 }
195 
199 struct BSONCXX_API b_array {
200  static constexpr auto type_id = type::k_array;
201 
203 
207  BSONCXX_INLINE operator array::view() const {
208  return value;
209  }
210 };
211 
217 BSONCXX_INLINE bool operator==(const b_array& lhs, const b_array& rhs) {
218  return lhs.value == rhs.value;
219 }
220 
224 struct BSONCXX_API b_binary {
225  static constexpr auto type_id = type::k_binary;
226 
227  binary_sub_type sub_type;
228  uint32_t size;
229  const uint8_t* bytes;
230 };
231 
237 BSONCXX_INLINE bool operator==(const b_binary& lhs, const b_binary& rhs) {
238  return lhs.sub_type == rhs.sub_type && lhs.size == rhs.size &&
239  (!lhs.size || (std::memcmp(lhs.bytes, rhs.bytes, lhs.size) == 0));
240 }
241 
248 struct BSONCXX_API b_undefined {
249  static constexpr auto type_id = type::k_undefined;
250 };
251 
257 BSONCXX_INLINE bool operator==(const b_undefined&, const b_undefined&) {
258  return true;
259 }
260 
264 struct BSONCXX_API b_oid {
265  static constexpr auto type_id = type::k_oid;
266 
267  oid value;
268 };
269 
275 BSONCXX_INLINE bool operator==(const b_oid& lhs, const b_oid& rhs) {
276  return lhs.value == rhs.value;
277 }
278 
282 struct BSONCXX_API b_bool {
283  static constexpr auto type_id = type::k_bool;
284 
285  bool value;
286 
290  BSONCXX_INLINE operator bool() const {
291  return value;
292  }
293 };
294 
300 BSONCXX_INLINE bool operator==(const b_bool& lhs, const b_bool& rhs) {
301  return lhs.value == rhs.value;
302 }
303 
307 struct BSONCXX_API b_date {
308  static constexpr auto type_id = type::k_date;
309 
316  BSONCXX_INLINE
317  explicit b_date(std::chrono::milliseconds value) : value(value) {}
318 
325  BSONCXX_INLINE
326  explicit b_date(const std::chrono::system_clock::time_point& tp)
327  : value(std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch())) {}
328 
329  std::chrono::milliseconds value;
330 
334  BSONCXX_INLINE operator int64_t() const {
335  return value.count();
336  }
337 
341  BSONCXX_INLINE int64_t to_int64() const {
342  return value.count();
343  }
344 
348  BSONCXX_INLINE 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 
359 BSONCXX_INLINE bool operator==(const b_date& lhs, const b_date& rhs) {
360  return lhs.value == rhs.value;
361 }
362 
366 struct BSONCXX_API b_null {
367  static constexpr auto type_id = type::k_null;
368 };
369 
375 BSONCXX_INLINE bool operator==(const b_null&, const b_null&) {
376  return true;
377 }
378 
382 struct BSONCXX_API b_regex {
383  static constexpr auto type_id = type::k_regex;
384 
394  template <typename T,
395  typename U = stdx::string_view,
396  detail::requires_not_t<int, detail::is_alike<b_regex, T>> = 0>
397  BSONCXX_INLINE explicit b_regex(T&& regex, U&& options = U{})
398  : regex(std::forward<T>(regex)), options(std::forward<U>(options)) {}
399 
400  stdx::string_view regex;
401  stdx::string_view options;
402 };
403 
409 BSONCXX_INLINE bool operator==(const b_regex& lhs, const b_regex& rhs) {
410  return lhs.regex == rhs.regex && lhs.options == rhs.options;
411 }
412 
419 struct BSONCXX_API b_dbpointer {
420  static constexpr auto type_id = type::k_dbpointer;
421 
422  stdx::string_view collection;
423  oid value;
424 };
425 
431 BSONCXX_INLINE bool operator==(const b_dbpointer& lhs, const b_dbpointer& rhs) {
432  return lhs.collection == rhs.collection && lhs.value == rhs.value;
433 }
434 
438 struct BSONCXX_API b_code {
439  static constexpr auto type_id = type::k_code;
440 
447  template <typename T, detail::requires_not_t<int, detail::is_alike<b_code, T>> = 0>
448  BSONCXX_INLINE explicit b_code(T&& t) : code(std::forward<T>(t)) {}
449 
450  stdx::string_view code;
451 
455  BSONCXX_INLINE operator stdx::string_view() const {
456  return code;
457  }
458 };
459 
465 BSONCXX_INLINE bool operator==(const b_code& lhs, const b_code& rhs) {
466  return lhs.code == rhs.code;
467 }
468 
475 struct BSONCXX_API b_symbol {
476  static constexpr auto type_id = type::k_symbol;
477 
484  template <typename T, detail::requires_not_t<int, detail::is_alike<b_symbol, T>> = 0>
485  BSONCXX_INLINE explicit b_symbol(T&& t) : symbol(std::forward<T>(t)) {}
486 
487  stdx::string_view symbol;
488 
492  BSONCXX_INLINE operator stdx::string_view() const {
493  return symbol;
494  }
495 };
496 
502 BSONCXX_INLINE bool operator==(const b_symbol& lhs, const b_symbol& rhs) {
503  return lhs.symbol == rhs.symbol;
504 }
505 
509 struct BSONCXX_API b_codewscope {
510  static constexpr auto type_id = type::k_codewscope;
511 
521  template <typename T,
522  typename U,
523  detail::requires_not_t<int, detail::is_alike<b_codewscope, T>> = 0>
524  BSONCXX_INLINE explicit b_codewscope(T&& code, U&& scope)
525  : code(std::forward<T>(code)), scope(std::forward<U>(scope)) {}
526 
527  stdx::string_view code;
528  document::view scope;
529 };
530 
536 BSONCXX_INLINE bool operator==(const b_codewscope& lhs, const b_codewscope& rhs) {
537  return lhs.code == rhs.code && lhs.scope == rhs.scope;
538 }
539 
543 struct BSONCXX_API b_int32 {
544  static constexpr auto type_id = type::k_int32;
545 
546  int32_t value;
547 
551  BSONCXX_INLINE operator int32_t() const {
552  return value;
553  }
554 };
555 
561 BSONCXX_INLINE bool operator==(const b_int32& lhs, const b_int32& rhs) {
562  return lhs.value == rhs.value;
563 }
564 
568 struct BSONCXX_API b_timestamp {
569  static constexpr auto type_id = type::k_timestamp;
570 
571  uint32_t increment;
572  uint32_t timestamp;
573 };
574 
580 BSONCXX_INLINE bool operator==(const b_timestamp& lhs, const b_timestamp& rhs) {
581  return lhs.increment == rhs.increment && lhs.timestamp == rhs.timestamp;
582 }
583 
587 struct BSONCXX_API b_int64 {
588  static constexpr auto type_id = type::k_int64;
589 
590  int64_t value;
591 
595  BSONCXX_INLINE operator int64_t() const {
596  return value;
597  }
598 };
599 
605 BSONCXX_INLINE bool operator==(const b_int64& lhs, const b_int64& rhs) {
606  return lhs.value == rhs.value;
607 }
608 
612 struct BSONCXX_API b_decimal128 {
613  static constexpr auto type_id = type::k_decimal128;
614 
616 
623  template <typename T, detail::requires_not_t<int, detail::is_alike<b_decimal128, T>> = 0>
624  BSONCXX_INLINE explicit b_decimal128(T&& t) : value(std::forward<T>(t)) {}
625 };
626 
632 BSONCXX_INLINE bool operator==(const b_decimal128& lhs, const b_decimal128& rhs) {
633  return lhs.value == rhs.value;
634 }
635 
639 struct BSONCXX_API b_minkey {
640  static constexpr auto type_id = type::k_minkey;
641 };
642 
648 BSONCXX_INLINE bool operator==(const b_minkey&, const b_minkey&) {
649  return true;
650 }
651 
655 struct BSONCXX_API b_maxkey {
656  static constexpr auto type_id = type::k_maxkey;
657 };
658 
664 BSONCXX_INLINE bool operator==(const b_maxkey&, const b_maxkey&) {
665  return true;
666 }
667 
668 #define BSONCXX_ENUM(name, val) \
669  BSONCXX_INLINE bool operator!=(const b_##name& lhs, const b_##name& rhs) { \
670  return !(lhs == rhs); \
671  }
672 #include <bsoncxx/enums/type.hpp>
673 #undef BSONCXX_ENUM
674 
675 } // namespace types
676 } // namespace v_noabi
677 } // namespace bsoncxx
678 
679 #if defined(__clang__)
680 #pragma clang diagnostic pop
681 #elif defined(__GNUC__)
682 #pragma GCC diagnostic pop
683 #endif
684 
685 #ifdef BSONCXX_ENUM
686 static_assert(false, "BSONCXX_ENUM must be undef'ed");
687 #endif
688 #pragma pop_macro("BSONCXX_ENUM")
689 
690 #include <bsoncxx/config/postlude.hpp>
A read-only, non-owning view of a BSON document.
Definition: view.hpp:39
Represents an IEEE 754-2008 BSON Decimal128 value in a platform-independent way.
Definition: decimal128.hpp:29
A read-only, non-owning view of a BSON document.
Definition: view.hpp:33
Represents a MongoDB ObjectId.
Definition: oid.hpp:37
A view-only variant that can contain any BSON type.
Definition: view.hpp:44
binary_sub_type
An enumeration of each BSON binary sub type.
Definition: types.hpp:70
std::string to_string(type rhs)
Returns a stringification of the given type.
type
An enumeration of each BSON type.
Definition: types.hpp:50
The top-level namespace for bsoncxx library entities.
Definition: element.hpp:24
A BSON array value.
Definition: types.hpp:199
bool operator==(const b_array &lhs, const b_array &rhs)
free function comparator for b_array
Definition: types.hpp:217
A BSON binary data value.
Definition: types.hpp:224
bool operator==(const b_binary &lhs, const b_binary &rhs)
free function comparator for b_binary
Definition: types.hpp:237
A BSON boolean value.
Definition: types.hpp:282
bool operator==(const b_bool &lhs, const b_bool &rhs)
free function comparator for b_bool
Definition: types.hpp:300
A BSON JavaScript code value.
Definition: types.hpp:438
bool operator==(const b_code &lhs, const b_code &rhs)
free function comparator for b_code
Definition: types.hpp:465
b_code(T &&t)
Constructor for b_code.
Definition: types.hpp:448
A BSON JavaScript code with scope value.
Definition: types.hpp:509
bool operator==(const b_codewscope &lhs, const b_codewscope &rhs)
free function comparator for b_codewscope
Definition: types.hpp:536
b_codewscope(T &&code, U &&scope)
Constructor for b_codewscope.
Definition: types.hpp:524
A BSON date value.
Definition: types.hpp:307
bool operator==(const b_date &lhs, const b_date &rhs)
free function comparator for b_date
Definition: types.hpp:359
b_date(const std::chrono::system_clock::time_point &tp)
Constructor for b_date.
Definition: types.hpp:326
int64_t to_int64() const
Manually convert this b_date to an int64_t.
Definition: types.hpp:341
b_date(std::chrono::milliseconds value)
Constructor for b_date.
Definition: types.hpp:317
A BSON DBPointer value.
Definition: types.hpp:419
bool operator==(const b_dbpointer &lhs, const b_dbpointer &rhs)
free function comparator for b_dbpointer
Definition: types.hpp:431
A BSON Decimal128 value.
Definition: types.hpp:612
bool operator==(const b_decimal128 &lhs, const b_decimal128 &rhs)
free function comparator for b_decimal128
Definition: types.hpp:632
b_decimal128(T &&t)
Constructor for b_decimal128.
Definition: types.hpp:624
A BSON document value.
Definition: types.hpp:167
document::view view()
Returns an unwrapped document::view.
Definition: types.hpp:182
bool operator==(const b_document &lhs, const b_document &rhs)
free function comparator for b_document
Definition: types.hpp:192
A BSON double value.
Definition: types.hpp:101
bool operator==(const b_double &lhs, const b_double &rhs)
free function comparator for b_double
Definition: types.hpp:119
A BSON signed 32-bit integer value.
Definition: types.hpp:543
bool operator==(const b_int32 &lhs, const b_int32 &rhs)
free function comparator for b_int32
Definition: types.hpp:561
A BSON 64-bit signed integer value.
Definition: types.hpp:587
bool operator==(const b_int64 &lhs, const b_int64 &rhs)
free function comparator for b_int64
Definition: types.hpp:605
A BSON max-key value.
Definition: types.hpp:655
bool operator==(const b_maxkey &, const b_maxkey &)
free function comparator for b_maxkey
Definition: types.hpp:664
A BSON min-key value.
Definition: types.hpp:639
bool operator==(const b_minkey &, const b_minkey &)
free function comparator for b_minkey
Definition: types.hpp:648
A BSON null value.
Definition: types.hpp:366
bool operator==(const b_null &, const b_null &)
free function comparator for b_null
Definition: types.hpp:375
A BSON ObjectId value.
Definition: types.hpp:264
bool operator==(const b_oid &lhs, const b_oid &rhs)
free function comparator for b_oid
Definition: types.hpp:275
A BSON regex value.
Definition: types.hpp:382
bool operator==(const b_regex &lhs, const b_regex &rhs)
free function comparator for b_regex
Definition: types.hpp:409
b_regex(T &&regex, U &&options=U{})
Constructor for b_regex.
Definition: types.hpp:397
A BSON UTF-8 encoded string value.
Definition: types.hpp:126
b_string(T &&t)
Constructor for b_string.
Definition: types.hpp:136
bool operator==(const b_string &lhs, const b_string &rhs)
free function comparator for b_string
Definition: types.hpp:153
A BSON Symbol value.
Definition: types.hpp:475
b_symbol(T &&t)
Constructor for b_symbol.
Definition: types.hpp:485
bool operator==(const b_symbol &lhs, const b_symbol &rhs)
free function comparator for b_symbol
Definition: types.hpp:502
A BSON replication timestamp value.
Definition: types.hpp:568
bool operator==(const b_timestamp &lhs, const b_timestamp &rhs)
free function comparator for b_timestamp
Definition: types.hpp:580
A BSON undefined value.
Definition: types.hpp:248
bool operator==(const b_undefined &, const b_undefined &)
free function comparator for b_undefined
Definition: types.hpp:257