MongoDB C++ Driver mongocxx-4.0.0
Loading...
Searching...
No Matches
view_or_value.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 <type_traits>
18
20
22
24
25namespace bsoncxx {
26namespace v_noabi {
27
31template <typename View, typename Value>
33 public:
34 using view_type = View;
35 using value_type = Value;
36
40 static_assert(std::is_constructible<View, Value>::value,
41 "View type must be constructible from a Value");
42
46 static_assert(std::is_default_constructible<View>::value,
47 "View type must be default constructible");
48
52 ~view_or_value() = default;
53
58 view_or_value() = default;
59
67 view_or_value(View view) : _view{view} {}
68
75 view_or_value(Value&& value) : _value(std::move(value)), _view(*_value) {}
76
81 : _value(other._value), _view(_value ? *_value : other._view) {}
82
87 _value = other._value;
88 _view = _value ? *_value : other._view;
89 return *this;
90 }
91
95
97 view_or_value(view_or_value&& other) noexcept
98 : _value{std::move(other._value)}, _view(_value ? *_value : std::move(other._view)) {
99 other._view = View();
100 other._value = stdx::nullopt;
101 }
102
108 _value = std::move(other._value);
109 _view = _value ? *_value : std::move(other._view);
110 other._view = View();
111 other._value = stdx::nullopt;
112 return *this;
113 }
114
120 bool is_owning() const noexcept {
121 return static_cast<bool>(_value);
122 }
123
129 operator View() const {
130 return _view;
131 }
132
138 const View& view() const {
139 return _view;
140 }
141
142 private:
144 View _view;
145};
146
151
153template <typename View, typename Value>
155 return lhs.view() == rhs.view();
156}
157
159template <typename View, typename Value>
161 return !(lhs == rhs);
162}
163
166
171
173template <typename View, typename Value>
174bool operator==(const view_or_value<View, Value>& lhs, View rhs) {
175 return lhs.view() == rhs;
176}
177
179template <typename View, typename Value>
180bool operator==(View lhs, const view_or_value<View, Value>& rhs) {
181 return rhs == lhs;
182}
183
185template <typename View, typename Value>
186bool operator!=(const view_or_value<View, Value>& lhs, View rhs) {
187 return !(lhs == rhs);
188}
189
191template <typename View, typename Value>
192bool operator!=(View lhs, const view_or_value<View, Value>& rhs) {
193 return !(rhs == lhs);
194}
195
197template <typename View, typename Value>
198bool operator==(const view_or_value<View, Value>& lhs, const Value& rhs) {
199 return lhs.view() == View(rhs);
200}
201
203template <typename View, typename Value>
204bool operator==(const Value& lhs, const view_or_value<View, Value>& rhs) {
205 return rhs == lhs;
206}
207
209template <typename View, typename Value>
210bool operator!=(const view_or_value<View, Value>& lhs, const Value& rhs) {
211 return !(lhs == rhs);
212}
213
215template <typename View, typename Value>
216bool operator!=(const Value& lhs, const view_or_value<View, Value>& rhs) {
217 return !(rhs == lhs);
218}
219
222
223} // namespace v_noabi
224} // namespace bsoncxx
225
226namespace bsoncxx {
227
228using ::bsoncxx::v_noabi::operator==;
229using ::bsoncxx::v_noabi::operator!=;
230
231} // namespace bsoncxx
232
234
239
240#if defined(BSONCXX_PRIVATE_DOXYGEN_PREPROCESSOR)
241
242namespace bsoncxx {
243
245template <typename View, typename Value>
248
250template <typename View, typename Value>
253
255template <typename View, typename Value>
257
259template <typename View, typename Value>
261
263template <typename View, typename Value>
265
267template <typename View, typename Value>
269
271template <typename View, typename Value>
272bool operator==(const v_noabi::view_or_value<View, Value>& lhs, const Value& rhs);
273
275template <typename View, typename Value>
276bool operator==(const Value& lhs, const v_noabi::view_or_value<View, Value>& rhs);
277
279template <typename View, typename Value>
280bool operator!=(const v_noabi::view_or_value<View, Value>& lhs, const Value& rhs);
281
282} // namespace bsoncxx
283
284#endif // defined(BSONCXX_PRIVATE_DOXYGEN_PREPROCESSOR)
The bsoncxx macro guard postlude header.
The bsoncxx macro guard prelude header.
A polyfill for std::optional<T>.
Definition optional.hpp:826
Class representing a view-or-value variant type.
Definition view_or_value.hpp:32
bool operator!=(const Value &lhs, const view_or_value< View, Value > &rhs)
Mixed (in)equality operators for view_or_value against View and Value types.
Definition view_or_value.hpp:216
bool operator!=(View lhs, const view_or_value< View, Value > &rhs)
Mixed (in)equality operators for view_or_value against View and Value types.
Definition view_or_value.hpp:192
view_or_value(View view)
Construct a view_or_value from a View. When constructed with a View, this object is non-owning....
Definition view_or_value.hpp:67
view_or_value()=default
Default-constructs a view_or_value. This is equivalent to constructing a view_or_value with a default...
view_or_value(const view_or_value &other)
Construct a view_or_value from a copied view_or_value.
Definition view_or_value.hpp:80
bool operator!=(const view_or_value< View, Value > &lhs, const view_or_value< View, Value > &rhs)
Compare view_or_value objects for (in)equality.
Definition view_or_value.hpp:160
bool is_owning() const noexcept
Return whether or not this view_or_value owns an underlying Value.
Definition view_or_value.hpp:120
view_or_value(Value &&value)
Constructs a view_or_value from a Value type. This object owns the passed-in Value.
Definition view_or_value.hpp:75
view_or_value & operator=(view_or_value &&other) noexcept
Assign to this view_or_value from a moved-in view_or_value.
Definition view_or_value.hpp:107
bool operator!=(const view_or_value< View, Value > &lhs, const Value &rhs)
Mixed (in)equality operators for view_or_value against View and Value types.
Definition view_or_value.hpp:210
bool operator==(const view_or_value< View, Value > &lhs, View rhs)
Mixed (in)equality operators for view_or_value against View and Value types.
Definition view_or_value.hpp:174
bool operator!=(const view_or_value< View, Value > &lhs, View rhs)
Mixed (in)equality operators for view_or_value against View and Value types.
Definition view_or_value.hpp:186
bool operator==(const view_or_value< View, Value > &lhs, const Value &rhs)
Mixed (in)equality operators for view_or_value against View and Value types.
Definition view_or_value.hpp:198
bool operator==(View lhs, const view_or_value< View, Value > &rhs)
Mixed (in)equality operators for view_or_value against View and Value types.
Definition view_or_value.hpp:180
view_or_value(view_or_value &&other) noexcept
Construct a view_or_value from a moved-in view_or_value.
Definition view_or_value.hpp:97
const View & view() const
Get a View for the type.
Definition view_or_value.hpp:138
~view_or_value()=default
Class View must be constructible from an instance of class Value.
bool operator==(const Value &lhs, const view_or_value< View, Value > &rhs)
Mixed (in)equality operators for view_or_value against View and Value types.
Definition view_or_value.hpp:204
view_or_value & operator=(const view_or_value &other)
Assign to this view_or_value from a copied view_or_value.
Definition view_or_value.hpp:86
bool operator==(const view_or_value< View, Value > &lhs, const view_or_value< View, Value > &rhs)
Compare view_or_value objects for (in)equality.
Definition view_or_value.hpp:154
The top-level namespace within which all bsoncxx library entities are declared.
bool operator==(const v_noabi::view_or_value< View, Value > &lhs, const v_noabi::view_or_value< View, Value > &rhs)
bsoncxx::v_noabi::operator==(const v_noabi::view_or_value<View, Value>& lhs, const v_noabi::view_or_v...
bool operator!=(const v_noabi::view_or_value< View, Value > &lhs, const v_noabi::view_or_value< View, Value > &rhs)
bsoncxx::v_noabi::operator!=(const v_noabi::view_or_value<View, Value>& lhs, const v_noabi::view_or_v...
The top-level namespace reserved for the C++ standard library.
Provides std::optional-related polyfills for library API usage.
Declares bsoncxx::v_noabi::view_or_value.