MongoDB C++ Driver current
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
25namespace bsoncxx {
26BSONCXX_INLINE_NAMESPACE_BEGIN
27
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
310// sfinae in the bool return to avoid competing with the value == value
311// operators.
312template <typename T>
313using not_view = typename std::enable_if<
314 std::is_constructible<bson_value::view, T>::value &&
315 !std::is_same<typename std::decay<T>::type, bson_value::view>::value &&
316 !std::is_same<typename std::decay<T>::type, bson_value::value>::value,
317 bool>::type;
318
319template <typename T>
320BSONCXX_INLINE not_view<T> operator==(const bson_value::view& lhs, T&& rhs) {
321 return lhs == bson_value::view{std::forward<T>(rhs)};
322}
323
324template <typename T>
325BSONCXX_INLINE not_view<T> operator==(T&& lhs, const bson_value::view& rhs) {
326 return bson_value::view{std::forward<T>(lhs)} == rhs;
327}
328
329template <typename T>
330BSONCXX_INLINE not_view<T> operator!=(const bson_value::view& lhs, T&& rhs) {
331 return lhs != bson_value::view{std::forward<T>(rhs)};
332}
333
334template <typename T>
335BSONCXX_INLINE not_view<T> operator!=(T&& lhs, const bson_value::view& rhs) {
336 return bson_value::view{std::forward<T>(lhs)} != rhs;
337}
338
339} // namespace bson_value
340} // namespace types
341
342BSONCXX_INLINE_NAMESPACE_END
343} // namespace bsoncxx
344
345#include <bsoncxx/config/postlude.hpp>
A variant owning type that represents any BSON type.
Definition: value.hpp:44
A view-only variant that can contain any BSON type.
Definition: view.hpp:44
view() noexcept
Default constructs a bson_value::view.
Top level namespace for MongoDB C++ BSON functionality.
Definition: element.hpp:24
type
An enumeration of each BSON type.
Definition: types.hpp:46
A BSON array value.
Definition: types.hpp:197
A BSON binary data value.
Definition: types.hpp:222
A BSON boolean value.
Definition: types.hpp:280
A BSON JavaScript code value.
Definition: types.hpp:437
A BSON JavaScript code with scope value.
Definition: types.hpp:512
A BSON date value.
Definition: types.hpp:305
A BSON DBPointer value.
Definition: types.hpp:418
A BSON Decimal128 value.
Definition: types.hpp:621
A BSON document value.
Definition: types.hpp:165
A BSON double value.
Definition: types.hpp:97
A BSON signed 32-bit integer value.
Definition: types.hpp:548
A BSON 64-bit signed integer value.
Definition: types.hpp:596
A BSON max-key value.
Definition: types.hpp:667
A BSON min-key value.
Definition: types.hpp:651
A BSON null value.
Definition: types.hpp:364
A BSON ObjectId value.
Definition: types.hpp:262
A BSON regex value.
Definition: types.hpp:380
A BSON UTF-8 encoded string value.
Definition: types.hpp:122
A BSON Symbol value.
Definition: types.hpp:476
A BSON replication timestamp value.
Definition: types.hpp:577
A BSON undefined value.
Definition: types.hpp:246