MongoDB C++ Driver  mongocxx-3.4.0
value.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 <cstddef>
18 #include <cstdint>
19 #include <type_traits>
20 
21 #include <bsoncxx/types.hpp>
22 
23 #include <bsoncxx/config/prelude.hpp>
24 
25 namespace bsoncxx {
26 BSONCXX_INLINE_NAMESPACE_BEGIN
27 
28 namespace types {
29 
37 class BSONCXX_API value {
38  public:
42  explicit value(b_double) noexcept;
43 
47  explicit value(b_utf8) noexcept;
48 
52  explicit value(b_document) noexcept;
53 
57  explicit value(b_array) noexcept;
58 
62  explicit value(b_binary) noexcept;
63 
67  explicit value(b_undefined) noexcept;
68 
72  explicit value(b_oid) noexcept;
73 
77  explicit value(b_bool) noexcept;
78 
82  explicit value(b_date) noexcept;
83 
87  explicit value(b_null) noexcept;
88 
92  explicit value(b_regex) noexcept;
93 
97  explicit value(b_dbpointer) noexcept;
98 
102  explicit value(b_code) noexcept;
103 
107  explicit value(b_symbol) noexcept;
108 
112  explicit value(b_codewscope) noexcept;
113 
117  explicit value(b_int32) noexcept;
118 
122  explicit value(b_timestamp) noexcept;
123 
127  explicit value(b_int64) noexcept;
128 
132  explicit value(b_decimal128) noexcept;
133 
137  explicit value(b_minkey) noexcept;
138 
142  explicit value(b_maxkey) noexcept;
143 
144  value(const value&) noexcept;
145  value& operator=(const value&) noexcept;
146 
147  ~value();
148 
156  friend BSONCXX_API bool BSONCXX_CALL operator==(const value&, const value&);
157  friend BSONCXX_API bool BSONCXX_CALL operator!=(const value&, const value&);
161 
165  bsoncxx::type type() const;
166 
174  const b_double& get_double() const;
175 
183  const b_utf8& get_utf8() const;
184 
192  const b_document& get_document() const;
193 
201  const b_array& get_array() const;
202 
210  const b_binary& get_binary() const;
211 
219  const b_undefined& get_undefined() const;
220 
228  const b_oid& get_oid() const;
229 
237  const b_bool& get_bool() const;
238 
246  const b_date& get_date() const;
247 
255  const b_null& get_null() const;
256 
264  const b_regex& get_regex() const;
265 
273  const b_dbpointer& get_dbpointer() const;
274 
282  const b_code& get_code() const;
283 
291  const b_symbol& get_symbol() const;
292 
300  const b_codewscope& get_codewscope() const;
301 
309  const b_int32& get_int32() const;
310 
318  const b_timestamp& get_timestamp() const;
319 
327  const b_int64& get_int64() const;
328 
336  const b_decimal128& get_decimal128() const;
337 
345  const b_minkey& get_minkey() const;
346 
354  const b_maxkey& get_maxkey() const;
355 
356  private:
357  void BSONCXX_PRIVATE destroy() noexcept;
358 
359  bsoncxx::type _type;
360  union {
361  struct b_double _b_double;
362  struct b_utf8 _b_utf8;
363  struct b_document _b_document;
364  struct b_array _b_array;
365  struct b_binary _b_binary;
366  struct b_undefined _b_undefined;
367  struct b_oid _b_oid;
368  struct b_bool _b_bool;
369  struct b_date _b_date;
370  struct b_null _b_null;
371  struct b_regex _b_regex;
372  struct b_dbpointer _b_dbpointer;
373  struct b_code _b_code;
374  struct b_symbol _b_symbol;
375  struct b_codewscope _b_codewscope;
376  struct b_int32 _b_int32;
377  struct b_timestamp _b_timestamp;
378  struct b_int64 _b_int64;
379  struct b_decimal128 _b_decimal128;
380  struct b_minkey _b_minkey;
381  struct b_maxkey _b_maxkey;
382  };
383 };
384 
385 // sfinae in the bool return to avoid competing with the value == value
386 // operators
387 template <typename T>
388 using not_value = typename std::enable_if<
389  std::is_constructible<value, T>::value &&
390  !std::is_same<typename std::remove_reference<T>::type, value>::value,
391  bool>::type;
392 
393 // these all return bool
394 template <typename T>
395 BSONCXX_INLINE not_value<T> operator==(const value& lhs, T&& rhs) {
396  return lhs == value{std::forward<T>(rhs)};
397 }
398 
399 template <typename T>
400 BSONCXX_INLINE not_value<T> operator==(T&& lhs, const value& rhs) {
401  return value{std::forward<T>(lhs)} == rhs;
402 }
403 
404 template <typename T>
405 BSONCXX_INLINE not_value<T> operator!=(const value& lhs, T&& rhs) {
406  return lhs != value{std::forward<T>(rhs)};
407 }
408 
409 template <typename T>
410 BSONCXX_INLINE not_value<T> operator!=(T&& lhs, const value& rhs) {
411  return value{std::forward<T>(lhs)} != rhs;
412 }
413 
414 } // namespace types
415 
416 BSONCXX_INLINE_NAMESPACE_END
417 } // namespace bsoncxx
418 
419 #include <bsoncxx/config/postlude.hpp>
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
A BSON null value.
Definition: types.hpp:345
A BSON regex value.
Definition: types.hpp:361
A BSON binary data value.
Definition: types.hpp:203
A BSON DBPointer value.
Definition: types.hpp:399
A BSON max-key value.
Definition: types.hpp:648
A BSON UTF-8 encoded string value.
Definition: types.hpp:110
A BSON date value.
Definition: types.hpp:286
A BSON min-key value.
Definition: types.hpp:632
A BSON JavaScript code value.
Definition: types.hpp:418
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
A BSON document value.
Definition: types.hpp:146
type
An enumeration of each BSON type.
Definition: types.hpp:39
A BSON boolean value.
Definition: types.hpp:261
A BSON ObjectId value.
Definition: types.hpp:243
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