MongoDB C++ Driver  mongocxx-3.3.2
All Classes Namespaces Functions 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 
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() const {
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) : value(std::forward<T>(t)) {}
123 
124  stdx::string_view value;
125 
129  BSONCXX_INLINE operator stdx::string_view() const {
130  return value;
131  }
132 };
133 
139 BSONCXX_INLINE bool operator==(const b_utf8& lhs, const b_utf8& rhs) {
140  return lhs.value == rhs.value;
141 }
142 
146 struct BSONCXX_API b_document {
147  static constexpr auto type_id = type::k_document;
148 
150 
154  BSONCXX_INLINE operator document::view() const {
155  return value;
156  }
157 
161  BSONCXX_INLINE document::view view() {
162  return value;
163  }
164 };
165 
171 BSONCXX_INLINE bool operator==(const b_document& lhs, const b_document& rhs) {
172  return lhs.value == rhs.value;
173 }
174 
178 struct BSONCXX_API b_array {
179  static constexpr auto type_id = type::k_array;
180 
182 
186  BSONCXX_INLINE operator array::view() const {
187  return value;
188  }
189 };
190 
196 BSONCXX_INLINE bool operator==(const b_array& lhs, const b_array& rhs) {
197  return lhs.value == rhs.value;
198 }
199 
203 struct BSONCXX_API b_binary {
204  static constexpr auto type_id = type::k_binary;
205 
206  binary_sub_type sub_type;
207  uint32_t size;
208  const uint8_t* bytes;
209 };
210 
216 BSONCXX_INLINE bool operator==(const b_binary& lhs, const b_binary& rhs) {
217  return lhs.sub_type == rhs.sub_type && lhs.size == rhs.size &&
218  (std::memcmp(lhs.bytes, rhs.bytes, lhs.size) == 0);
219 }
220 
227 struct BSONCXX_API b_undefined {
228  static constexpr auto type_id = type::k_undefined;
229 };
230 
236 BSONCXX_INLINE bool operator==(const b_undefined&, const b_undefined&) {
237  return true;
238 }
239 
243 struct BSONCXX_API b_oid {
244  static constexpr auto type_id = type::k_oid;
245 
246  oid value;
247 };
248 
254 BSONCXX_INLINE bool operator==(const b_oid& lhs, const b_oid& rhs) {
255  return lhs.value == rhs.value;
256 }
257 
261 struct BSONCXX_API b_bool {
262  static constexpr auto type_id = type::k_bool;
263 
264  bool value;
265 
269  BSONCXX_INLINE operator bool() const {
270  return value;
271  }
272 };
273 
279 BSONCXX_INLINE bool operator==(const b_bool& lhs, const b_bool& rhs) {
280  return lhs.value == rhs.value;
281 }
282 
286 struct BSONCXX_API b_date {
287  static constexpr auto type_id = type::k_date;
288 
295  BSONCXX_INLINE
296  explicit b_date(std::chrono::milliseconds value) : value(value) {}
297 
304  BSONCXX_INLINE
305  explicit b_date(const std::chrono::system_clock::time_point& tp)
306  : value(std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch())) {}
307 
308  std::chrono::milliseconds value;
309 
313  BSONCXX_INLINE operator int64_t() const {
314  return value.count();
315  }
316 
320  BSONCXX_INLINE int64_t to_int64() const {
321  return value.count();
322  }
323 
327  BSONCXX_INLINE operator std::chrono::system_clock::time_point() const {
328  return std::chrono::system_clock::time_point(
329  std::chrono::duration_cast<std::chrono::system_clock::duration>(value));
330  }
331 };
332 
338 BSONCXX_INLINE bool operator==(const b_date& lhs, const b_date& rhs) {
339  return lhs.value == rhs.value;
340 }
341 
345 struct BSONCXX_API b_null {
346  static constexpr auto type_id = type::k_null;
347 };
348 
354 BSONCXX_INLINE bool operator==(const b_null&, const b_null&) {
355  return true;
356 }
357 
361 struct BSONCXX_API b_regex {
362  static constexpr auto type_id = type::k_regex;
363 
373  template <typename T,
374  typename U = stdx::string_view,
375  typename std::enable_if<!std::is_same<b_regex, typename std::decay<T>::type>::value,
376  int>::type = 0>
377  BSONCXX_INLINE explicit b_regex(T&& regex, U&& options = U{})
378  : regex(std::forward<T>(regex)), options(std::forward<U>(options)) {}
379 
380  stdx::string_view regex;
381  stdx::string_view options;
382 };
383 
389 BSONCXX_INLINE bool operator==(const b_regex& lhs, const b_regex& rhs) {
390  return lhs.regex == rhs.regex && lhs.options == rhs.options;
391 }
392 
399 struct BSONCXX_API b_dbpointer {
400  static constexpr auto type_id = type::k_dbpointer;
401 
402  stdx::string_view collection;
403  oid value;
404 };
405 
411 BSONCXX_INLINE bool operator==(const b_dbpointer& lhs, const b_dbpointer& rhs) {
412  return lhs.collection == rhs.collection && lhs.value == rhs.value;
413 }
414 
418 struct BSONCXX_API b_code {
419  static constexpr auto type_id = type::k_code;
420 
427  template <typename T,
428  typename std::enable_if<!std::is_same<b_code, typename std::decay<T>::type>::value,
429  int>::type = 0>
430  BSONCXX_INLINE explicit b_code(T&& t) : code(std::forward<T>(t)) {}
431 
432  stdx::string_view code;
433 
437  BSONCXX_INLINE operator stdx::string_view() const {
438  return code;
439  }
440 };
441 
447 BSONCXX_INLINE bool operator==(const b_code& lhs, const b_code& rhs) {
448  return lhs.code == rhs.code;
449 }
450 
457 struct BSONCXX_API b_symbol {
458  static constexpr auto type_id = type::k_symbol;
459 
466  template <typename T,
467  typename std::enable_if<!std::is_same<b_symbol, typename std::decay<T>::type>::value,
468  int>::type = 0>
469  BSONCXX_INLINE explicit b_symbol(T&& t) : symbol(std::forward<T>(t)) {}
470 
471  stdx::string_view symbol;
472 
476  BSONCXX_INLINE operator stdx::string_view() const {
477  return symbol;
478  }
479 };
480 
486 BSONCXX_INLINE bool operator==(const b_symbol& lhs, const b_symbol& rhs) {
487  return lhs.symbol == rhs.symbol;
488 }
489 
493 struct BSONCXX_API b_codewscope {
494  static constexpr auto type_id = type::k_codewscope;
495 
505  template <
506  typename T,
507  typename U,
508  typename std::enable_if<!std::is_same<b_codewscope, typename std::decay<T>::type>::value,
509  int>::type = 0>
510  BSONCXX_INLINE explicit b_codewscope(T&& code, U&& scope)
511  : code(std::forward<T>(code)), scope(std::forward<U>(scope)) {}
512 
513  stdx::string_view code;
514  document::view scope;
515 };
516 
522 BSONCXX_INLINE bool operator==(const b_codewscope& lhs, const b_codewscope& rhs) {
523  return lhs.code == rhs.code && lhs.scope == rhs.scope;
524 }
525 
529 struct BSONCXX_API b_int32 {
530  static constexpr auto type_id = type::k_int32;
531 
532  int32_t value;
533 
537  BSONCXX_INLINE operator int32_t() const {
538  return value;
539  }
540 };
541 
547 BSONCXX_INLINE bool operator==(const b_int32& lhs, const b_int32& rhs) {
548  return lhs.value == rhs.value;
549 }
550 
558 struct BSONCXX_API b_timestamp {
559  static constexpr auto type_id = type::k_timestamp;
560 
561  uint32_t increment;
562  uint32_t timestamp;
563 };
564 
570 BSONCXX_INLINE bool operator==(const b_timestamp& lhs, const b_timestamp& rhs) {
571  return lhs.increment == rhs.increment && lhs.timestamp == rhs.timestamp;
572 }
573 
577 struct BSONCXX_API b_int64 {
578  static constexpr auto type_id = type::k_int64;
579 
580  int64_t value;
581 
585  BSONCXX_INLINE operator int64_t() const {
586  return value;
587  }
588 };
589 
595 BSONCXX_INLINE bool operator==(const b_int64& lhs, const b_int64& rhs) {
596  return lhs.value == rhs.value;
597 }
598 
602 struct BSONCXX_API b_decimal128 {
603  static constexpr auto type_id = type::k_decimal128;
604 
606 
613  template <
614  typename T,
615  typename std::enable_if<!std::is_same<b_decimal128, typename std::decay<T>::type>::value,
616  int>::type = 0>
617  BSONCXX_INLINE explicit b_decimal128(T&& t) : value(std::forward<T>(t)) {}
618 };
619 
625 BSONCXX_INLINE bool operator==(const b_decimal128& lhs, const b_decimal128& rhs) {
626  return lhs.value == rhs.value;
627 }
628 
632 struct BSONCXX_API b_minkey {
633  static constexpr auto type_id = type::k_minkey;
634 };
635 
641 BSONCXX_INLINE bool operator==(const b_minkey&, const b_minkey&) {
642  return true;
643 }
644 
648 struct BSONCXX_API b_maxkey {
649  static constexpr auto type_id = type::k_maxkey;
650 };
651 
657 BSONCXX_INLINE bool operator==(const b_maxkey&, const b_maxkey&) {
658  return true;
659 }
660 
661 #define BSONCXX_ENUM(name, val) \
662  BSONCXX_INLINE bool operator!=(const b_##name& lhs, const b_##name& rhs) { \
663  return !(lhs == rhs); \
664  }
665 #include <bsoncxx/enums/type.hpp>
666 #undef BSONCXX_ENUM
667 
668 } // namespace types
669 BSONCXX_INLINE_NAMESPACE_END
670 } // namespace bsoncxx
671 
672 #include <bsoncxx/config/postlude.hpp>
bool operator==(const b_document &lhs, const b_document &rhs)
free function comparator for b_document
Definition: types.hpp:171
bool operator==(const b_maxkey &, const b_maxkey &)
free function comparator for b_maxkey
Definition: types.hpp:657
A BSON signed 32-bit integer value.
Definition: types.hpp:529
A BSON double value.
Definition: types.hpp:85
A BSON Symbol value.
Definition: types.hpp:457
Represents a MongoDB ObjectId.
Definition: oid.hpp:38
b_regex(T &&regex, U &&options=U{})
Constructor for b_regex.
Definition: types.hpp:377
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:389
binary_sub_type
An enumeration of each BSON binary sub type.
Definition: types.hpp:54
Definition: error_code.hpp:117
A BSON null value.
Definition: types.hpp:345
b_code(T &&t)
Constructor for b_code.
Definition: types.hpp:430
A BSON regex value.
Definition: types.hpp:361
A BSON binary data value.
Definition: types.hpp:203
bool operator==(const b_null &, const b_null &)
free function comparator for b_null
Definition: types.hpp:354
bool operator==(const b_decimal128 &lhs, const b_decimal128 &rhs)
free function comparator for b_decimal128
Definition: types.hpp:625
bool operator==(const b_code &lhs, const b_code &rhs)
free function comparator for b_code
Definition: types.hpp:447
A BSON DBPointer value.
Definition: types.hpp:399
bool operator==(const b_undefined &, const b_undefined &)
free function comparator for b_undefined
Definition: types.hpp:236
A BSON max-key value.
Definition: types.hpp:648
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:486
A BSON date value.
Definition: types.hpp:286
A BSON min-key value.
Definition: types.hpp:632
bool operator==(const b_binary &lhs, const b_binary &rhs)
free function comparator for b_binary
Definition: types.hpp:216
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:617
A BSON JavaScript code value.
Definition: types.hpp:418
bool operator==(const b_double &lhs, const b_double &rhs)
free function comparator for b_double
Definition: types.hpp:103
bool operator==(const b_oid &lhs, const b_oid &rhs)
free function comparator for b_oid
Definition: types.hpp:254
b_symbol(T &&t)
Constructor for b_symbol.
Definition: types.hpp:469
int64_t to_int64() const
Manually convert this b_date to an int64_t.
Definition: types.hpp:320
bool operator==(const b_timestamp &lhs, const b_timestamp &rhs)
free function comparator for b_timestamp
Definition: types.hpp:570
bool operator==(const b_dbpointer &lhs, const b_dbpointer &rhs)
free function comparator for b_dbpointer
Definition: types.hpp:411
A BSON JavaScript code with scope value.
Definition: types.hpp:493
A BSON 64-bit signed integer value.
Definition: types.hpp:577
A BSON replication timestamp value.
Definition: types.hpp:558
bool operator==(const b_bool &lhs, const b_bool &rhs)
free function comparator for b_bool
Definition: types.hpp:279
b_codewscope(T &&code, U &&scope)
Constructor for b_codewscope.
Definition: types.hpp:510
bool operator==(const b_int64 &lhs, const b_int64 &rhs)
free function comparator for b_int64
Definition: types.hpp:595
A BSON document value.
Definition: types.hpp:146
bool operator==(const b_int32 &lhs, const b_int32 &rhs)
free function comparator for b_int32
Definition: types.hpp:547
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:338
b_date(std::chrono::milliseconds value)
Constructor for b_date.
Definition: types.hpp:296
A BSON boolean value.
Definition: types.hpp:261
bool operator==(const b_array &lhs, const b_array &rhs)
free function comparator for b_array
Definition: types.hpp:196
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:641
b_date(const std::chrono::system_clock::time_point &tp)
Constructor for b_date.
Definition: types.hpp:305
bool operator==(const b_utf8 &lhs, const b_utf8 &rhs)
free function comparator for b_utf8
Definition: types.hpp:139
A BSON ObjectId value.
Definition: types.hpp:243
document::view view()
Returns an unwrapped document::view.
Definition: types.hpp:161
Top level namespace for MongoDB C++ BSON functionality.
Definition: element.hpp:24
A BSON undefined value.
Definition: types.hpp:227
A BSON array value.
Definition: types.hpp:178
A variant that can contain any BSON type.
Definition: value.hpp:37
A BSON Decimal128 value.
Definition: types.hpp:602
bool operator==(const b_codewscope &lhs, const b_codewscope &rhs)
free function comparator for b_codewscope
Definition: types.hpp:522