MongoDB C++ Driver 4.3.0
Loading...
Searching...
No Matches
view.hpp
Go to the documentation of this file.
1// Copyright 2009-present 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 <bsoncxx/types/bson_value/view-fwd.hpp> // IWYU pragma: export
18
19//
20
21#include <bsoncxx/v1/types/view.hpp> // IWYU pragma: export
22
23#include <cstddef>
24#include <cstdint> // IWYU pragma: keep: backward compatibility, to be removed.
25#include <type_traits>
26
27#include <bsoncxx/document/element-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
29
31#include <bsoncxx/types.hpp>
32
34
35namespace bsoncxx {
36namespace detail {
37
38template <typename T>
39using is_bson_view_compatible = detail::conjunction<
40 std::is_constructible<v_noabi::types::bson_value::view, T>,
41 detail::negation<detail::disjunction<
42 detail::is_alike<T, v_noabi::types::bson_value::view>,
43 detail::is_alike<T, v_noabi::types::bson_value::value>>>>;
44
45} // namespace detail
46} // namespace bsoncxx
47
48namespace bsoncxx {
49namespace v_noabi {
50namespace types {
51namespace bson_value {
52
62class view {
63 private:
64 v_noabi::type _id;
65
66#pragma push_macro("X")
67#undef X
68#define X(_name, _value) b_##_name _b_##_name;
69
70 union {
72 };
73#pragma pop_macro("X")
74
75 public:
80 view() noexcept : _id{v_noabi::type::k_null}, _b_null{} {}
81
85 /* explicit(false) */ view(v1::types::view const& v) : _id{from_v1(v.type_id())} {
86#pragma push_macro("X")
87#undef X
88#define X(_name, _value) \
89 case v_noabi::type::k_##_name: \
90 _b_##_name = from_v1(v.get_##_name()); \
91 break;
92
93 switch (_id) { BSONCXX_V1_TYPES_XMACRO(X) }
94#pragma pop_macro("X")
95 }
96
97#pragma push_macro("X")
98#undef X
99#define X(_name, _value) \
100 explicit view(v_noabi::types::b_##_name v) noexcept : _id{v.type_id}, _b_##_name{v} {}
101
109#pragma pop_macro("X")
110
114 explicit operator v1::types::view() const {
115#pragma push_macro("X")
116#undef X
117#define X(_name, _value) \
118 case v_noabi::type::k_##_name: \
119 return {to_v1(_b_##_name)};
120
121 switch (_id) {
123
124 default:
125 return {}; // Unreachable.
126 }
127#pragma pop_macro("X")
128 }
129
136 return _id;
137 }
138
145 return _id;
146 }
147
148#pragma push_macro("X")
149#undef X
150#define X(_name, _value) BSONCXX_ABI_EXPORT_CDECL(v_noabi::types::b_##_name const&) get_##_name() const;
151
162#pragma pop_macro("X")
163
170 friend BSONCXX_ABI_EXPORT_CDECL(bool) operator==(view const& lhs, view const& rhs);
171
172 friend bool operator!=(view const& lhs, view const& rhs) {
173 return !(lhs == rhs);
174 }
175
177};
178
186
188template <typename T>
189detail::requires_t<bool, detail::is_bson_view_compatible<T>> operator==(view const& lhs, T&& rhs) {
190 return lhs == view{std::forward<T>(rhs)};
191}
192
194template <typename T>
195detail::requires_t<bool, detail::is_bson_view_compatible<T>> operator==(T&& lhs, view const& rhs) {
196 return view{std::forward<T>(lhs)} == rhs;
197}
198
200template <typename T>
201detail::requires_t<bool, detail::is_bson_view_compatible<T>> operator!=(view const& lhs, T&& rhs) {
202 return lhs != view{std::forward<T>(rhs)};
203}
204
206template <typename T>
207detail::requires_t<bool, detail::is_bson_view_compatible<T>> operator!=(T&& lhs, view const& rhs) {
208 return view{std::forward<T>(lhs)} != rhs;
209}
210
213
214} // namespace bson_value
215} // namespace types
216} // namespace v_noabi
217} // namespace bsoncxx
218
219namespace bsoncxx {
220namespace v_noabi {
221
226 return {v};
227}
228
235
236} // namespace v_noabi
237} // namespace bsoncxx
238
239namespace bsoncxx {
240namespace types {
241namespace bson_value {
242
243using v_noabi::types::bson_value::operator==;
244using v_noabi::types::bson_value::operator!=;
245
246} // namespace bson_value
247} // namespace types
248} // namespace bsoncxx
249
251
#define BSONCXX_ABI_EXPORT_CDECL(...)
Equivalent to BSONCXX_ABI_EXPORT with BSONCXX_ABI_CDECL.
Definition export.hpp:52
The bsoncxx v_noabi macro guard postlude header.
The bsoncxx v_noabi macro guard prelude header.
view() noexcept
Default constructs a bson_value::view. The resulting view will be initialized to point at a bson_valu...
Definition view.hpp:80
A non-owning, read-only union of BSON type values.
Definition view.hpp:1023
A non-owning variant that can contain any BSON type.
Definition view.hpp:62
detail::requires_t< bool, detail::is_bson_view_compatible< T > > operator!=(T &&lhs, view const &rhs)
Compares a view with a type representable as a view.
Definition view.hpp:207
v_noabi::type type_id() const
Equivalent to type() const.
Definition view.hpp:144
detail::requires_t< bool, detail::is_bson_view_compatible< T > > operator!=(view const &lhs, T &&rhs)
Compares a view with a type representable as a view.
Definition view.hpp:201
friend bool operator==(view const &lhs, view const &rhs)
Compare two bson_value::views for equality.
v_noabi::type type() const
Returns the type of the underlying BSON value stored in this object.
Definition view.hpp:135
friend bool operator!=(view const &lhs, view const &rhs)
Compare two bson_value::views for equality.
Definition view.hpp:172
detail::requires_t< bool, detail::is_bson_view_compatible< T > > operator==(view const &lhs, T &&rhs)
Compares a view with a type representable as a view.
Definition view.hpp:189
view() noexcept
Default constructs a bson_value::view. The resulting view will be initialized to point at a bson_valu...
Definition view.hpp:80
view(v1::types::view const &v)
Construct with the bsoncxx::v1 equivalent.
Definition view.hpp:85
detail::requires_t< bool, detail::is_bson_view_compatible< T > > operator==(T &&lhs, view const &rhs)
Compares a view with a type representable as a view.
Definition view.hpp:195
Declares bsoncxx::v_noabi::document::element.
Declares entities representing any BSON value type.
Declares entities representing BSON value types.
Declares entities representing any BSON value type.
Declares entities representing BSON value types.
Declares entities whose ABI stability is NOT guaranteed.
type
An enumeration of each BSON type.
Definition types.hpp:47
@ k_null
Null value.
Definition types.hpp:57
v1::element::view to_v1(v_noabi::array::element const &v)
Convert to the bsoncxx::v1 equivalent of v.
Definition element.hpp:132
v_noabi::array::value from_v1(v1::array::value const &v)
Convert from the bsoncxx::v1 equivalent of v.
Definition value.hpp:267
The top-level namespace within which all bsoncxx library entities are declared.
Provides entities used to represent BSON types.
#define BSONCXX_V1_TYPES_XMACRO(X)
X-macro over the name and value of BSON types.
Definition id-fwd.hpp:44
Provides non-owning, read-only entities representing a BSON type value.
For internal use only!
Declares bsoncxx::v_noabi::types::bson_value::value.
Declares bsoncxx::v_noabi::types::bson_value::view.