MongoDB C++ Driver  mongocxx-3.7.0
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 
35 namespace bsoncxx {
36 BSONCXX_INLINE_NAMESPACE_BEGIN
37 
46 enum 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 
66 enum 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 
80 BSONCXX_API std::string BSONCXX_CALL to_string(type rhs);
81 
90 BSONCXX_API std::string BSONCXX_CALL to_string(binary_sub_type rhs);
91 
92 namespace types {
93 
97 struct 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 
115 BSONCXX_INLINE bool operator==(const b_double& lhs, const b_double& rhs) {
116  return lhs.value == rhs.value;
117 }
118 
122 struct 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 
151 BSONCXX_INLINE bool operator==(const b_string& lhs, const b_string& rhs) {
152  return lhs.value == rhs.value;
153 }
154 
160 BSONCXX_DEPRECATED typedef b_string b_utf8;
161 
165 struct 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 
190 BSONCXX_INLINE bool operator==(const b_document& lhs, const b_document& rhs) {
191  return lhs.value == rhs.value;
192 }
193 
197 struct 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 
215 BSONCXX_INLINE bool operator==(const b_array& lhs, const b_array& rhs) {
216  return lhs.value == rhs.value;
217 }
218 
222 struct 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 
235 BSONCXX_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 
246 struct BSONCXX_API b_undefined {
247  static constexpr auto type_id = type::k_undefined;
248 };
249 
255 BSONCXX_INLINE bool operator==(const b_undefined&, const b_undefined&) {
256  return true;
257 }
258 
262 struct BSONCXX_API b_oid {
263  static constexpr auto type_id = type::k_oid;
264 
265  oid value;
266 };
267 
273 BSONCXX_INLINE bool operator==(const b_oid& lhs, const b_oid& rhs) {
274  return lhs.value == rhs.value;
275 }
276 
280 struct 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 
298 BSONCXX_INLINE bool operator==(const b_bool& lhs, const b_bool& rhs) {
299  return lhs.value == rhs.value;
300 }
301 
305 struct 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 
357 BSONCXX_INLINE bool operator==(const b_date& lhs, const b_date& rhs) {
358  return lhs.value == rhs.value;
359 }
360 
364 struct BSONCXX_API b_null {
365  static constexpr auto type_id = type::k_null;
366 };
367 
373 BSONCXX_INLINE bool operator==(const b_null&, const b_null&) {
374  return true;
375 }
376 
380 struct 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 
408 BSONCXX_INLINE bool operator==(const b_regex& lhs, const b_regex& rhs) {
409  return lhs.regex == rhs.regex && lhs.options == rhs.options;
410 }
411 
418 struct BSONCXX_API b_dbpointer {
419  static constexpr auto type_id = type::k_dbpointer;
420 
421  stdx::string_view collection;
422  oid value;
423 };
424 
430 BSONCXX_INLINE bool operator==(const b_dbpointer& lhs, const b_dbpointer& rhs) {
431  return lhs.collection == rhs.collection && lhs.value == rhs.value;
432 }
433 
437 struct 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 
466 BSONCXX_INLINE bool operator==(const b_code& lhs, const b_code& rhs) {
467  return lhs.code == rhs.code;
468 }
469 
476 struct 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 
505 BSONCXX_INLINE bool operator==(const b_symbol& lhs, const b_symbol& rhs) {
506  return lhs.symbol == rhs.symbol;
507 }
508 
512 struct 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 
541 BSONCXX_INLINE bool operator==(const b_codewscope& lhs, const b_codewscope& rhs) {
542  return lhs.code == rhs.code && lhs.scope == rhs.scope;
543 }
544 
548 struct 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 
566 BSONCXX_INLINE bool operator==(const b_int32& lhs, const b_int32& rhs) {
567  return lhs.value == rhs.value;
568 }
569 
577 struct BSONCXX_API b_timestamp {
578  static constexpr auto type_id = type::k_timestamp;
579 
580  uint32_t increment;
581  uint32_t timestamp;
582 };
583 
589 BSONCXX_INLINE bool operator==(const b_timestamp& lhs, const b_timestamp& rhs) {
590  return lhs.increment == rhs.increment && lhs.timestamp == rhs.timestamp;
591 }
592 
596 struct 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 
614 BSONCXX_INLINE bool operator==(const b_int64& lhs, const b_int64& rhs) {
615  return lhs.value == rhs.value;
616 }
617 
621 struct 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 
644 BSONCXX_INLINE bool operator==(const b_decimal128& lhs, const b_decimal128& rhs) {
645  return lhs.value == rhs.value;
646 }
647 
651 struct BSONCXX_API b_minkey {
652  static constexpr auto type_id = type::k_minkey;
653 };
654 
660 BSONCXX_INLINE bool operator==(const b_minkey&, const b_minkey&) {
661  return true;
662 }
663 
667 struct BSONCXX_API b_maxkey {
668  static constexpr auto type_id = type::k_maxkey;
669 };
670 
676 BSONCXX_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
688 BSONCXX_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
bsoncxx::types::b_decimal128::operator==
bool operator==(const b_decimal128 &lhs, const b_decimal128 &rhs)
free function comparator for b_decimal128
Definition: types.hpp:644
bsoncxx::types::b_array
A BSON array value.
Definition: types.hpp:197
bsoncxx::types::b_oid
A BSON ObjectId value.
Definition: types.hpp:262
bsoncxx::types::b_document::operator==
bool operator==(const b_document &lhs, const b_document &rhs)
free function comparator for b_document
Definition: types.hpp:190
bsoncxx::types::b_symbol::operator==
bool operator==(const b_symbol &lhs, const b_symbol &rhs)
free function comparator for b_symbol
Definition: types.hpp:505
bsoncxx::types::b_document::view
document::view view()
Returns an unwrapped document::view.
Definition: types.hpp:180
bsoncxx::types::b_undefined::operator==
bool operator==(const b_undefined &, const b_undefined &)
free function comparator for b_undefined
Definition: types.hpp:255
bsoncxx::types::b_oid::operator==
bool operator==(const b_oid &lhs, const b_oid &rhs)
free function comparator for b_oid
Definition: types.hpp:273
bsoncxx::types::b_regex::operator==
bool operator==(const b_regex &lhs, const b_regex &rhs)
free function comparator for b_regex
Definition: types.hpp:408
bsoncxx
Top level namespace for MongoDB C++ BSON functionality.
Definition: element.hpp:24
bsoncxx::types::b_document
A BSON document value.
Definition: types.hpp:165
bsoncxx::types::b_null
A BSON null value.
Definition: types.hpp:364
bsoncxx::types::b_maxkey::operator==
bool operator==(const b_maxkey &, const b_maxkey &)
free function comparator for b_maxkey
Definition: types.hpp:676
bsoncxx::types::b_codewscope
A BSON JavaScript code with scope value.
Definition: types.hpp:512
bsoncxx::types::b_string
A BSON UTF-8 encoded string value.
Definition: types.hpp:122
bsoncxx::types::b_int64
A BSON 64-bit signed integer value.
Definition: types.hpp:596
bsoncxx::binary_sub_type
binary_sub_type
An enumeration of each BSON binary sub type.
Definition: types.hpp:66
bsoncxx::types::b_binary
A BSON binary data value.
Definition: types.hpp:222
bsoncxx::types::b_dbpointer::operator==
bool operator==(const b_dbpointer &lhs, const b_dbpointer &rhs)
free function comparator for b_dbpointer
Definition: types.hpp:430
bsoncxx::types::b_symbol::b_symbol
b_symbol(T &&t)
Constructor for b_symbol.
Definition: types.hpp:488
bsoncxx::array::view
A read-only, non-owning view of a BSON document.
Definition: view.hpp:40
bsoncxx::types::b_date::to_int64
int64_t to_int64() const
Manually convert this b_date to an int64_t.
Definition: types.hpp:339
bsoncxx::types::b_codewscope::b_codewscope
b_codewscope(T &&code, U &&scope)
Constructor for b_codewscope.
Definition: types.hpp:529
bsoncxx::types::b_string::operator==
bool operator==(const b_string &lhs, const b_string &rhs)
free function comparator for b_string
Definition: types.hpp:151
bsoncxx::types::b_null::operator==
bool operator==(const b_null &, const b_null &)
free function comparator for b_null
Definition: types.hpp:373
bsoncxx::types::b_symbol
A BSON Symbol value.
Definition: types.hpp:476
bsoncxx::types::b_dbpointer
A BSON DBPointer value.
Definition: types.hpp:418
bsoncxx::oid
Represents a MongoDB ObjectId.
Definition: oid.hpp:38
bsoncxx::types::b_string::b_string
b_string(T &&t)
Constructor for b_string.
Definition: types.hpp:134
bsoncxx::types::b_date::b_date
b_date(const std::chrono::system_clock::time_point &tp)
Constructor for b_date.
Definition: types.hpp:324
bsoncxx::types::b_int32::operator==
bool operator==(const b_int32 &lhs, const b_int32 &rhs)
free function comparator for b_int32
Definition: types.hpp:566
bsoncxx::types::b_regex
A BSON regex value.
Definition: types.hpp:380
bsoncxx::types::b_maxkey
A BSON max-key value.
Definition: types.hpp:667
bsoncxx::types::b_array::operator==
bool operator==(const b_array &lhs, const b_array &rhs)
free function comparator for b_array
Definition: types.hpp:215
bsoncxx::types::b_code::b_code
b_code(T &&t)
Constructor for b_code.
Definition: types.hpp:449
bsoncxx::decimal128
Represents an IEEE 754-2008 BSON Decimal128 value in a platform-independent way.
Definition: decimal128.hpp:30
bsoncxx::types::b_codewscope::operator==
bool operator==(const b_codewscope &lhs, const b_codewscope &rhs)
free function comparator for b_codewscope
Definition: types.hpp:541
bsoncxx::types::b_date::operator==
bool operator==(const b_date &lhs, const b_date &rhs)
free function comparator for b_date
Definition: types.hpp:357
bsoncxx::types::b_timestamp
A BSON replication timestamp value.
Definition: types.hpp:577
bsoncxx::types::b_code::operator==
bool operator==(const b_code &lhs, const b_code &rhs)
free function comparator for b_code
Definition: types.hpp:466
bsoncxx::types::b_date
A BSON date value.
Definition: types.hpp:305
bsoncxx::types::b_int32
A BSON signed 32-bit integer value.
Definition: types.hpp:548
bsoncxx::types::b_minkey
A BSON min-key value.
Definition: types.hpp:651
bsoncxx::types::b_timestamp::operator==
bool operator==(const b_timestamp &lhs, const b_timestamp &rhs)
free function comparator for b_timestamp
Definition: types.hpp:589
bsoncxx::types::b_double::operator==
bool operator==(const b_double &lhs, const b_double &rhs)
free function comparator for b_double
Definition: types.hpp:115
bsoncxx::to_string
std::string to_string(type rhs)
Returns a stringification of the given type.
bsoncxx::types::b_bool::operator==
bool operator==(const b_bool &lhs, const b_bool &rhs)
free function comparator for b_bool
Definition: types.hpp:298
bsoncxx::types::b_binary::operator==
bool operator==(const b_binary &lhs, const b_binary &rhs)
free function comparator for b_binary
Definition: types.hpp:235
bsoncxx::types::b_minkey::operator==
bool operator==(const b_minkey &, const b_minkey &)
free function comparator for b_minkey
Definition: types.hpp:660
bsoncxx::types::b_int64::operator==
bool operator==(const b_int64 &lhs, const b_int64 &rhs)
free function comparator for b_int64
Definition: types.hpp:614
bsoncxx::types::b_code
A BSON JavaScript code value.
Definition: types.hpp:437
bsoncxx::types::b_decimal128::b_decimal128
b_decimal128(T &&t)
Constructor for b_decimal128.
Definition: types.hpp:636
bsoncxx::types::b_decimal128
A BSON Decimal128 value.
Definition: types.hpp:621
bsoncxx::type
type
An enumeration of each BSON type.
Definition: types.hpp:46
bsoncxx::types::b_undefined
A BSON undefined value.
Definition: types.hpp:246
bsoncxx::types::b_date::b_date
b_date(std::chrono::milliseconds value)
Constructor for b_date.
Definition: types.hpp:315
bsoncxx::types::b_regex::b_regex
b_regex(T &&regex, U &&options=U{})
Constructor for b_regex.
Definition: types.hpp:396
bsoncxx::types::b_bool
A BSON boolean value.
Definition: types.hpp:280
bsoncxx::types::b_double
A BSON double value.
Definition: types.hpp:97
bsoncxx::document::view
A read-only, non-owning view of a BSON document.
Definition: view.hpp:33