MongoDB C++ Driver  mongocxx-3.6.2
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 
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 
57 enum class binary_sub_type : std::uint8_t {
58 #define BSONCXX_ENUM(name, val) k_##name = val,
59 #include <bsoncxx/enums/binary_sub_type.hpp>
60 #undef BSONCXX_ENUM
61 };
62 
71 BSONCXX_API std::string BSONCXX_CALL to_string(type rhs);
72 
81 BSONCXX_API std::string BSONCXX_CALL to_string(binary_sub_type rhs);
82 
83 namespace types {
84 
88 struct BSONCXX_API b_double {
89  static constexpr auto type_id = type::k_double;
90 
91  double value;
92 
96  BSONCXX_INLINE operator double() const {
97  return value;
98  }
99 };
100 
106 BSONCXX_INLINE bool operator==(const b_double& lhs, const b_double& rhs) {
107  return lhs.value == rhs.value;
108 }
109 
113 struct BSONCXX_API b_utf8 {
114  static constexpr auto type_id = type::k_utf8;
115 
122  template <typename T,
123  typename std::enable_if<!std::is_same<b_utf8, typename std::decay<T>::type>::value,
124  int>::type = 0>
125  BSONCXX_INLINE explicit b_utf8(T&& t) : value(std::forward<T>(t)) {}
126 
127  stdx::string_view value;
128 
132  BSONCXX_INLINE operator stdx::string_view() const {
133  return value;
134  }
135 };
136 
142 BSONCXX_INLINE bool operator==(const b_utf8& lhs, const b_utf8& rhs) {
143  return lhs.value == rhs.value;
144 }
145 
149 struct BSONCXX_API b_document {
150  static constexpr auto type_id = type::k_document;
151 
152  document::view value;
153 
157  BSONCXX_INLINE operator document::view() const {
158  return value;
159  }
160 
164  BSONCXX_INLINE document::view view() {
165  return value;
166  }
167 };
168 
174 BSONCXX_INLINE bool operator==(const b_document& lhs, const b_document& rhs) {
175  return lhs.value == rhs.value;
176 }
177 
181 struct BSONCXX_API b_array {
182  static constexpr auto type_id = type::k_array;
183 
184  array::view value;
185 
189  BSONCXX_INLINE operator array::view() const {
190  return value;
191  }
192 };
193 
199 BSONCXX_INLINE bool operator==(const b_array& lhs, const b_array& rhs) {
200  return lhs.value == rhs.value;
201 }
202 
206 struct BSONCXX_API b_binary {
207  static constexpr auto type_id = type::k_binary;
208 
209  binary_sub_type sub_type;
210  uint32_t size;
211  const uint8_t* bytes;
212 };
213 
219 BSONCXX_INLINE bool operator==(const b_binary& lhs, const b_binary& rhs) {
220  return lhs.sub_type == rhs.sub_type && lhs.size == rhs.size &&
221  (std::memcmp(lhs.bytes, rhs.bytes, lhs.size) == 0);
222 }
223 
230 struct BSONCXX_API b_undefined {
231  static constexpr auto type_id = type::k_undefined;
232 };
233 
239 BSONCXX_INLINE bool operator==(const b_undefined&, const b_undefined&) {
240  return true;
241 }
242 
246 struct BSONCXX_API b_oid {
247  static constexpr auto type_id = type::k_oid;
248 
249  oid value;
250 };
251 
257 BSONCXX_INLINE bool operator==(const b_oid& lhs, const b_oid& rhs) {
258  return lhs.value == rhs.value;
259 }
260 
264 struct BSONCXX_API b_bool {
265  static constexpr auto type_id = type::k_bool;
266 
267  bool value;
268 
272  BSONCXX_INLINE operator bool() const {
273  return value;
274  }
275 };
276 
282 BSONCXX_INLINE bool operator==(const b_bool& lhs, const b_bool& rhs) {
283  return lhs.value == rhs.value;
284 }
285 
289 struct BSONCXX_API b_date {
290  static constexpr auto type_id = type::k_date;
291 
298  BSONCXX_INLINE
299  explicit b_date(std::chrono::milliseconds value) : value(value) {}
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  std::chrono::milliseconds value;
312 
316  BSONCXX_INLINE operator int64_t() const {
317  return value.count();
318  }
319 
323  BSONCXX_INLINE int64_t to_int64() const {
324  return value.count();
325  }
326 
330  BSONCXX_INLINE operator std::chrono::system_clock::time_point() const {
331  return std::chrono::system_clock::time_point(
332  std::chrono::duration_cast<std::chrono::system_clock::duration>(value));
333  }
334 };
335 
341 BSONCXX_INLINE bool operator==(const b_date& lhs, const b_date& rhs) {
342  return lhs.value == rhs.value;
343 }
344 
348 struct BSONCXX_API b_null {
349  static constexpr auto type_id = type::k_null;
350 };
351 
357 BSONCXX_INLINE bool operator==(const b_null&, const b_null&) {
358  return true;
359 }
360 
364 struct BSONCXX_API b_regex {
365  static constexpr auto type_id = type::k_regex;
366 
376  template <typename T,
377  typename U = stdx::string_view,
378  typename std::enable_if<!std::is_same<b_regex, typename std::decay<T>::type>::value,
379  int>::type = 0>
380  BSONCXX_INLINE explicit b_regex(T&& regex, U&& options = U{})
381  : regex(std::forward<T>(regex)), options(std::forward<U>(options)) {}
382 
383  stdx::string_view regex;
384  stdx::string_view options;
385 };
386 
392 BSONCXX_INLINE bool operator==(const b_regex& lhs, const b_regex& rhs) {
393  return lhs.regex == rhs.regex && lhs.options == rhs.options;
394 }
395 
402 struct BSONCXX_API b_dbpointer {
403  static constexpr auto type_id = type::k_dbpointer;
404 
405  stdx::string_view collection;
406  oid value;
407 };
408 
414 BSONCXX_INLINE bool operator==(const b_dbpointer& lhs, const b_dbpointer& rhs) {
415  return lhs.collection == rhs.collection && lhs.value == rhs.value;
416 }
417 
421 struct BSONCXX_API b_code {
422  static constexpr auto type_id = type::k_code;
423 
430  template <typename T,
431  typename std::enable_if<!std::is_same<b_code, typename std::decay<T>::type>::value,
432  int>::type = 0>
433  BSONCXX_INLINE explicit b_code(T&& t) : code(std::forward<T>(t)) {}
434 
435  stdx::string_view code;
436 
440  BSONCXX_INLINE operator stdx::string_view() const {
441  return code;
442  }
443 };
444 
450 BSONCXX_INLINE bool operator==(const b_code& lhs, const b_code& rhs) {
451  return lhs.code == rhs.code;
452 }
453 
460 struct BSONCXX_API b_symbol {
461  static constexpr auto type_id = type::k_symbol;
462 
469  template <typename T,
470  typename std::enable_if<!std::is_same<b_symbol, typename std::decay<T>::type>::value,
471  int>::type = 0>
472  BSONCXX_INLINE explicit b_symbol(T&& t) : symbol(std::forward<T>(t)) {}
473 
474  stdx::string_view symbol;
475 
479  BSONCXX_INLINE operator stdx::string_view() const {
480  return symbol;
481  }
482 };
483 
489 BSONCXX_INLINE bool operator==(const b_symbol& lhs, const b_symbol& rhs) {
490  return lhs.symbol == rhs.symbol;
491 }
492 
496 struct BSONCXX_API b_codewscope {
497  static constexpr auto type_id = type::k_codewscope;
498 
508  template <
509  typename T,
510  typename U,
511  typename std::enable_if<!std::is_same<b_codewscope, typename std::decay<T>::type>::value,
512  int>::type = 0>
513  BSONCXX_INLINE explicit b_codewscope(T&& code, U&& scope)
514  : code(std::forward<T>(code)), scope(std::forward<U>(scope)) {}
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() const {
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() const {
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 
608  decimal128 value;
609 
616  template <
617  typename T,
618  typename std::enable_if<!std::is_same<b_decimal128, typename std::decay<T>::type>::value,
619  int>::type = 0>
620  BSONCXX_INLINE explicit b_decimal128(T&& t) : value(std::forward<T>(t)) {}
621 };
622 
628 BSONCXX_INLINE bool operator==(const b_decimal128& lhs, const b_decimal128& rhs) {
629  return lhs.value == rhs.value;
630 }
631 
635 struct BSONCXX_API b_minkey {
636  static constexpr auto type_id = type::k_minkey;
637 };
638 
644 BSONCXX_INLINE bool operator==(const b_minkey&, const b_minkey&) {
645  return true;
646 }
647 
651 struct BSONCXX_API b_maxkey {
652  static constexpr auto type_id = type::k_maxkey;
653 };
654 
660 BSONCXX_INLINE bool operator==(const b_maxkey&, const b_maxkey&) {
661  return true;
662 }
663 
664 #define BSONCXX_ENUM(name, val) \
665  BSONCXX_INLINE bool operator!=(const b_##name& lhs, const b_##name& rhs) { \
666  return !(lhs == rhs); \
667  }
668 #include <bsoncxx/enums/type.hpp>
669 #undef BSONCXX_ENUM
670 
671 } // namespace types
672 BSONCXX_INLINE_NAMESPACE_END
673 } // namespace bsoncxx
674 
675 #include <bsoncxx/config/postlude.hpp>
bsoncxx::types::b_decimal128::operator==
bool operator==(const b_decimal128 &lhs, const b_decimal128 &rhs)
free function comparator for b_decimal128
Definition: types.hpp:628
bsoncxx::types::b_array
A BSON array value.
Definition: types.hpp:181
bsoncxx::types::b_oid
A BSON ObjectId value.
Definition: types.hpp:246
bsoncxx::types::b_document::operator==
bool operator==(const b_document &lhs, const b_document &rhs)
free function comparator for b_document
Definition: types.hpp:174
bsoncxx::types::b_symbol::operator==
bool operator==(const b_symbol &lhs, const b_symbol &rhs)
free function comparator for b_symbol
Definition: types.hpp:489
bsoncxx::types::b_document::view
document::view view()
Returns an unwrapped document::view.
Definition: types.hpp:164
bsoncxx::types::b_undefined::operator==
bool operator==(const b_undefined &, const b_undefined &)
free function comparator for b_undefined
Definition: types.hpp:239
bsoncxx::types::b_oid::operator==
bool operator==(const b_oid &lhs, const b_oid &rhs)
free function comparator for b_oid
Definition: types.hpp:257
bsoncxx::types::b_utf8
A BSON UTF-8 encoded string value.
Definition: types.hpp:113
bsoncxx::types::b_regex::operator==
bool operator==(const b_regex &lhs, const b_regex &rhs)
free function comparator for b_regex
Definition: types.hpp:392
bsoncxx
Top level namespace for MongoDB C++ BSON functionality.
Definition: element.hpp:24
bsoncxx::types::b_document
A BSON document value.
Definition: types.hpp:149
bsoncxx::types::b_null
A BSON null value.
Definition: types.hpp:348
bsoncxx::types::b_maxkey::operator==
bool operator==(const b_maxkey &, const b_maxkey &)
free function comparator for b_maxkey
Definition: types.hpp:660
bsoncxx::types::b_codewscope
A BSON JavaScript code with scope value.
Definition: types.hpp:496
bsoncxx::types::b_int64
A BSON 64-bit signed integer value.
Definition: types.hpp:580
bsoncxx::binary_sub_type
binary_sub_type
An enumeration of each BSON binary sub type.
Definition: types.hpp:57
bsoncxx::types::b_binary
A BSON binary data value.
Definition: types.hpp:206
bsoncxx::types::b_dbpointer::operator==
bool operator==(const b_dbpointer &lhs, const b_dbpointer &rhs)
free function comparator for b_dbpointer
Definition: types.hpp:414
bsoncxx::types::b_symbol::b_symbol
b_symbol(T &&t)
Constructor for b_symbol.
Definition: types.hpp:472
bsoncxx::array::view
A read-only, non-owning view of a BSON document.
Definition: view.hpp:33
bsoncxx::types::b_utf8::b_utf8
b_utf8(T &&t)
Constructor for b_utf8.
Definition: types.hpp:125
bsoncxx::types::b_date::to_int64
int64_t to_int64() const
Manually convert this b_date to an int64_t.
Definition: types.hpp:323
bsoncxx::types::b_codewscope::b_codewscope
b_codewscope(T &&code, U &&scope)
Constructor for b_codewscope.
Definition: types.hpp:513
bsoncxx::types::b_null::operator==
bool operator==(const b_null &, const b_null &)
free function comparator for b_null
Definition: types.hpp:357
bsoncxx::types::b_symbol
A BSON Symbol value.
Definition: types.hpp:460
bsoncxx::types::b_dbpointer
A BSON DBPointer value.
Definition: types.hpp:402
bsoncxx::oid
Represents a MongoDB ObjectId.
Definition: oid.hpp:38
bsoncxx::types::b_date::b_date
b_date(const std::chrono::system_clock::time_point &tp)
Constructor for b_date.
Definition: types.hpp:308
bsoncxx::types::b_int32::operator==
bool operator==(const b_int32 &lhs, const b_int32 &rhs)
free function comparator for b_int32
Definition: types.hpp:550
bsoncxx::types::b_regex
A BSON regex value.
Definition: types.hpp:364
bsoncxx::types::b_maxkey
A BSON max-key value.
Definition: types.hpp:651
bsoncxx::types::b_array::operator==
bool operator==(const b_array &lhs, const b_array &rhs)
free function comparator for b_array
Definition: types.hpp:199
bsoncxx::types::b_code::b_code
b_code(T &&t)
Constructor for b_code.
Definition: types.hpp:433
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:525
bsoncxx::types::b_date::operator==
bool operator==(const b_date &lhs, const b_date &rhs)
free function comparator for b_date
Definition: types.hpp:341
bsoncxx::types::b_timestamp
A BSON replication timestamp value.
Definition: types.hpp:561
bsoncxx::types::b_code::operator==
bool operator==(const b_code &lhs, const b_code &rhs)
free function comparator for b_code
Definition: types.hpp:450
bsoncxx::types::b_date
A BSON date value.
Definition: types.hpp:289
bsoncxx::types::b_int32
A BSON signed 32-bit integer value.
Definition: types.hpp:532
bsoncxx::types::b_minkey
A BSON min-key value.
Definition: types.hpp:635
bsoncxx::types::b_timestamp::operator==
bool operator==(const b_timestamp &lhs, const b_timestamp &rhs)
free function comparator for b_timestamp
Definition: types.hpp:573
bsoncxx::types::b_double::operator==
bool operator==(const b_double &lhs, const b_double &rhs)
free function comparator for b_double
Definition: types.hpp:106
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:282
bsoncxx::types::b_binary::operator==
bool operator==(const b_binary &lhs, const b_binary &rhs)
free function comparator for b_binary
Definition: types.hpp:219
bsoncxx::types::b_minkey::operator==
bool operator==(const b_minkey &, const b_minkey &)
free function comparator for b_minkey
Definition: types.hpp:644
bsoncxx::types::b_int64::operator==
bool operator==(const b_int64 &lhs, const b_int64 &rhs)
free function comparator for b_int64
Definition: types.hpp:598
bsoncxx::types::b_code
A BSON JavaScript code value.
Definition: types.hpp:421
bsoncxx::types::b_decimal128::b_decimal128
b_decimal128(T &&t)
Constructor for b_decimal128.
Definition: types.hpp:620
bsoncxx::types::b_decimal128
A BSON Decimal128 value.
Definition: types.hpp:605
bsoncxx::type
type
An enumeration of each BSON type.
Definition: types.hpp:39
bsoncxx::types::b_undefined
A BSON undefined value.
Definition: types.hpp:230
bsoncxx::types::b_date::b_date
b_date(std::chrono::milliseconds value)
Constructor for b_date.
Definition: types.hpp:299
bsoncxx::types::b_utf8::operator==
bool operator==(const b_utf8 &lhs, const b_utf8 &rhs)
free function comparator for b_utf8
Definition: types.hpp:142
bsoncxx::types::b_regex::b_regex
b_regex(T &&regex, U &&options=U{})
Constructor for b_regex.
Definition: types.hpp:380
bsoncxx::types::b_bool
A BSON boolean value.
Definition: types.hpp:264
bsoncxx::types::b_double
A BSON double value.
Definition: types.hpp:88
bsoncxx::document::view
A read-only, non-owning view of a BSON document.
Definition: view.hpp:33