MongoDB C++ Driver  mongocxx-3.6.2
All Classes Namespaces Functions Typedefs Enumerations Enumerator Friends Pages
view.hpp
1 // Copyright 2020 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 document {
29 class element;
30 } // namespace document
31 
32 namespace types {
33 namespace bson_value {
34 
35 class value;
36 
44 class BSONCXX_API view {
45  public:
60 #define BSONCXX_ENUM(type, val) explicit view(b_##type) noexcept;
61 #include <bsoncxx/enums/type.hpp>
62 #undef BSONCXX_ENUM
63 
68  view() noexcept;
69 
70  view(const view&) noexcept;
71  view& operator=(const view&) noexcept;
72 
73  ~view();
74 
82  friend BSONCXX_API bool BSONCXX_CALL operator==(const bson_value::view&,
83  const bson_value::view&);
84  friend BSONCXX_API bool BSONCXX_CALL operator!=(const bson_value::view&,
85  const bson_value::view&);
89 
93  bsoncxx::type type() const;
94 
101  const b_double& get_double() const;
102 
109  const b_utf8& get_utf8() const;
110 
117  const b_document& get_document() const;
118 
125  const b_array& get_array() const;
126 
133  const b_binary& get_binary() const;
134 
141  const b_undefined& get_undefined() const;
142 
149  const b_oid& get_oid() const;
150 
157  const b_bool& get_bool() const;
158 
165  const b_date& get_date() const;
166 
173  const b_null& get_null() const;
174 
181  const b_regex& get_regex() const;
182 
189  const b_dbpointer& get_dbpointer() const;
190 
197  const b_code& get_code() const;
198 
205  const b_symbol& get_symbol() const;
206 
213  const b_codewscope& get_codewscope() const;
214 
221  const b_int32& get_int32() const;
222 
229  const b_timestamp& get_timestamp() const;
230 
237  const b_int64& get_int64() const;
238 
245  const b_decimal128& get_decimal128() const;
246 
253  const b_minkey& get_minkey() const;
254 
261  const b_maxkey& get_maxkey() const;
262 
263  private:
264  friend class document::element;
265  friend class bson_value::value;
266 
267  view(const std::uint8_t* raw, std::uint32_t length, std::uint32_t offset, std::uint32_t keylen);
268  view(void* internal_value) noexcept;
269 
270  void _init(void* internal_value) noexcept;
271 
272  void BSONCXX_PRIVATE destroy() noexcept;
273 
274  bsoncxx::type _type;
275  union {
276  struct b_double _b_double;
277  struct b_utf8 _b_utf8;
278  struct b_document _b_document;
279  struct b_array _b_array;
280  struct b_binary _b_binary;
281  struct b_undefined _b_undefined;
282  struct b_oid _b_oid;
283  struct b_bool _b_bool;
284  struct b_date _b_date;
285  struct b_null _b_null;
286  struct b_regex _b_regex;
287  struct b_dbpointer _b_dbpointer;
288  struct b_code _b_code;
289  struct b_symbol _b_symbol;
290  struct b_codewscope _b_codewscope;
291  struct b_int32 _b_int32;
292  struct b_timestamp _b_timestamp;
293  struct b_int64 _b_int64;
294  struct b_decimal128 _b_decimal128;
295  struct b_minkey _b_minkey;
296  struct b_maxkey _b_maxkey;
297  };
298 };
299 
300 // sfinae in the bool return to avoid competing with the value == value
301 // operators.
302 template <typename T>
303 using not_view = typename std::enable_if<
304  std::is_constructible<bson_value::view, T>::value &&
305  !std::is_same<typename std::decay<T>::type, bson_value::view>::value &&
306  !std::is_same<typename std::decay<T>::type, bson_value::value>::value,
307  bool>::type;
308 
309 template <typename T>
310 BSONCXX_INLINE not_view<T> operator==(const bson_value::view& lhs, T&& rhs) {
311  return lhs == bson_value::view{std::forward<T>(rhs)};
312 }
313 
314 template <typename T>
315 BSONCXX_INLINE not_view<T> operator==(T&& lhs, const bson_value::view& rhs) {
316  return bson_value::view{std::forward<T>(lhs)} == rhs;
317 }
318 
319 template <typename T>
320 BSONCXX_INLINE not_view<T> operator!=(const bson_value::view& lhs, T&& rhs) {
321  return lhs != bson_value::view{std::forward<T>(rhs)};
322 }
323 
324 template <typename T>
325 BSONCXX_INLINE not_view<T> operator!=(T&& lhs, const bson_value::view& rhs) {
326  return bson_value::view{std::forward<T>(lhs)} != rhs;
327 }
328 
329 } // namespace bson_value
330 } // namespace types
331 
332 BSONCXX_INLINE_NAMESPACE_END
333 } // namespace bsoncxx
334 
335 #include <bsoncxx/config/postlude.hpp>
bsoncxx::types::b_array
A BSON array value.
Definition: types.hpp:181
bsoncxx::types::b_oid
A BSON ObjectId value.
Definition: types.hpp:246
bsoncxx::types::b_utf8
A BSON UTF-8 encoded string value.
Definition: types.hpp:113
bsoncxx
Top level namespace for MongoDB C++ BSON functionality.
Definition: element.hpp:24
bsoncxx::types::b_document
A BSON document value.
Definition: types.hpp:149
bsoncxx::types::b_null
A BSON null value.
Definition: types.hpp:348
bsoncxx::types::b_codewscope
A BSON JavaScript code with scope value.
Definition: types.hpp:496
bsoncxx::types::b_int64
A BSON 64-bit signed integer value.
Definition: types.hpp:580
bsoncxx::types::b_binary
A BSON binary data value.
Definition: types.hpp:206
bsoncxx::types::bson_value::view::view
view() noexcept
Default constructs a bson_value::view.
bsoncxx::types::b_symbol
A BSON Symbol value.
Definition: types.hpp:460
bsoncxx::types::b_dbpointer
A BSON DBPointer value.
Definition: types.hpp:402
bsoncxx::types::b_regex
A BSON regex value.
Definition: types.hpp:364
bsoncxx::types::b_maxkey
A BSON max-key value.
Definition: types.hpp:651
bsoncxx::types::b_timestamp
A BSON replication timestamp value.
Definition: types.hpp:561
bsoncxx::types::b_date
A BSON date value.
Definition: types.hpp:289
bsoncxx::types::b_int32
A BSON signed 32-bit integer value.
Definition: types.hpp:532
bsoncxx::types::b_minkey
A BSON min-key value.
Definition: types.hpp:635
bsoncxx::types::b_code
A BSON JavaScript code value.
Definition: types.hpp:421
bsoncxx::types::b_decimal128
A BSON Decimal128 value.
Definition: types.hpp:605
bsoncxx::type
type
An enumeration of each BSON type.
Definition: types.hpp:39
bsoncxx::types::b_undefined
A BSON undefined value.
Definition: types.hpp:230
bsoncxx::types::bson_value::value
A variant owning type that represents any BSON type.
Definition: value.hpp:39
bsoncxx::types::b_bool
A BSON boolean value.
Definition: types.hpp:264
bsoncxx::types::b_double
A BSON double value.
Definition: types.hpp:88
bsoncxx::types::bson_value::view
A view-only variant that can contain any BSON type.
Definition: view.hpp:44