Loading...
Searching...
No Matches
view_or_value.hpp
Go to the documentation of this file.
// Copyright 2009-present MongoDB, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <type_traits>
#include <bsoncxx/view_or_value-fwd.hpp> // IWYU pragma: export
#include <bsoncxx/stdx/optional.hpp>
#include <bsoncxx/config/prelude.hpp>
namespace bsoncxx {
namespace v_noabi {
template <typename View, typename Value>
class view_or_value {
public:
using view_type = View;
using value_type = Value;
static_assert(std::is_constructible<View, Value>::value, "View type must be constructible from a Value");
static_assert(std::is_default_constructible<View>::value, "View type must be default constructible");
~view_or_value() = default;
view_or_value() = default;
view_or_value(View view) : _view{view} {}
view_or_value(Value&& value) : _value(std::move(value)), _view(*_value) {}
view_or_value(view_or_value const& other) : _value(other._value), _view(_value ? *_value : other._view) {}
view_or_value& operator=(view_or_value const& other) {
_value = other._value;
_view = _value ? *_value : other._view;
return *this;
}
view_or_value(view_or_value&& other) noexcept
: _value{std::move(other._value)}, _view(_value ? *_value : std::move(other._view)) {
other._view = View();
other._value = stdx::nullopt;
}
view_or_value& operator=(view_or_value&& other) noexcept {
_value = std::move(other._value);
_view = _value ? *_value : std::move(other._view);
other._view = View();
other._value = stdx::nullopt;
return *this;
}
bool is_owning() const noexcept {
return static_cast<bool>(_value);
}
operator View() const {
return _view;
}
View const& view() const {
return _view;
}
private:
stdx::optional<Value> _value;
View _view;
};
template <typename View, typename Value>
bool operator==(view_or_value<View, Value> const& lhs, view_or_value<View, Value> const& rhs) {
return lhs.view() == rhs.view();
}
template <typename View, typename Value>
bool operator!=(view_or_value<View, Value> const& lhs, view_or_value<View, Value> const& rhs) {
return !(lhs == rhs);
}
template <typename View, typename Value>
bool operator==(view_or_value<View, Value> const& lhs, View rhs) {
return lhs.view() == rhs;
}
template <typename View, typename Value>
bool operator==(View lhs, view_or_value<View, Value> const& rhs) {
return rhs == lhs;
}
template <typename View, typename Value>
bool operator!=(view_or_value<View, Value> const& lhs, View rhs) {
return !(lhs == rhs);
}
template <typename View, typename Value>
bool operator!=(View lhs, view_or_value<View, Value> const& rhs) {
return !(rhs == lhs);
}
template <typename View, typename Value>
bool operator==(view_or_value<View, Value> const& lhs, Value const& rhs) {
return lhs.view() == View(rhs);
}
template <typename View, typename Value>
bool operator==(Value const& lhs, view_or_value<View, Value> const& rhs) {
return rhs == lhs;
}
template <typename View, typename Value>
bool operator!=(view_or_value<View, Value> const& lhs, Value const& rhs) {
return !(lhs == rhs);
}
template <typename View, typename Value>
bool operator!=(Value const& lhs, view_or_value<View, Value> const& rhs) {
return !(rhs == lhs);
}
} // namespace v_noabi
} // namespace bsoncxx
namespace bsoncxx {
using v_noabi::operator==;
using v_noabi::operator!=;
} // namespace bsoncxx
#include <bsoncxx/config/postlude.hpp>
postlude.hpp
The bsoncxx v_noabi macro guard postlude header.
prelude.hpp
The bsoncxx v_noabi macro guard prelude header.
bsoncxx::v1::stdx::optional
A polyfill for std::optional<T>.
Definition optional.hpp:799
bsoncxx::v_noabi::view_or_value::view_or_value
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:65
bsoncxx::v_noabi::view_or_value::operator==
bool operator==(view_or_value< View, Value > const &lhs, view_or_value< View, Value > const &rhs)
Compare view_or_value objects for (in)equality.
Definition view_or_value.hpp:151
bsoncxx::v_noabi::view_or_value::view_or_value
view_or_value()=default
Default-constructs a view_or_value. This is equivalent to constructing a view_or_value with a default...
bsoncxx::v_noabi::view_or_value::operator!=
bool operator!=(Value const &lhs, view_or_value< View, Value > const &rhs)
Mixed (in)equality operators for view_or_value against View and Value types.
Definition view_or_value.hpp:213
bsoncxx::v_noabi::view_or_value::view_or_value
view_or_value(view_or_value const &other)
Construct a view_or_value from a copied view_or_value.
Definition view_or_value.hpp:78
bsoncxx::v_noabi::view_or_value::is_owning
bool is_owning() const noexcept
Return whether or not this view_or_value owns an underlying Value.
Definition view_or_value.hpp:117
bsoncxx::v_noabi::view_or_value::operator!=
bool operator!=(view_or_value< View, Value > const &lhs, View rhs)
Mixed (in)equality operators for view_or_value against View and Value types.
Definition view_or_value.hpp:183
bsoncxx::v_noabi::view_or_value< view, value >::view
view const & view() const
Definition view_or_value.hpp:135
bsoncxx::v_noabi::view_or_value::view_or_value
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:73
bsoncxx::v_noabi::view_or_value::operator==
bool operator==(View lhs, view_or_value< View, Value > const &rhs)
Mixed (in)equality operators for view_or_value against View and Value types.
Definition view_or_value.hpp:177
bsoncxx::v_noabi::view_or_value::operator=
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
bsoncxx::v_noabi::view_or_value::operator!=
bool operator!=(view_or_value< View, Value > const &lhs, view_or_value< View, Value > const &rhs)
Compare view_or_value objects for (in)equality.
Definition view_or_value.hpp:157
bsoncxx::v_noabi::view_or_value::operator=
view_or_value & operator=(view_or_value const &other)
Assign to this view_or_value from a copied view_or_value.
Definition view_or_value.hpp:83
bsoncxx::v_noabi::view_or_value::operator==
bool operator==(Value const &lhs, view_or_value< View, Value > const &rhs)
Mixed (in)equality operators for view_or_value against View and Value types.
Definition view_or_value.hpp:201
bsoncxx::v_noabi::view_or_value::operator==
bool operator==(view_or_value< View, Value > const &lhs, Value const &rhs)
Mixed (in)equality operators for view_or_value against View and Value types.
Definition view_or_value.hpp:195
bsoncxx::v_noabi::view_or_value::operator!=
bool operator!=(view_or_value< View, Value > const &lhs, Value const &rhs)
Mixed (in)equality operators for view_or_value against View and Value types.
Definition view_or_value.hpp:207
bsoncxx::v_noabi::view_or_value::view_or_value
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:94
bsoncxx::v_noabi::view_or_value::operator!=
bool operator!=(View lhs, view_or_value< View, Value > const &rhs)
Mixed (in)equality operators for view_or_value against View and Value types.
Definition view_or_value.hpp:189
bsoncxx::v_noabi::view_or_value::~view_or_value
~view_or_value()=default
View must be constructible from an instance of class Value.
bsoncxx::v_noabi::view_or_value::operator==
bool operator==(view_or_value< View, Value > const &lhs, View rhs)
Mixed (in)equality operators for view_or_value against View and Value types.
Definition view_or_value.hpp:171
bsoncxx::v_noabi
Declares entities whose ABI stability is NOT guaranteed.
bsoncxx
The top-level namespace within which all bsoncxx library entities are declared.
optional.hpp
Provides std::optional-related polyfills for library API usage.
view_or_value-fwd.hpp
Declares bsoncxx::v_noabi::view_or_value.