MongoDB C++ Driver mongocxx-3.6.7
Loading...
Searching...
No Matches
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
28namespace bsoncxx {
29BSONCXX_INLINE_NAMESPACE_BEGIN
30
39enum 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
57enum 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
71BSONCXX_API std::string BSONCXX_CALL to_string(type rhs);
72
81BSONCXX_API std::string BSONCXX_CALL to_string(binary_sub_type rhs);
82
83namespace types {
84
88struct 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
106BSONCXX_INLINE bool operator==(const b_double& lhs, const b_double& rhs) {
107 return lhs.value == rhs.value;
108}
109
113struct 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
142BSONCXX_INLINE bool operator==(const b_utf8& lhs, const b_utf8& rhs) {
143 return lhs.value == rhs.value;
144}
145
149struct BSONCXX_API b_document {
150 static constexpr auto type_id = type::k_document;
151
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
174BSONCXX_INLINE bool operator==(const b_document& lhs, const b_document& rhs) {
175 return lhs.value == rhs.value;
176}
177
181struct BSONCXX_API b_array {
182 static constexpr auto type_id = type::k_array;
183
185
189 BSONCXX_INLINE operator array::view() const {
190 return value;
191 }
192};
193
199BSONCXX_INLINE bool operator==(const b_array& lhs, const b_array& rhs) {
200 return lhs.value == rhs.value;
201}
202
206struct 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
219BSONCXX_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
230struct BSONCXX_API b_undefined {
231 static constexpr auto type_id = type::k_undefined;
232};
233
239BSONCXX_INLINE bool operator==(const b_undefined&, const b_undefined&) {
240 return true;
241}
242
246struct BSONCXX_API b_oid {
247 static constexpr auto type_id = type::k_oid;
248
249 oid value;
250};
251
257BSONCXX_INLINE bool operator==(const b_oid& lhs, const b_oid& rhs) {
258 return lhs.value == rhs.value;
259}
260
264struct 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
282BSONCXX_INLINE bool operator==(const b_bool& lhs, const b_bool& rhs) {
283 return lhs.value == rhs.value;
284}
285
289struct 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
341BSONCXX_INLINE bool operator==(const b_date& lhs, const b_date& rhs) {
342 return lhs.value == rhs.value;
343}
344
348struct BSONCXX_API b_null {
349 static constexpr auto type_id = type::k_null;
350};
351
357BSONCXX_INLINE bool operator==(const b_null&, const b_null&) {
358 return true;
359}
360
364struct 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
392BSONCXX_INLINE bool operator==(const b_regex& lhs, const b_regex& rhs) {
393 return lhs.regex == rhs.regex && lhs.options == rhs.options;
394}
395
402struct BSONCXX_API b_dbpointer {
403 static constexpr auto type_id = type::k_dbpointer;
404
405 stdx::string_view collection;
406 oid value;
407};
408
414BSONCXX_INLINE bool operator==(const b_dbpointer& lhs, const b_dbpointer& rhs) {
415 return lhs.collection == rhs.collection && lhs.value == rhs.value;
416}
417
421struct 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
450BSONCXX_INLINE bool operator==(const b_code& lhs, const b_code& rhs) {
451 return lhs.code == rhs.code;
452}
453
460struct 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
489BSONCXX_INLINE bool operator==(const b_symbol& lhs, const b_symbol& rhs) {
490 return lhs.symbol == rhs.symbol;
491}
492
496struct 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
525BSONCXX_INLINE bool operator==(const b_codewscope& lhs, const b_codewscope& rhs) {
526 return lhs.code == rhs.code && lhs.scope == rhs.scope;
527}
528
532struct 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
550BSONCXX_INLINE bool operator==(const b_int32& lhs, const b_int32& rhs) {
551 return lhs.value == rhs.value;
552}
553
561struct BSONCXX_API b_timestamp {
562 static constexpr auto type_id = type::k_timestamp;
563
564 uint32_t increment;
565 uint32_t timestamp;
566};
567
573BSONCXX_INLINE bool operator==(const b_timestamp& lhs, const b_timestamp& rhs) {
574 return lhs.increment == rhs.increment && lhs.timestamp == rhs.timestamp;
575}
576
580struct 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
598BSONCXX_INLINE bool operator==(const b_int64& lhs, const b_int64& rhs) {
599 return lhs.value == rhs.value;
600}
601
605struct BSONCXX_API b_decimal128 {
606 static constexpr auto type_id = type::k_decimal128;
607
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
628BSONCXX_INLINE bool operator==(const b_decimal128& lhs, const b_decimal128& rhs) {
629 return lhs.value == rhs.value;
630}
631
635struct BSONCXX_API b_minkey {
636 static constexpr auto type_id = type::k_minkey;
637};
638
644BSONCXX_INLINE bool operator==(const b_minkey&, const b_minkey&) {
645 return true;
646}
647
651struct BSONCXX_API b_maxkey {
652 static constexpr auto type_id = type::k_maxkey;
653};
654
660BSONCXX_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
672BSONCXX_INLINE_NAMESPACE_END
673} // namespace bsoncxx
674
675#include <bsoncxx/config/postlude.hpp>
A read-only, non-owning view of a BSON document.
Definition view.hpp:33
Represents an IEEE 754-2008 BSON Decimal128 value in a platform-independent way.
Definition decimal128.hpp:30
A read-only, non-owning view of a BSON document.
Definition view.hpp:33
Represents a MongoDB ObjectId.
Definition oid.hpp:38
A view-only variant that can contain any BSON type.
Definition view.hpp:44
Top level namespace for MongoDB C++ BSON functionality.
Definition element.hpp:24
type
An enumeration of each BSON type.
Definition types.hpp:39
std::string to_string(type rhs)
Returns a stringification of the given type.
binary_sub_type
An enumeration of each BSON binary sub type.
Definition types.hpp:57
A BSON array value.
Definition types.hpp:181
bool operator==(const b_array &lhs, const b_array &rhs)
free function comparator for b_array
Definition types.hpp:199
A BSON binary data value.
Definition types.hpp:206
bool operator==(const b_binary &lhs, const b_binary &rhs)
free function comparator for b_binary
Definition types.hpp:219
A BSON boolean value.
Definition types.hpp:264
bool operator==(const b_bool &lhs, const b_bool &rhs)
free function comparator for b_bool
Definition types.hpp:282
A BSON JavaScript code value.
Definition types.hpp:421
bool operator==(const b_code &lhs, const b_code &rhs)
free function comparator for b_code
Definition types.hpp:450
b_code(T &&t)
Constructor for b_code.
Definition types.hpp:433
A BSON JavaScript code with scope value.
Definition types.hpp:496
b_codewscope(T &&code, U &&scope)
Constructor for b_codewscope.
Definition types.hpp:513
bool operator==(const b_codewscope &lhs, const b_codewscope &rhs)
free function comparator for b_codewscope
Definition types.hpp:525
A BSON date value.
Definition types.hpp:289
int64_t to_int64() const
Manually convert this b_date to an int64_t.
Definition types.hpp:323
b_date(const std::chrono::system_clock::time_point &tp)
Constructor for b_date.
Definition types.hpp:308
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:299
A BSON DBPointer value.
Definition types.hpp:402
bool operator==(const b_dbpointer &lhs, const b_dbpointer &rhs)
free function comparator for b_dbpointer
Definition types.hpp:414
A BSON Decimal128 value.
Definition types.hpp:605
bool operator==(const b_decimal128 &lhs, const b_decimal128 &rhs)
free function comparator for b_decimal128
Definition types.hpp:628
b_decimal128(T &&t)
Constructor for b_decimal128.
Definition types.hpp:620
A BSON document value.
Definition types.hpp:149
bool operator==(const b_document &lhs, const b_document &rhs)
free function comparator for b_document
Definition types.hpp:174
document::view view()
Returns an unwrapped document::view.
Definition types.hpp:164
A BSON double value.
Definition types.hpp:88
bool operator==(const b_double &lhs, const b_double &rhs)
free function comparator for b_double
Definition types.hpp:106
A BSON signed 32-bit integer value.
Definition types.hpp:532
bool operator==(const b_int32 &lhs, const b_int32 &rhs)
free function comparator for b_int32
Definition types.hpp:550
A BSON 64-bit signed integer value.
Definition types.hpp:580
bool operator==(const b_int64 &lhs, const b_int64 &rhs)
free function comparator for b_int64
Definition types.hpp:598
A BSON max-key value.
Definition types.hpp:651
bool operator==(const b_maxkey &, const b_maxkey &)
free function comparator for b_maxkey
Definition types.hpp:660
A BSON min-key value.
Definition types.hpp:635
bool operator==(const b_minkey &, const b_minkey &)
free function comparator for b_minkey
Definition types.hpp:644
A BSON null value.
Definition types.hpp:348
bool operator==(const b_null &, const b_null &)
free function comparator for b_null
Definition types.hpp:357
A BSON ObjectId value.
Definition types.hpp:246
bool operator==(const b_oid &lhs, const b_oid &rhs)
free function comparator for b_oid
Definition types.hpp:257
A BSON regex value.
Definition types.hpp:364
bool operator==(const b_regex &lhs, const b_regex &rhs)
free function comparator for b_regex
Definition types.hpp:392
b_regex(T &&regex, U &&options=U{})
Constructor for b_regex.
Definition types.hpp:380
A BSON Symbol value.
Definition types.hpp:460
bool operator==(const b_symbol &lhs, const b_symbol &rhs)
free function comparator for b_symbol
Definition types.hpp:489
b_symbol(T &&t)
Constructor for b_symbol.
Definition types.hpp:472
A BSON replication timestamp value.
Definition types.hpp:561
bool operator==(const b_timestamp &lhs, const b_timestamp &rhs)
free function comparator for b_timestamp
Definition types.hpp:573
A BSON undefined value.
Definition types.hpp:230
bool operator==(const b_undefined &, const b_undefined &)
free function comparator for b_undefined
Definition types.hpp:239
A BSON UTF-8 encoded string value.
Definition types.hpp:113
b_utf8(T &&t)
Constructor for b_utf8.
Definition types.hpp:125
bool operator==(const b_utf8 &lhs, const b_utf8 &rhs)
free function comparator for b_utf8
Definition types.hpp:142