9#include <boost/optional/optional_fwd.hpp>
11#include <userver/utils/assert.hpp>
13USERVER_NAMESPACE_BEGIN
29 static_assert(!std::is_reference<T>::value,
"Do not use a reference for T");
31 constexpr OptionalRef()
noexcept =
default;
32 constexpr OptionalRef(std::nullopt_t)
noexcept {}
33 constexpr OptionalRef(
const OptionalRef&)
noexcept =
default;
36 constexpr OptionalRef(T& other)
noexcept : data_(&other) {}
39 explicit constexpr OptionalRef(
const T&&) =
delete;
42 explicit constexpr OptionalRef(
const std::optional<U>& other)
noexcept : data_(GetPointer(other)) {}
45 explicit constexpr OptionalRef(std::optional<U>& other)
noexcept : data_(GetPointer(other)) {}
48 explicit constexpr OptionalRef(
const std::optional<U>&&)
noexcept {
49 static_assert(!
sizeof(U),
"Forming a reference to a temporary");
53 explicit constexpr OptionalRef(
const boost::optional<U>& other)
noexcept : data_(GetPointer(other)) {}
56 explicit constexpr OptionalRef(boost::optional<U>& other)
noexcept : data_(GetPointer(other)) {}
59 explicit constexpr OptionalRef(
const boost::optional<U>&&)
noexcept {
60 static_assert(!
sizeof(U),
"Forming a reference to a temporary");
63 constexpr bool has_value()
const noexcept {
return !!data_; }
64 constexpr explicit operator
bool()
const noexcept {
return has_value(); }
66 constexpr T* operator->()
const {
71 constexpr T& operator*()
const {
76 constexpr T& value()
const {
78 throw std::bad_optional_access();
85 template <
class Optional>
86 static T* GetPointer(Optional& other)
noexcept {
87 using ValueType =
decltype(*other);
89 std::is_const<T>::value || !std::is_const<ValueType>::value,
90 "Attempt to initialize non-const T from a const optional value"
101 T*
const data_ =
nullptr;
104template <
class T,
class U>
106 if (!lhs || !rhs)
return !lhs && !rhs;
110template <
class T,
class U>
112 return !(lhs == rhs);