libER 0.1.4.2
ELDEN RING API library
Loading...
Searching...
No Matches
optref.hpp
Go to the documentation of this file.
1
8#pragma once
9
10#include <concepts>
11#include <optional>
12#include <type_traits>
13#include <utility>
14
15namespace liber {
24template <typename T>
25 requires requires { !std::is_reference_v<T>; }
26struct optref {
31 using value_type = T;
32
37 using reference_type = std::add_lvalue_reference_t<T>;
38
43 using const_reference_type = std::add_const_t<reference_type>;
44
49 using reference_wrapper_type = std::reference_wrapper<value_type>;
50
55 constexpr optref() noexcept : opt(std::nullopt) {}
56
61 constexpr optref(std::nullopt_t) noexcept : opt(std::nullopt) {}
62
67 constexpr optref(const optref& other) noexcept : opt(other.opt) {}
68
75 template <typename U>
76 requires std::convertible_to<U&, T&>
77 constexpr optref(const optref<U>& other) noexcept
78 : optref([&]() -> optref {
79 if (!other.has_reference())
80 return std::nullopt;
81 return static_cast<reference_type>(other.reference());
82 }()) {}
83
88 constexpr optref(T& reference) noexcept
90
95 constexpr optref(std::reference_wrapper<T> wrapper) noexcept
96 : opt(wrapper) {}
97
102 constexpr optref(T* pointer) noexcept
103 : optref([&]() -> optref {
104 if (!pointer)
105 return std::nullopt;
106 return *pointer;
107 }()) {}
108
115 constexpr bool has_reference() const noexcept {
116 return this->opt.has_value();
117 }
118
125 explicit constexpr operator bool() const noexcept {
126 return static_cast<bool>(this->opt);
127 }
128
137 return this->opt.value();
138 }
139
147 constexpr const_reference_type reference() const {
148 return this->opt.value();
149 }
150
151 // Other std::optional functions
152 // std::optional::operator(s) *,-> and std::optional::value_or aren't
153 // applicable to references Would overload operator . if it was possible
154
160 void swap(optref& other) noexcept {
161 this->opt.swap(other.opt);
162 }
163
168 void reset() noexcept {
169 this->opt.reset();
170 }
171
172private:
173 std::optional<reference_wrapper_type> opt;
174};
175} // namespace liber
A std::optional<std::reference_wrapper<T>> wrapper for lvalue references.
Definition optref.hpp:26
constexpr optref(const optref &other) noexcept
Copy constructor.
Definition optref.hpp:67
constexpr bool has_reference() const noexcept
Does it contain a reference?
Definition optref.hpp:115
constexpr optref(const optref< U > &other) noexcept
Converting copy constructor for base/derived class references.
Definition optref.hpp:77
constexpr optref(std::nullopt_t) noexcept
Nullopt constructor - no reference.
Definition optref.hpp:61
constexpr optref(std::reference_wrapper< T > wrapper) noexcept
Construct an optref from std::reference_wrapper.
Definition optref.hpp:95
std::add_lvalue_reference_t< T > reference_type
Reference type.
Definition optref.hpp:37
void reset() noexcept
Reset (cannot assign after).
Definition optref.hpp:168
constexpr const_reference_type reference() const
Get the contained const reference; throws if none.
Definition optref.hpp:147
constexpr optref(T &reference) noexcept
Construct an optref from reference.
Definition optref.hpp:88
void swap(optref &other) noexcept
Swap implementation.
Definition optref.hpp:160
std::reference_wrapper< value_type > reference_wrapper_type
Reference wrapper type for std::optional.
Definition optref.hpp:49
std::add_const_t< reference_type > const_reference_type
Const reference type.
Definition optref.hpp:43
T value_type
Value type.
Definition optref.hpp:31
constexpr reference_type reference()
Get the contained reference; throws if none.
Definition optref.hpp:136
constexpr optref() noexcept
Default constructor - std::optional contains no reference.
Definition optref.hpp:55
constexpr optref(T *pointer) noexcept
Construct an optref from a pointer.
Definition optref.hpp:102