MongoDB C++ Driver  mongocxx-3.1.1
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 
26 #include <bsoncxx/config/prelude.hpp>
27 
28 namespace bsoncxx {
29 BSONCXX_INLINE_NAMESPACE_BEGIN
30 
39 enum class type : std::uint8_t {
40 #define BSONCXX_ENUM(name, val) k_##name = val,
41 #include <bsoncxx/enums/type.hpp>
42 #undef BSONCXX_ENUM
43 };
44 
54 enum class binary_sub_type : std::uint8_t {
55 #define BSONCXX_ENUM(name, val) k_##name = val,
56 #include <bsoncxx/enums/binary_sub_type.hpp>
57 #undef BSONCXX_ENUM
58 };
59 
68 BSONCXX_API std::string BSONCXX_CALL to_string(type rhs);
69 
78 BSONCXX_API std::string BSONCXX_CALL to_string(binary_sub_type rhs);
79 
80 namespace types {
81 
85 struct BSONCXX_API b_double {
86  static constexpr auto type_id = type::k_double;
87 
88  double value;
89 
93  BSONCXX_INLINE operator double() {
94  return value;
95  }
96 };
97 
103 BSONCXX_INLINE bool operator==(const b_double& lhs, const b_double& rhs) {
104  return lhs.value == rhs.value;
105 }
106 
110 struct BSONCXX_API b_utf8 {
111  static constexpr auto type_id = type::k_utf8;
112 
119  template <typename T,
120  typename std::enable_if<!std::is_same<b_utf8, typename std::decay<T>::type>::value,
121  int>::type = 0>
122  BSONCXX_INLINE explicit b_utf8(T&& t)
123  : value(std::forward<T>(t)) {
124  }
125 
126  stdx::string_view value;
127 
131  BSONCXX_INLINE operator stdx::string_view() {
132  return value;
133  }
134 };
135 
141 BSONCXX_INLINE bool operator==(const b_utf8& lhs, const b_utf8& rhs) {
142  return lhs.value == rhs.value;
143 }
144 
148 struct BSONCXX_API b_document {
149  static constexpr auto type_id = type::k_document;
150 
152 
156  BSONCXX_INLINE operator document::view() {
157  return value;
158  }
159 
163  BSONCXX_INLINE document::view view() {
164  return value;
165  }
166 };
167 
173 BSONCXX_INLINE bool operator==(const b_document& lhs, const b_document& rhs) {
174  return lhs.value == rhs.value;
175 }
176 
180 struct BSONCXX_API b_array {
181  static constexpr auto type_id = type::k_array;
182 
184 
188  BSONCXX_INLINE operator array::view() {
189  return value;
190  }
191 };
192 
198 BSONCXX_INLINE bool operator==(const b_array& lhs, const b_array& rhs) {
199  return lhs.value == rhs.value;
200 }
201 
205 struct BSONCXX_API b_binary {
206  static constexpr auto type_id = type::k_binary;
207 
208  binary_sub_type sub_type;
209  uint32_t size;
210  const uint8_t* bytes;
211 };
212 
218 BSONCXX_INLINE bool operator==(const b_binary& lhs, const b_binary& rhs) {
219  return lhs.sub_type == rhs.sub_type && lhs.size == rhs.size &&
220  (std::memcmp(lhs.bytes, rhs.bytes, lhs.size) == 0);
221 }
222 
229 struct BSONCXX_API b_undefined {
230  static constexpr auto type_id = type::k_undefined;
231 };
232 
238 BSONCXX_INLINE bool operator==(const b_undefined&, const b_undefined&) {
239  return true;
240 }
241 
245 struct BSONCXX_API b_oid {
246  static constexpr auto type_id = type::k_oid;
247 
248  oid value;
249 };
250 
256 BSONCXX_INLINE bool operator==(const b_oid& lhs, const b_oid& rhs) {
257  return lhs.value == rhs.value;
258 }
259 
263 struct BSONCXX_API b_bool {
264  static constexpr auto type_id = type::k_bool;
265 
266  bool value;
267 
271  BSONCXX_INLINE operator bool() {
272  return value;
273  }
274 };
275 
281 BSONCXX_INLINE bool operator==(const b_bool& lhs, const b_bool& rhs) {
282  return lhs.value == rhs.value;
283 }
284 
288 struct BSONCXX_API b_date {
289  static constexpr auto type_id = type::k_date;
290 
297  BSONCXX_INLINE
298  explicit b_date(std::chrono::milliseconds value) : value(value) {
299  }
300 
307  BSONCXX_INLINE
308  explicit b_date(const std::chrono::system_clock::time_point& tp)
309  : value(std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch())) {
310  }
311 
312  std::chrono::milliseconds value;
313 
317  BSONCXX_INLINE operator int64_t() {
318  return value.count();
319  }
320 
324  BSONCXX_INLINE int64_t to_int64() const {
325  return value.count();
326  }
327 
331  BSONCXX_INLINE operator std::chrono::system_clock::time_point() {
332  return std::chrono::system_clock::time_point(
333  std::chrono::duration_cast<std::chrono::system_clock::duration>(value));
334  }
335 };
336 
342 BSONCXX_INLINE bool operator==(const b_date& lhs, const b_date& rhs) {
343  return lhs.value == rhs.value;
344 }
345 
349 struct BSONCXX_API b_null {
350  static constexpr auto type_id = type::k_null;
351 };
352 
358 BSONCXX_INLINE bool operator==(const b_null&, const b_null&) {
359  return true;
360 }
361 
365 struct BSONCXX_API b_regex {
366  static constexpr auto type_id = type::k_regex;
367 
377  template <typename T, typename U>
378  BSONCXX_INLINE explicit b_regex(T&& regex, U&& options)
379  : regex(std::forward<T>(regex)), options(std::forward<U>(options)) {
380  }
381 
382  stdx::string_view regex;
383  stdx::string_view options;
384 };
385 
391 BSONCXX_INLINE bool operator==(const b_regex& lhs, const b_regex& rhs) {
392  return lhs.regex == rhs.regex && lhs.options == rhs.options;
393 }
394 
401 struct BSONCXX_API b_dbpointer {
402  static constexpr auto type_id = type::k_dbpointer;
403 
404  stdx::string_view collection;
405  oid value;
406 };
407 
413 BSONCXX_INLINE bool operator==(const b_dbpointer& lhs, const b_dbpointer& rhs) {
414  return lhs.collection == rhs.collection && lhs.value == rhs.value;
415 }
416 
420 struct BSONCXX_API b_code {
421  static constexpr auto type_id = type::k_code;
422 
429  template <typename T,
430  typename std::enable_if<!std::is_same<b_code, typename std::decay<T>::type>::value,
431  int>::type = 0>
432  BSONCXX_INLINE explicit b_code(T&& t)
433  : code(std::forward<T>(t)) {
434  }
435 
436  stdx::string_view code;
437 
441  BSONCXX_INLINE operator stdx::string_view() {
442  return code;
443  }
444 };
445 
451 BSONCXX_INLINE bool operator==(const b_code& lhs, const b_code& rhs) {
452  return lhs.code == rhs.code;
453 }
454 
461 struct BSONCXX_API b_symbol {
462  static constexpr auto type_id = type::k_symbol;
463 
470  template <typename T,
471  typename std::enable_if<!std::is_same<b_symbol, typename std::decay<T>::type>::value,
472  int>::type = 0>
473  BSONCXX_INLINE explicit b_symbol(T&& t)
474  : symbol(std::forward<T>(t)) {
475  }
476 
477  stdx::string_view symbol;
478 
482  BSONCXX_INLINE operator stdx::string_view() {
483  return symbol;
484  }
485 };
486 
492 BSONCXX_INLINE bool operator==(const b_symbol& lhs, const b_symbol& rhs) {
493  return lhs.symbol == rhs.symbol;
494 }
495 
499 struct BSONCXX_API b_codewscope {
500  static constexpr auto type_id = type::k_codewscope;
501 
511  template <typename T, typename U>
512  BSONCXX_INLINE explicit b_codewscope(T&& code, U&& scope)
513  : code(std::forward<T>(code)), scope(std::forward<U>(scope)) {
514  }
515 
516  stdx::string_view code;
517  document::view scope;
518 };
519 
525 BSONCXX_INLINE bool operator==(const b_codewscope& lhs, const b_codewscope& rhs) {
526  return lhs.code == rhs.code && lhs.scope == rhs.scope;
527 }
528 
532 struct BSONCXX_API b_int32 {
533  static constexpr auto type_id = type::k_int32;
534 
535  int32_t value;
536 
540  BSONCXX_INLINE operator int32_t() {
541  return value;
542  }
543 };
544 
550 BSONCXX_INLINE bool operator==(const b_int32& lhs, const b_int32& rhs) {
551  return lhs.value == rhs.value;
552 }
553 
561 struct BSONCXX_API b_timestamp {
562  static constexpr auto type_id = type::k_timestamp;
563 
564  uint32_t increment;
565  uint32_t timestamp;
566 };
567 
573 BSONCXX_INLINE bool operator==(const b_timestamp& lhs, const b_timestamp& rhs) {
574  return lhs.increment == rhs.increment && lhs.timestamp == rhs.timestamp;
575 }
576 
580 struct BSONCXX_API b_int64 {
581  static constexpr auto type_id = type::k_int64;
582 
583  int64_t value;
584 
588  BSONCXX_INLINE operator int64_t() {
589  return value;
590  }
591 };
592 
598 BSONCXX_INLINE bool operator==(const b_int64& lhs, const b_int64& rhs) {
599  return lhs.value == rhs.value;
600 }
601 
605 struct BSONCXX_API b_decimal128 {
606  static constexpr auto type_id = type::k_decimal128;
607 
609 
616  template <typename T,
617  typename std::enable_if<
618  !std::is_same<b_decimal128, typename std::decay<T>::type>::value, int>::type = 0>
619  BSONCXX_INLINE explicit b_decimal128(T&& t)
620  : value(std::forward<T>(t)) {
621  }
622 };
623 
629 BSONCXX_INLINE bool operator==(const b_decimal128& lhs, const b_decimal128& rhs) {
630  return lhs.value == rhs.value;
631 }
632 
636 struct BSONCXX_API b_minkey {
637  static constexpr auto type_id = type::k_minkey;
638 };
639 
645 BSONCXX_INLINE bool operator==(const b_minkey&, const b_minkey&) {
646  return true;
647 }
648 
652 struct BSONCXX_API b_maxkey {
653  static constexpr auto type_id = type::k_maxkey;
654 };
655 
661 BSONCXX_INLINE bool operator==(const b_maxkey&, const b_maxkey&) {
662  return true;
663 }
664 
665 #define BSONCXX_ENUM(name, val) \
666  BSONCXX_INLINE bool operator!=(const b_##name& lhs, const b_##name& rhs) { \
667  return !(lhs == rhs); \
668  }
669 #include <bsoncxx/enums/type.hpp>
670 #undef BSONCXX_ENUM
671 
672 } // namespace types
673 BSONCXX_INLINE_NAMESPACE_END
674 } // namespace bsoncxx
675 
676 #include <bsoncxx/config/postlude.hpp>
bool operator==(const b_document &lhs, const b_document &rhs)
free function comparator for b_document
Definition: types.hpp:173
bool operator==(const b_maxkey &, const b_maxkey &)
free function comparator for b_maxkey
Definition: types.hpp:661
A BSON signed 32-bit integer value.
Definition: types.hpp:532
A BSON double value.
Definition: types.hpp:85
A BSON Symbol value.
Definition: types.hpp:461
Represents a MongoDB ObjectId.
Definition: oid.hpp:38
A read-only, non-owning view of a BSON document.
Definition: view.hpp:33
b_utf8(T &&t)
Constructor for b_utf8.
Definition: types.hpp:122
bool operator==(const b_regex &lhs, const b_regex &rhs)
free function comparator for b_regex
Definition: types.hpp:391
binary_sub_type
An enumeration of each BSON binary sub type.
Definition: types.hpp:54
Definition: error_code.hpp:72
A BSON null value.
Definition: types.hpp:349
b_code(T &&t)
Constructor for b_code.
Definition: types.hpp:432
A BSON regex value.
Definition: types.hpp:365
A BSON binary data value.
Definition: types.hpp:205
bool operator==(const b_null &, const b_null &)
free function comparator for b_null
Definition: types.hpp:358
bool operator==(const b_decimal128 &lhs, const b_decimal128 &rhs)
free function comparator for b_decimal128
Definition: types.hpp:629
bool operator==(const b_code &lhs, const b_code &rhs)
free function comparator for b_code
Definition: types.hpp:451
A BSON DBPointer value.
Definition: types.hpp:401
bool operator==(const b_undefined &, const b_undefined &)
free function comparator for b_undefined
Definition: types.hpp:238
A BSON max-key value.
Definition: types.hpp:652
A BSON UTF-8 encoded string value.
Definition: types.hpp:110
Represents an IEEE 754-2008 BSON Decimal128 value in a platform-independent way.
Definition: decimal128.hpp:30
bool operator==(const b_symbol &lhs, const b_symbol &rhs)
free function comparator for b_symbol
Definition: types.hpp:492
A BSON date value.
Definition: types.hpp:288
A BSON min-key value.
Definition: types.hpp:636
bool operator==(const b_binary &lhs, const b_binary &rhs)
free function comparator for b_binary
Definition: types.hpp:218
A read-only, non-owning view of a BSON document.
Definition: view.hpp:33
b_decimal128(T &&t)
Constructor for b_decimal128.
Definition: types.hpp:619
A BSON JavaScript code value.
Definition: types.hpp:420
bool operator==(const b_double &lhs, const b_double &rhs)
free function comparator for b_double
Definition: types.hpp:103
b_codewscope(T &&code, U &&scope)
Constructor for b_codewscope.
Definition: types.hpp:512
bool operator==(const b_oid &lhs, const b_oid &rhs)
free function comparator for b_oid
Definition: types.hpp:256
b_symbol(T &&t)
Constructor for b_symbol.
Definition: types.hpp:473
bool operator==(const b_timestamp &lhs, const b_timestamp &rhs)
free function comparator for b_timestamp
Definition: types.hpp:573
bool operator==(const b_dbpointer &lhs, const b_dbpointer &rhs)
free function comparator for b_dbpointer
Definition: types.hpp:413
A BSON JavaScript code with scope value.
Definition: types.hpp:499
A BSON 64-bit signed integer value.
Definition: types.hpp:580
A BSON replication timestamp value.
Definition: types.hpp:561
bool operator==(const b_bool &lhs, const b_bool &rhs)
free function comparator for b_bool
Definition: types.hpp:281
int64_t to_int64() const
Manually convert this b_date to an int64_t.
Definition: types.hpp:324
bool operator==(const b_int64 &lhs, const b_int64 &rhs)
free function comparator for b_int64
Definition: types.hpp:598
A BSON document value.
Definition: types.hpp:148
bool operator==(const b_int32 &lhs, const b_int32 &rhs)
free function comparator for b_int32
Definition: types.hpp:550
type
An enumeration of each BSON type.
Definition: types.hpp:39
bool operator==(const b_date &lhs, const b_date &rhs)
free function comparator for b_date
Definition: types.hpp:342
b_date(std::chrono::milliseconds value)
Constructor for b_date.
Definition: types.hpp:298
A BSON boolean value.
Definition: types.hpp:263
bool operator==(const b_array &lhs, const b_array &rhs)
free function comparator for b_array
Definition: types.hpp:198
std::string to_string(type rhs)
Returns a stringification of the given type.
bool operator==(const b_minkey &, const b_minkey &)
free function comparator for b_minkey
Definition: types.hpp:645
b_date(const std::chrono::system_clock::time_point &tp)
Constructor for b_date.
Definition: types.hpp:308
bool operator==(const b_utf8 &lhs, const b_utf8 &rhs)
free function comparator for b_utf8
Definition: types.hpp:141
A BSON ObjectId value.
Definition: types.hpp:245
document::view view()
Returns an unwrapped document::view.
Definition: types.hpp:163
b_regex(T &&regex, U &&options)
Constructor for b_regex.
Definition: types.hpp:378
Top level namespace for MongoDB C++ BSON functionality.
Definition: element.hpp:24
A BSON undefined value.
Definition: types.hpp:229
A BSON array value.
Definition: types.hpp:180
A variant that can contain any BSON type.
Definition: value.hpp:37
A BSON Decimal128 value.
Definition: types.hpp:605
bool operator==(const b_codewscope &lhs, const b_codewscope &rhs)
free function comparator for b_codewscope
Definition: types.hpp:525