17 #include <type_traits>
19 #include <bsoncxx/stdx/optional.hpp>
21 #include <bsoncxx/config/prelude.hpp>
24 BSONCXX_INLINE_NAMESPACE_BEGIN
29 template <
typename View,
typename Value>
32 using view_type = View;
33 using value_type = Value;
38 static_assert(std::is_constructible<View, Value>::value,
39 "View type must be constructible from a Value");
44 static_assert(std::is_default_constructible<View>::value,
45 "View type must be default constructible");
69 BSONCXX_INLINE
view_or_value(Value&& value) : _value(
std::move(value)), _view(*_value) {
76 : _value(other._value), _view(_value ? *_value : other._view) {
83 _value = other._value;
84 _view = _value ? *_value : other._view;
94 : _value{std::move(other._value)},
95 _view(_value ? *_value : std::move(other._view)) {
97 other._value = stdx::nullopt;
105 _value = std::move(other._value);
106 _view = _value ? *_value : std::move(other._view);
107 other._view = View();
108 other._value = stdx::nullopt;
118 return static_cast<bool>(_value);
126 BSONCXX_INLINE
operator View()
const {
135 BSONCXX_INLINE
const View&
view()
const {
140 stdx::optional<Value> _value;
151 template <
typename View,
typename Value>
157 template <
typename View,
typename Value>
160 return !(lhs == rhs);
173 template <
typename View,
typename Value>
175 return lhs.
view() == rhs;
178 template <
typename View,
typename Value>
183 template <
typename View,
typename Value>
184 BSONCXX_INLINE
bool operator!=(
const view_or_value<View, Value>& lhs, View rhs) {
185 return !(lhs == rhs);
188 template <
typename View,
typename Value>
189 BSONCXX_INLINE
bool operator!=(View lhs,
const view_or_value<View, Value>& rhs) {
190 return !(rhs == lhs);
193 template <
typename View,
typename Value>
194 BSONCXX_INLINE
bool operator==(
const view_or_value<View, Value>& lhs,
const Value& rhs) {
195 return lhs.view() == View(rhs);
198 template <
typename View,
typename Value>
199 BSONCXX_INLINE
bool operator==(
const Value& lhs,
const view_or_value<View, Value>& rhs) {
203 template <
typename View,
typename Value>
204 BSONCXX_INLINE
bool operator!=(
const view_or_value<View, Value>& lhs,
const Value& rhs) {
205 return !(lhs == rhs);
208 template <
typename View,
typename Value>
209 BSONCXX_INLINE
bool operator!=(
const Value& lhs,
const view_or_value<View, Value>& rhs) {
210 return !(rhs == lhs);
216 BSONCXX_INLINE_NAMESPACE_END
219 #include <bsoncxx/config/postlude.hpp>
bool is_owning() const noexcept
Return whether or not this view_or_value owns an underlying Value.
Definition: view_or_value.hpp:117
view_or_value(Value &&value)
Constructs a view_or_value from a Value type.
Definition: view_or_value.hpp:69
Definition: error_code.hpp:69
view_or_value()=default
Class View must be constructible from an instance of class Value.
view_or_value(View view)
Construct a view_or_value from a View.
Definition: view_or_value.hpp:60
Class representing a view-or-value variant type.
Definition: view_or_value.hpp:30
const View & view() const
Get a View for the type.
Definition: view_or_value.hpp:135
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:152
bool operator==(const view_or_value< View, Value > &lhs, View rhs)
Mixed (in)-equality operators for view_or_value against View or Value types.
Definition: view_or_value.hpp:174
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:93
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:104
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:82
view_or_value(const view_or_value &other)
Construct a view_or_value from a copied view_or_value.
Definition: view_or_value.hpp:75
Definition: element.hpp:24