MongoDB C++ Driver current
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
100// For backward compatibility, the `from_v1(to_v1(v))` round-trip ensures empty string-like BSON type values are always
101// represented as not-null empty strings.
102#define X(_name, _value) \
103 explicit view(v_noabi::types::b_##_name v) noexcept : _id{v.type_id}, _b_##_name{from_v1(to_v1(v))} {}
104
112#pragma pop_macro("X")
113
117 explicit operator v1::types::view() const {
118#pragma push_macro("X")
119#undef X
120#define X(_name, _value) \
121 case v_noabi::type::k_##_name: \
122 return {to_v1(_b_##_name)};
123
124 switch (_id) {
126
127 default:
128 return {}; // Unreachable.
129 }
130#pragma pop_macro("X")
131 }
132
139 return _id;
140 }
141
148 return _id;
149 }
150
151#pragma push_macro("X")
152#undef X
153#define X(_name, _value) BSONCXX_ABI_EXPORT_CDECL_UNSTABLE(v_noabi::types::b_##_name const&) get_##_name() const;
154
165#pragma pop_macro("X")
166
173 friend BSONCXX_ABI_EXPORT_CDECL_UNSTABLE(bool) operator==(view const& lhs, view const& rhs);
174
175 friend bool operator!=(view const& lhs, view const& rhs) {
176 return !(lhs == rhs);
177 }
178
180};
181
189
191template <typename T>
192detail::requires_t<bool, detail::is_bson_view_compatible<T>> operator==(view const& lhs, T&& rhs) {
193 return lhs == view{std::forward<T>(rhs)};
194}
195
197template <typename T>
198detail::requires_t<bool, detail::is_bson_view_compatible<T>> operator==(T&& lhs, view const& rhs) {
199 return view{std::forward<T>(lhs)} == rhs;
200}
201
203template <typename T>
204detail::requires_t<bool, detail::is_bson_view_compatible<T>> operator!=(view const& lhs, T&& rhs) {
205 return lhs != view{std::forward<T>(rhs)};
206}
207
209template <typename T>
210detail::requires_t<bool, detail::is_bson_view_compatible<T>> operator!=(T&& lhs, view const& rhs) {
211 return view{std::forward<T>(lhs)} != rhs;
212}
213
216
217} // namespace bson_value
218} // namespace types
219} // namespace v_noabi
220} // namespace bsoncxx
221
222namespace bsoncxx {
223namespace v_noabi {
224
229 return {v};
230}
231
238
239} // namespace v_noabi
240} // namespace bsoncxx
241
242namespace bsoncxx {
243namespace types {
244namespace bson_value {
245
246using v_noabi::types::bson_value::operator==;
247using v_noabi::types::bson_value::operator!=;
248
249} // namespace bson_value
250} // namespace types
251} // namespace bsoncxx
252
254
#define BSONCXX_ABI_EXPORT_CDECL_UNSTABLE(...)
Equivalent to BSONCXX_ABI_EXPORT_UNSTABLE with BSONCXX_ABI_CDECL.
Definition config.hpp:28
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:979
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:210
v_noabi::type type_id() const
Equivalent to type() const.
Definition view.hpp:147
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:204
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:138
friend bool operator!=(view const &lhs, view const &rhs)
Compare two bson_value::views for equality.
Definition view.hpp:175
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:192
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:198
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:263
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.