17 #include <type_traits>
19 #include <bsoncxx/view_or_value-fwd.hpp>
21 #include <bsoncxx/stdx/optional.hpp>
23 #include <bsoncxx/config/prelude.hpp>
31 template <
typename View,
typename Value>
34 using view_type = View;
35 using value_type = Value;
40 static_assert(std::is_constructible<View, Value>::value,
41 "View type must be constructible from a Value");
46 static_assert(std::is_default_constructible<View>::value,
47 "View type must be default constructible");
70 BSONCXX_INLINE
view_or_value(Value&& value) : _value(std::move(value)), _view(*_value) {}
76 : _value(other._value), _view(_value ? *_value : other._view) {}
82 _value = other._value;
83 _view = _value ? *_value : other._view;
93 : _value{std::move(other._value)}, _view(_value ? *_value : std::move(other._view)) {
95 other._value = stdx::nullopt;
103 _value = std::move(other._value);
104 _view = _value ? *_value : std::move(other._view);
105 other._view = View();
106 other._value = stdx::nullopt;
116 return static_cast<bool>(_value);
124 BSONCXX_INLINE
operator View()
const {
133 BSONCXX_INLINE
const View&
view()
const {
138 stdx::optional<Value> _value;
149 template <
typename View,
typename Value>
155 template <
typename View,
typename Value>
158 return !(lhs == rhs);
171 template <
typename View,
typename Value>
173 return lhs.
view() == rhs;
176 template <
typename View,
typename Value>
181 template <
typename View,
typename Value>
182 BSONCXX_INLINE
bool operator!=(
const view_or_value<View, Value>& lhs, View rhs) {
183 return !(lhs == rhs);
186 template <
typename View,
typename Value>
187 BSONCXX_INLINE
bool operator!=(View lhs,
const view_or_value<View, Value>& rhs) {
188 return !(rhs == lhs);
191 template <
typename View,
typename Value>
192 BSONCXX_INLINE
bool operator==(
const view_or_value<View, Value>& lhs,
const Value& rhs) {
193 return lhs.view() == View(rhs);
196 template <
typename View,
typename Value>
197 BSONCXX_INLINE
bool operator==(
const Value& lhs,
const view_or_value<View, Value>& rhs) {
201 template <
typename View,
typename Value>
202 BSONCXX_INLINE
bool operator!=(
const view_or_value<View, Value>& lhs,
const Value& rhs) {
203 return !(lhs == rhs);
206 template <
typename View,
typename Value>
207 BSONCXX_INLINE
bool operator!=(
const Value& lhs,
const view_or_value<View, Value>& rhs) {
208 return !(rhs == lhs);
219 using ::bsoncxx::v_noabi::operator==;
220 using ::bsoncxx::v_noabi::operator!=;
224 #include <bsoncxx/config/postlude.hpp>
Class representing a view-or-value variant type.
Definition: view_or_value.hpp:32
view_or_value(View view)
Construct a view_or_value from a View.
Definition: view_or_value.hpp:62
view_or_value()=default
Class View must be constructible from an instance of class Value.
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
const View & view() const
Get a View for the type.
Definition: view_or_value.hpp:133
bool is_owning() const noexcept
Return whether or not this view_or_value owns an underlying Value.
Definition: view_or_value.hpp:115
view_or_value(Value &&value)
Constructs a view_or_value from a Value type.
Definition: view_or_value.hpp:70
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:172
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:102
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:81
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:92
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:150
The top-level namespace for bsoncxx library entities.
Definition: element-fwd.hpp:19