MongoDB C++ Driver  mongocxx-3.0.2
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_minkey) noexcept;
133 
137  explicit value(b_maxkey) noexcept;
138 
139  value(const value&) noexcept;
140  value& operator=(const value&) noexcept;
141 
142  ~value();
143 
151  friend BSONCXX_API bool BSONCXX_CALL operator==(const value&, const value&);
152  friend BSONCXX_API bool BSONCXX_CALL operator!=(const value&, const value&);
156 
160  bsoncxx::type type() const;
161 
169  const b_double& get_double() const;
170 
178  const b_utf8& get_utf8() const;
179 
187  const b_document& get_document() const;
188 
196  const b_array& get_array() const;
197 
205  const b_binary& get_binary() const;
206 
214  const b_undefined& get_undefined() const;
215 
223  const b_oid& get_oid() const;
224 
232  const b_bool& get_bool() const;
233 
241  const b_date& get_date() const;
242 
250  const b_null& get_null() const;
251 
259  const b_regex& get_regex() const;
260 
268  const b_dbpointer& get_dbpointer() const;
269 
277  const b_code& get_code() const;
278 
286  const b_symbol& get_symbol() const;
287 
295  const b_codewscope& get_codewscope() const;
296 
304  const b_int32& get_int32() const;
305 
313  const b_timestamp& get_timestamp() const;
314 
322  const b_int64& get_int64() const;
323 
331  const b_minkey& get_minkey() const;
332 
340  const b_maxkey& get_maxkey() const;
341 
342  private:
343  void BSONCXX_PRIVATE destroy() noexcept;
344 
345  bsoncxx::type _type;
346  union {
347  struct b_double _b_double;
348  struct b_utf8 _b_utf8;
349  struct b_document _b_document;
350  struct b_array _b_array;
351  struct b_binary _b_binary;
352  struct b_undefined _b_undefined;
353  struct b_oid _b_oid;
354  struct b_bool _b_bool;
355  struct b_date _b_date;
356  struct b_null _b_null;
357  struct b_regex _b_regex;
358  struct b_dbpointer _b_dbpointer;
359  struct b_code _b_code;
360  struct b_symbol _b_symbol;
361  struct b_codewscope _b_codewscope;
362  struct b_int32 _b_int32;
363  struct b_timestamp _b_timestamp;
364  struct b_int64 _b_int64;
365  struct b_minkey _b_minkey;
366  struct b_maxkey _b_maxkey;
367  };
368 };
369 
370 // sfinae in the bool return to avoid competing with the value == value
371 // operators
372 template <typename T>
373 using not_value =
374  typename std::enable_if<!std::is_same<typename std::remove_reference<T>::type, value>::value,
375  bool>::type;
376 
377 // these all return bool
378 template <typename T>
379 BSONCXX_INLINE not_value<T> operator==(const value& lhs, T&& rhs) {
380  return lhs == value{std::forward<T>(rhs)};
381 }
382 
383 template <typename T>
384 BSONCXX_INLINE not_value<T> operator==(T&& lhs, const value& rhs) {
385  return value{std::forward<T>(lhs)} == rhs;
386 }
387 
388 template <typename T>
389 BSONCXX_INLINE not_value<T> operator!=(const value& lhs, T&& rhs) {
390  return lhs != value{std::forward<T>(rhs)};
391 }
392 
393 template <typename T>
394 BSONCXX_INLINE not_value<T> operator!=(T&& lhs, const value& rhs) {
395  return value{std::forward<T>(lhs)} != rhs;
396 }
397 
398 } // namespace types
399 
400 BSONCXX_INLINE_NAMESPACE_END
401 } // namespace bsoncxx
402 
403 #include <bsoncxx/config/postlude.hpp>
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
A BSON null value.
Definition: types.hpp:348
A BSON regex value.
Definition: types.hpp:364
A BSON binary data value.
Definition: types.hpp:204
A BSON DBPointer value.
Definition: types.hpp:400
A BSON max-key value.
Definition: types.hpp:620
A BSON UTF-8 encoded string value.
Definition: types.hpp:109
A BSON date value.
Definition: types.hpp:287
A BSON min-key value.
Definition: types.hpp:604
A BSON JavaScript code value.
Definition: types.hpp:419
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
A BSON document value.
Definition: types.hpp:147
A BSON boolean value.
Definition: types.hpp:262
A BSON ObjectId value.
Definition: types.hpp:244
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