MongoDB C++ Driver current
Loading...
Searching...
No Matches
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/stdx/type_traits.hpp>
22#include <bsoncxx/types.hpp>
23
24#include <bsoncxx/config/prelude.hpp>
25
26namespace bsoncxx {
27inline namespace v_noabi {
28namespace document {
29class element;
30} // namespace document
31
32namespace types {
33namespace bson_value {
34
35class value;
36
44class 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
111 BSONCXX_DEPRECATED const b_string& get_utf8() const;
112
119 const b_string& get_string() const;
120
127 const b_document& get_document() const;
128
135 const b_array& get_array() const;
136
143 const b_binary& get_binary() const;
144
151 const b_undefined& get_undefined() const;
152
159 const b_oid& get_oid() const;
160
167 const b_bool& get_bool() const;
168
175 const b_date& get_date() const;
176
183 const b_null& get_null() const;
184
191 const b_regex& get_regex() const;
192
199 const b_dbpointer& get_dbpointer() const;
200
207 const b_code& get_code() const;
208
215 const b_symbol& get_symbol() const;
216
223 const b_codewscope& get_codewscope() const;
224
231 const b_int32& get_int32() const;
232
239 const b_timestamp& get_timestamp() const;
240
247 const b_int64& get_int64() const;
248
255 const b_decimal128& get_decimal128() const;
256
263 const b_minkey& get_minkey() const;
264
271 const b_maxkey& get_maxkey() const;
272
273 private:
274 friend class document::element;
275 friend class bson_value::value;
276
277 view(const std::uint8_t* raw, std::uint32_t length, std::uint32_t offset, std::uint32_t keylen);
278 view(void* internal_value) noexcept;
279
280 void _init(void* internal_value) noexcept;
281
282 void BSONCXX_PRIVATE destroy() noexcept;
283
284 bsoncxx::type _type;
285 union {
286 struct b_double _b_double;
287 struct b_string _b_string;
288 struct b_document _b_document;
289 struct b_array _b_array;
290 struct b_binary _b_binary;
291 struct b_undefined _b_undefined;
292 struct b_oid _b_oid;
293 struct b_bool _b_bool;
294 struct b_date _b_date;
295 struct b_null _b_null;
296 struct b_regex _b_regex;
297 struct b_dbpointer _b_dbpointer;
298 struct b_code _b_code;
299 struct b_symbol _b_symbol;
300 struct b_codewscope _b_codewscope;
301 struct b_int32 _b_int32;
302 struct b_timestamp _b_timestamp;
303 struct b_int64 _b_int64;
304 struct b_decimal128 _b_decimal128;
305 struct b_minkey _b_minkey;
306 struct b_maxkey _b_maxkey;
307 };
308};
309
310template <typename T>
311using is_bson_view_compatible = detail::conjunction<
312 std::is_constructible<bson_value::view, T>,
313 detail::negation<detail::disjunction<detail::is_alike<T, bson_value::view>,
314 detail::is_alike<T, bson_value::value>>>>;
315
316template <typename T>
317using not_view = is_bson_view_compatible<T>;
318
319template <typename T>
320BSONCXX_INLINE detail::requires_t<bool, is_bson_view_compatible<T>> //
321operator==(const bson_value::view& lhs, T&& rhs) {
322 return lhs == bson_value::view{std::forward<T>(rhs)};
323}
324
325template <typename T>
326BSONCXX_INLINE detail::requires_t<bool, is_bson_view_compatible<T>> //
327operator==(T&& lhs, const bson_value::view& rhs) {
328 return bson_value::view{std::forward<T>(lhs)} == rhs;
329}
330
331template <typename T>
332BSONCXX_INLINE detail::requires_t<bool, is_bson_view_compatible<T>> //
333operator!=(const bson_value::view& lhs, T&& rhs) {
334 return lhs != bson_value::view{std::forward<T>(rhs)};
335}
336
337template <typename T>
338BSONCXX_INLINE detail::requires_t<bool, is_bson_view_compatible<T>> //
339operator!=(T&& lhs, const bson_value::view& rhs) {
340 return bson_value::view{std::forward<T>(lhs)} != rhs;
341}
342
343} // namespace bson_value
344} // namespace types
345
346} // namespace v_noabi
347} // namespace bsoncxx
348
349#include <bsoncxx/config/postlude.hpp>
A variant owning type that represents any BSON type.
Definition value.hpp:43
A view-only variant that can contain any BSON type.
Definition view.hpp:44
view() noexcept
Default constructs a bson_value::view.
type
An enumeration of each BSON type.
Definition types.hpp:50
The top-level namespace for bsoncxx library entities.
Definition element.hpp:24
A BSON array value.
Definition types.hpp:199
A BSON binary data value.
Definition types.hpp:224
A BSON boolean value.
Definition types.hpp:282
A BSON JavaScript code value.
Definition types.hpp:438
A BSON JavaScript code with scope value.
Definition types.hpp:509
A BSON date value.
Definition types.hpp:307
A BSON DBPointer value.
Definition types.hpp:419
A BSON Decimal128 value.
Definition types.hpp:612
A BSON document value.
Definition types.hpp:167
A BSON double value.
Definition types.hpp:101
A BSON signed 32-bit integer value.
Definition types.hpp:543
A BSON 64-bit signed integer value.
Definition types.hpp:587
A BSON max-key value.
Definition types.hpp:655
A BSON min-key value.
Definition types.hpp:639
A BSON null value.
Definition types.hpp:366
A BSON ObjectId value.
Definition types.hpp:264
A BSON regex value.
Definition types.hpp:382
A BSON UTF-8 encoded string value.
Definition types.hpp:126
A BSON Symbol value.
Definition types.hpp:475
A BSON replication timestamp value.
Definition types.hpp:568
A BSON undefined value.
Definition types.hpp:248