MongoDB C++ Driver  mongocxx-3.0.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 
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  BSONCXX_INLINE explicit b_utf8(T&& value)
120  : value(std::forward<T>(value)) {
121  }
122 
123  stdx::string_view value;
124 
128  BSONCXX_INLINE operator stdx::string_view() {
129  return value;
130  }
131 };
132 
138 BSONCXX_INLINE bool operator==(const b_utf8& lhs, const b_utf8& rhs) {
139  return lhs.value == rhs.value;
140 }
141 
145 struct BSONCXX_API b_document {
146  static constexpr auto type_id = type::k_document;
147 
149 
153  BSONCXX_INLINE operator document::view() {
154  return value;
155  }
156 
160  BSONCXX_INLINE document::view view() {
161  return value;
162  }
163 };
164 
170 BSONCXX_INLINE bool operator==(const b_document& lhs, const b_document& rhs) {
171  return lhs.value == rhs.value;
172 }
173 
177 struct BSONCXX_API b_array {
178  static constexpr auto type_id = type::k_array;
179 
181 
185  BSONCXX_INLINE operator array::view() {
186  return value;
187  }
188 };
189 
195 BSONCXX_INLINE bool operator==(const b_array& lhs, const b_array& rhs) {
196  return lhs.value == rhs.value;
197 }
198 
202 struct BSONCXX_API b_binary {
203  static constexpr auto type_id = type::k_binary;
204 
205  binary_sub_type sub_type;
206  uint32_t size;
207  const uint8_t* bytes;
208 };
209 
215 BSONCXX_INLINE bool operator==(const b_binary& lhs, const b_binary& rhs) {
216  return lhs.sub_type == rhs.sub_type && lhs.size == rhs.size &&
217  (std::memcmp(lhs.bytes, rhs.bytes, lhs.size) == 0);
218 }
219 
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() {
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(int64_t value) : value(value) {
297  }
298 
305  BSONCXX_INLINE
306  explicit b_date(const std::chrono::system_clock::time_point& tp)
307  : value(std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch())
308  .count()) {
309  }
310 
311  int64_t value;
312 
316  BSONCXX_INLINE operator int64_t() {
317  return value;
318  }
319 
323  BSONCXX_INLINE operator std::chrono::system_clock::time_point() {
324  return std::chrono::system_clock::time_point(
325  std::chrono::duration_cast<std::chrono::system_clock::duration>(
326  std::chrono::milliseconds{value}));
327  }
328 };
329 
335 BSONCXX_INLINE bool operator==(const b_date& lhs, const b_date& rhs) {
336  return lhs.value == rhs.value;
337 }
338 
346 struct BSONCXX_API b_null {
347  static constexpr auto type_id = type::k_null;
348 };
349 
355 BSONCXX_INLINE bool operator==(const b_null&, const b_null&) {
356  return true;
357 }
358 
366 struct BSONCXX_API b_regex {
367  static constexpr auto type_id = type::k_regex;
368 
378  template <typename T, typename U>
379  BSONCXX_INLINE explicit b_regex(T&& regex, U&& options)
380  : regex(std::forward<T>(regex)), options(std::forward<U>(options)) {
381  }
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 
425 struct BSONCXX_API b_code {
426  static constexpr auto type_id = type::k_code;
427 
434  template <typename T>
435  BSONCXX_INLINE explicit b_code(T&& code)
436  : code(std::forward<T>(code)) {
437  }
438 
439  stdx::string_view code;
440 
444  BSONCXX_INLINE operator stdx::string_view() {
445  return code;
446  }
447 };
448 
454 BSONCXX_INLINE bool operator==(const b_code& lhs, const b_code& rhs) {
455  return lhs.code == rhs.code;
456 }
457 
465 struct BSONCXX_API b_symbol {
466  static constexpr auto type_id = type::k_symbol;
467 
474  template <typename T>
475  BSONCXX_INLINE explicit b_symbol(T&& symbol)
476  : symbol(std::forward<T>(symbol)) {
477  }
478 
479  stdx::string_view symbol;
480 
484  BSONCXX_INLINE operator stdx::string_view() {
485  return symbol;
486  }
487 };
488 
494 BSONCXX_INLINE bool operator==(const b_symbol& lhs, const b_symbol& rhs) {
495  return lhs.symbol == rhs.symbol;
496 }
497 
505 struct BSONCXX_API b_codewscope {
506  static constexpr auto type_id = type::k_codewscope;
507 
517  template <typename T, typename U>
518  BSONCXX_INLINE explicit b_codewscope(T&& code, U&& scope)
519  : code(std::forward<T>(code)), scope(std::forward<U>(scope)) {
520  }
521 
522  stdx::string_view code;
523  document::view scope;
524 };
525 
531 BSONCXX_INLINE bool operator==(const b_codewscope& lhs, const b_codewscope& rhs) {
532  return lhs.code == rhs.code && lhs.scope == rhs.scope;
533 }
534 
538 struct BSONCXX_API b_int32 {
539  static constexpr auto type_id = type::k_int32;
540 
541  int32_t value;
542 
546  BSONCXX_INLINE operator int32_t() {
547  return value;
548  }
549 };
550 
556 BSONCXX_INLINE bool operator==(const b_int32& lhs, const b_int32& rhs) {
557  return lhs.value == rhs.value;
558 }
559 
567 struct BSONCXX_API b_timestamp {
568  static constexpr auto type_id = type::k_timestamp;
569 
570  uint32_t increment;
571  uint32_t timestamp;
572 };
573 
579 BSONCXX_INLINE bool operator==(const b_timestamp& lhs, const b_timestamp& rhs) {
580  return lhs.increment == rhs.increment && lhs.timestamp == rhs.timestamp;
581 }
582 
586 struct BSONCXX_API b_int64 {
587  static constexpr auto type_id = type::k_int64;
588 
589  int64_t value;
590 
594  BSONCXX_INLINE operator int64_t() {
595  return value;
596  }
597 };
598 
604 BSONCXX_INLINE bool operator==(const b_int64& lhs, const b_int64& rhs) {
605  return lhs.value == rhs.value;
606 }
607 
615 struct BSONCXX_API b_minkey {
616  static constexpr auto type_id = type::k_minkey;
617 };
618 
624 BSONCXX_INLINE bool operator==(const b_minkey&, const b_minkey&) {
625  return true;
626 }
627 
635 struct BSONCXX_API b_maxkey {
636  static constexpr auto type_id = type::k_maxkey;
637 };
638 
644 BSONCXX_INLINE bool operator==(const b_maxkey&, const b_maxkey&) {
645  return true;
646 }
647 
648 #define BSONCXX_ENUM(name, val) \
649  BSONCXX_INLINE bool operator!=(const b_##name& lhs, const b_##name& rhs) { \
650  return !(lhs == rhs); \
651  }
652 #include <bsoncxx/enums/type.hpp>
653 #undef BSONCXX_ENUM
654 
655 } // namespace types
656 BSONCXX_INLINE_NAMESPACE_END
657 } // namespace bsoncxx
658 
659 #include <bsoncxx/config/postlude.hpp>
bool operator==(const b_document &lhs, const b_document &rhs)
free function comparator for b_document
Definition: types.hpp:170
bool operator==(const b_maxkey &, const b_maxkey &)
free function comparator for b_maxkey
Definition: types.hpp:644
A BSON signed 32-bit integer value.
Definition: types.hpp:538
A BSON double value.
Definition: types.hpp:84
A BSON JavaScript code value.
Definition: types.hpp:465
Represents a MongoDB ObjectId.
Definition: oid.hpp:38
A read-only, non-owning view of a BSON document.
Definition: view.hpp:33
bool operator==(const b_regex &lhs, const b_regex &rhs)
free function comparator for b_regex
Definition: types.hpp:392
Definition: error_code.hpp:67
b_date(int64_t value)
Constructor for b_date.
Definition: types.hpp:296
A BSON null value.
Definition: types.hpp:346
A BSON regex value.
Definition: types.hpp:366
A BSON binary data value.
Definition: types.hpp:202
bool operator==(const b_null &, const b_null &)
free function comparator for b_null
Definition: types.hpp:355
bool operator==(const b_code &lhs, const b_code &rhs)
free function comparator for b_code
Definition: types.hpp:454
A BSON DBPointer value.
Definition: types.hpp:402
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:635
b_utf8(T &&value)
Constructor for b_utf8.
Definition: types.hpp:119
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:494
A BSON date value.
Definition: types.hpp:286
A BSON min-key value.
Definition: types.hpp:615
bool operator==(const b_binary &lhs, const b_binary &rhs)
free function comparator for b_binary
Definition: types.hpp:215
A read-only, non-owning view of a BSON document.
Definition: view.hpp:33
A BSON JavaScript code value.
Definition: types.hpp:425
b_code(T &&code)
Constructor for b_code.
Definition: types.hpp:435
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:518
bool operator==(const b_oid &lhs, const b_oid &rhs)
free function comparator for b_oid
Definition: types.hpp:254
bool operator==(const b_timestamp &lhs, const b_timestamp &rhs)
free function comparator for b_timestamp
Definition: types.hpp:579
bool operator==(const b_dbpointer &lhs, const b_dbpointer &rhs)
free function comparator for b_dbpointer
Definition: types.hpp:414
A BSON JavaScript code with scope value.
Definition: types.hpp:505
A BSON 64-bit signed integer value.
Definition: types.hpp:586
A BSON replication timestamp value.
Definition: types.hpp:567
bool operator==(const b_bool &lhs, const b_bool &rhs)
free function comparator for b_bool
Definition: types.hpp:279
bool operator==(const b_int64 &lhs, const b_int64 &rhs)
free function comparator for b_int64
Definition: types.hpp:604
A BSON document value.
Definition: types.hpp:145
bool operator==(const b_int32 &lhs, const b_int32 &rhs)
free function comparator for b_int32
Definition: types.hpp:556
bool operator==(const b_date &lhs, const b_date &rhs)
free function comparator for b_date
Definition: types.hpp:335
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:195
bool operator==(const b_minkey &, const b_minkey &)
free function comparator for b_minkey
Definition: types.hpp:624
b_symbol(T &&symbol)
Constructor for b_symbol.
Definition: types.hpp:475
b_date(const std::chrono::system_clock::time_point &tp)
Constructor for b_date.
Definition: types.hpp:306
bool operator==(const b_utf8 &lhs, const b_utf8 &rhs)
free function comparator for b_utf8
Definition: types.hpp:138
A BSON ObjectId value.
Definition: types.hpp:243
document::view view()
Returns an unwrapped document::view.
Definition: types.hpp:160
b_regex(T &&regex, U &&options)
Constructor for b_regex.
Definition: types.hpp:379
Definition: element.hpp:24
A BSON undefined value.
Definition: types.hpp:227
A BSON array value.
Definition: types.hpp:177
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:531