MongoDB C++ Driver  mongocxx-3.0.3
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/document/view.hpp>
22 #include <bsoncxx/oid.hpp>
23 #include <bsoncxx/stdx/string_view.hpp>
24 
25 #include <bsoncxx/config/prelude.hpp>
26 
27 namespace bsoncxx {
28 BSONCXX_INLINE_NAMESPACE_BEGIN
29 
38 enum class type : std::uint8_t {
39 #define BSONCXX_ENUM(name, val) k_##name = val,
40 #include <bsoncxx/enums/type.hpp>
41 #undef BSONCXX_ENUM
42 };
43 
53 enum class binary_sub_type : std::uint8_t {
54 #define BSONCXX_ENUM(name, val) k_##name = val,
55 #include <bsoncxx/enums/binary_sub_type.hpp>
56 #undef BSONCXX_ENUM
57 };
58 
67 BSONCXX_API std::string BSONCXX_CALL to_string(type rhs);
68 
77 BSONCXX_API std::string BSONCXX_CALL to_string(binary_sub_type rhs);
78 
79 namespace types {
80 
84 struct BSONCXX_API b_double {
85  static constexpr auto type_id = type::k_double;
86 
87  double value;
88 
92  BSONCXX_INLINE operator double() {
93  return value;
94  }
95 };
96 
102 BSONCXX_INLINE bool operator==(const b_double& lhs, const b_double& rhs) {
103  return lhs.value == rhs.value;
104 }
105 
109 struct BSONCXX_API b_utf8 {
110  static constexpr auto type_id = type::k_utf8;
111 
118  template <typename T,
119  typename std::enable_if<!std::is_same<b_utf8, typename std::decay<T>::type>::value,
120  int>::type = 0>
121  BSONCXX_INLINE explicit b_utf8(T&& t)
122  : value(std::forward<T>(t)) {
123  }
124 
125  stdx::string_view value;
126 
130  BSONCXX_INLINE operator stdx::string_view() {
131  return value;
132  }
133 };
134 
140 BSONCXX_INLINE bool operator==(const b_utf8& lhs, const b_utf8& rhs) {
141  return lhs.value == rhs.value;
142 }
143 
147 struct BSONCXX_API b_document {
148  static constexpr auto type_id = type::k_document;
149 
151 
155  BSONCXX_INLINE operator document::view() {
156  return value;
157  }
158 
162  BSONCXX_INLINE document::view view() {
163  return value;
164  }
165 };
166 
172 BSONCXX_INLINE bool operator==(const b_document& lhs, const b_document& rhs) {
173  return lhs.value == rhs.value;
174 }
175 
179 struct BSONCXX_API b_array {
180  static constexpr auto type_id = type::k_array;
181 
183 
187  BSONCXX_INLINE operator array::view() {
188  return value;
189  }
190 };
191 
197 BSONCXX_INLINE bool operator==(const b_array& lhs, const b_array& rhs) {
198  return lhs.value == rhs.value;
199 }
200 
204 struct BSONCXX_API b_binary {
205  static constexpr auto type_id = type::k_binary;
206 
207  binary_sub_type sub_type;
208  uint32_t size;
209  const uint8_t* bytes;
210 };
211 
217 BSONCXX_INLINE bool operator==(const b_binary& lhs, const b_binary& rhs) {
218  return lhs.sub_type == rhs.sub_type && lhs.size == rhs.size &&
219  (std::memcmp(lhs.bytes, rhs.bytes, lhs.size) == 0);
220 }
221 
228 struct BSONCXX_API b_undefined {
229  static constexpr auto type_id = type::k_undefined;
230 };
231 
237 BSONCXX_INLINE bool operator==(const b_undefined&, const b_undefined&) {
238  return true;
239 }
240 
244 struct BSONCXX_API b_oid {
245  static constexpr auto type_id = type::k_oid;
246 
247  oid value;
248 };
249 
255 BSONCXX_INLINE bool operator==(const b_oid& lhs, const b_oid& rhs) {
256  return lhs.value == rhs.value;
257 }
258 
262 struct BSONCXX_API b_bool {
263  static constexpr auto type_id = type::k_bool;
264 
265  bool value;
266 
270  BSONCXX_INLINE operator bool() {
271  return value;
272  }
273 };
274 
280 BSONCXX_INLINE bool operator==(const b_bool& lhs, const b_bool& rhs) {
281  return lhs.value == rhs.value;
282 }
283 
287 struct BSONCXX_API b_date {
288  static constexpr auto type_id = type::k_date;
289 
296  BSONCXX_INLINE
297  explicit b_date(std::chrono::milliseconds value) : value(value) {
298  }
299 
306  BSONCXX_INLINE
307  explicit b_date(const std::chrono::system_clock::time_point& tp)
308  : value(std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch())) {
309  }
310 
311  std::chrono::milliseconds value;
312 
316  BSONCXX_INLINE operator int64_t() {
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() {
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, typename U>
377  BSONCXX_INLINE explicit b_regex(T&& regex, U&& options)
378  : regex(std::forward<T>(regex)), options(std::forward<U>(options)) {
379  }
380 
381  stdx::string_view regex;
382  stdx::string_view options;
383 };
384 
390 BSONCXX_INLINE bool operator==(const b_regex& lhs, const b_regex& rhs) {
391  return lhs.regex == rhs.regex && lhs.options == rhs.options;
392 }
393 
400 struct BSONCXX_API b_dbpointer {
401  static constexpr auto type_id = type::k_dbpointer;
402 
403  stdx::string_view collection;
404  oid value;
405 };
406 
412 BSONCXX_INLINE bool operator==(const b_dbpointer& lhs, const b_dbpointer& rhs) {
413  return lhs.collection == rhs.collection && lhs.value == rhs.value;
414 }
415 
419 struct BSONCXX_API b_code {
420  static constexpr auto type_id = type::k_code;
421 
428  template <typename T,
429  typename std::enable_if<!std::is_same<b_code, typename std::decay<T>::type>::value,
430  int>::type = 0>
431  BSONCXX_INLINE explicit b_code(T&& t)
432  : code(std::forward<T>(t)) {
433  }
434 
435  stdx::string_view code;
436 
440  BSONCXX_INLINE operator stdx::string_view() {
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)
473  : symbol(std::forward<T>(t)) {
474  }
475 
476  stdx::string_view symbol;
477 
481  BSONCXX_INLINE operator stdx::string_view() {
482  return symbol;
483  }
484 };
485 
491 BSONCXX_INLINE bool operator==(const b_symbol& lhs, const b_symbol& rhs) {
492  return lhs.symbol == rhs.symbol;
493 }
494 
498 struct BSONCXX_API b_codewscope {
499  static constexpr auto type_id = type::k_codewscope;
500 
510  template <typename T, typename U>
511  BSONCXX_INLINE explicit b_codewscope(T&& code, U&& scope)
512  : code(std::forward<T>(code)), scope(std::forward<U>(scope)) {
513  }
514 
515  stdx::string_view code;
516  document::view scope;
517 };
518 
524 BSONCXX_INLINE bool operator==(const b_codewscope& lhs, const b_codewscope& rhs) {
525  return lhs.code == rhs.code && lhs.scope == rhs.scope;
526 }
527 
531 struct BSONCXX_API b_int32 {
532  static constexpr auto type_id = type::k_int32;
533 
534  int32_t value;
535 
539  BSONCXX_INLINE operator int32_t() {
540  return value;
541  }
542 };
543 
549 BSONCXX_INLINE bool operator==(const b_int32& lhs, const b_int32& rhs) {
550  return lhs.value == rhs.value;
551 }
552 
560 struct BSONCXX_API b_timestamp {
561  static constexpr auto type_id = type::k_timestamp;
562 
563  uint32_t increment;
564  uint32_t timestamp;
565 };
566 
572 BSONCXX_INLINE bool operator==(const b_timestamp& lhs, const b_timestamp& rhs) {
573  return lhs.increment == rhs.increment && lhs.timestamp == rhs.timestamp;
574 }
575 
579 struct BSONCXX_API b_int64 {
580  static constexpr auto type_id = type::k_int64;
581 
582  int64_t value;
583 
587  BSONCXX_INLINE operator int64_t() {
588  return value;
589  }
590 };
591 
597 BSONCXX_INLINE bool operator==(const b_int64& lhs, const b_int64& rhs) {
598  return lhs.value == rhs.value;
599 }
600 
604 struct BSONCXX_API b_minkey {
605  static constexpr auto type_id = type::k_minkey;
606 };
607 
613 BSONCXX_INLINE bool operator==(const b_minkey&, const b_minkey&) {
614  return true;
615 }
616 
620 struct BSONCXX_API b_maxkey {
621  static constexpr auto type_id = type::k_maxkey;
622 };
623 
629 BSONCXX_INLINE bool operator==(const b_maxkey&, const b_maxkey&) {
630  return true;
631 }
632 
633 #define BSONCXX_ENUM(name, val) \
634  BSONCXX_INLINE bool operator!=(const b_##name& lhs, const b_##name& rhs) { \
635  return !(lhs == rhs); \
636  }
637 #include <bsoncxx/enums/type.hpp>
638 #undef BSONCXX_ENUM
639 
640 } // namespace types
641 BSONCXX_INLINE_NAMESPACE_END
642 } // namespace bsoncxx
643 
644 #include <bsoncxx/config/postlude.hpp>
bool operator==(const b_document &lhs, const b_document &rhs)
free function comparator for b_document
Definition: types.hpp:172
bool operator==(const b_maxkey &, const b_maxkey &)
free function comparator for b_maxkey
Definition: types.hpp:629
A BSON signed 32-bit integer value.
Definition: types.hpp:531
A BSON double value.
Definition: types.hpp:84
A BSON Symbol value.
Definition: types.hpp:460
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:121
bool operator==(const b_regex &lhs, const b_regex &rhs)
free function comparator for b_regex
Definition: types.hpp:390
Definition: error_code.hpp:69
A BSON null value.
Definition: types.hpp:348
b_code(T &&t)
Constructor for b_code.
Definition: types.hpp:431
A BSON regex value.
Definition: types.hpp:364
A BSON binary data value.
Definition: types.hpp:204
bool operator==(const b_null &, const b_null &)
free function comparator for b_null
Definition: types.hpp:357
bool operator==(const b_code &lhs, const b_code &rhs)
free function comparator for b_code
Definition: types.hpp:450
A BSON DBPointer value.
Definition: types.hpp:400
bool operator==(const b_undefined &, const b_undefined &)
free function comparator for b_undefined
Definition: types.hpp:237
A BSON max-key value.
Definition: types.hpp:620
A BSON UTF-8 encoded string value.
Definition: types.hpp:109
bool operator==(const b_symbol &lhs, const b_symbol &rhs)
free function comparator for b_symbol
Definition: types.hpp:491
A BSON date value.
Definition: types.hpp:287
A BSON min-key value.
Definition: types.hpp:604
bool operator==(const b_binary &lhs, const b_binary &rhs)
free function comparator for b_binary
Definition: types.hpp:217
A read-only, non-owning view of a BSON document.
Definition: view.hpp:33
A BSON JavaScript code value.
Definition: types.hpp:419
bool operator==(const b_double &lhs, const b_double &rhs)
free function comparator for b_double
Definition: types.hpp:102
b_codewscope(T &&code, U &&scope)
Constructor for b_codewscope.
Definition: types.hpp:511
bool operator==(const b_oid &lhs, const b_oid &rhs)
free function comparator for b_oid
Definition: types.hpp:255
b_symbol(T &&t)
Constructor for b_symbol.
Definition: types.hpp:472
bool operator==(const b_timestamp &lhs, const b_timestamp &rhs)
free function comparator for b_timestamp
Definition: types.hpp:572
bool operator==(const b_dbpointer &lhs, const b_dbpointer &rhs)
free function comparator for b_dbpointer
Definition: types.hpp:412
A BSON JavaScript code with scope value.
Definition: types.hpp:498
A BSON 64-bit signed integer value.
Definition: types.hpp:579
A BSON replication timestamp value.
Definition: types.hpp:560
bool operator==(const b_bool &lhs, const b_bool &rhs)
free function comparator for b_bool
Definition: types.hpp:280
int64_t to_int64() const
Manually convert this b_date to an int64_t.
Definition: types.hpp:323
bool operator==(const b_int64 &lhs, const b_int64 &rhs)
free function comparator for b_int64
Definition: types.hpp:597
A BSON document value.
Definition: types.hpp:147
bool operator==(const b_int32 &lhs, const b_int32 &rhs)
free function comparator for b_int32
Definition: types.hpp:549
bool operator==(const b_date &lhs, const b_date &rhs)
free function comparator for b_date
Definition: types.hpp:341
b_date(std::chrono::milliseconds value)
Constructor for b_date.
Definition: types.hpp:297
A BSON boolean value.
Definition: types.hpp:262
bool operator==(const b_array &lhs, const b_array &rhs)
free function comparator for b_array
Definition: types.hpp:197
bool operator==(const b_minkey &, const b_minkey &)
free function comparator for b_minkey
Definition: types.hpp:613
b_date(const std::chrono::system_clock::time_point &tp)
Constructor for b_date.
Definition: types.hpp:307
bool operator==(const b_utf8 &lhs, const b_utf8 &rhs)
free function comparator for b_utf8
Definition: types.hpp:140
A BSON ObjectId value.
Definition: types.hpp:244
document::view view()
Returns an unwrapped document::view.
Definition: types.hpp:162
b_regex(T &&regex, U &&options)
Constructor for b_regex.
Definition: types.hpp:377
Definition: element.hpp:24
A BSON undefined value.
Definition: types.hpp:228
A BSON array value.
Definition: types.hpp:179
A variant that can contain any BSON type.
Definition: value.hpp:37
bool operator==(const b_codewscope &lhs, const b_codewscope &rhs)
free function comparator for b_codewscope
Definition: types.hpp:524