Debian Codegraph @

src/utils/optional.hpp

raw
1 #ifndef OPTIONAL_HPP
2 #define OPTIONAL_HPP
3
4 #include <functional>
5 #include <new>
6 #include <type_traits>
7 #include <utility>
8
9 #include "utils/common.hpp"
10 #include "utils/error.hpp"
11
12 CPPTRACE_BEGIN_NAMESPACE
13 namespace detail {
14 struct nullopt_t {};
15
16 static constexpr nullopt_t nullopt;
17
18 template<
19 typename T,
20 typename std::enable_if<
21 !std::is_same<typename std::decay<T>::type, void>::value && !std::is_rvalue_reference<T>::value,
22 int
23 >::type = 0
24 >
25 using well_behaved = typename std::conditional<
26 std::is_reference<T>::value, std::reference_wrapper<typename std::remove_reference<T>::type>, T
27 >::type;
28
29 template<
30 typename T,
31 typename std::enable_if<!std::is_same<typename std::decay<T>::type, void>::value, int>::type = 0
32 >
33 class optional {
34 using value_type = well_behaved<T>;
35
36 union {
37 char x{};
38 value_type uvalue;
39 };
40
41 bool holds_value = false;
42
43 public:
44 optional() noexcept {}
45
46 optional(nullopt_t) noexcept {}
47
48 ~optional() {
49 reset();
50 }
51
52 optional(const optional& other) : holds_value(other.holds_value) {
53 if(holds_value) {
54 new (static_cast<void*>(std::addressof(uvalue))) value_type(other.uvalue);
55 }
56 }
57
58 optional(optional&& other)
59 noexcept(std::is_nothrow_move_constructible<value_type>::value)
60 : holds_value(other.holds_value)
61 {
62 if(holds_value) {
63 new (static_cast<void*>(std::addressof(uvalue))) value_type(std::move(other.uvalue));
64 }
65 }
66
67 optional& operator=(const optional& other) {
68 optional copy(other);
69 swap(copy);
70 return *this;
71 }
72
73 optional& operator=(optional&& other)
74 noexcept(std::is_nothrow_move_assignable<value_type>::value && std::is_nothrow_move_constructible<value_type>::value)
75 {
76 if (this != &other) {
77 reset();
78 if (other.holds_value) {
79 new (static_cast<void*>(std::addressof(uvalue))) value_type(std::move(other.uvalue));
80 holds_value = true;
81 }
82 }
83 return *this;
84 }
85
86 template<
87 typename U = T,
88 typename std::enable_if<!std::is_same<typename std::decay<U>::type, optional<T>>::value, int>::type = 0
89 >
90 optional(U&& value) : holds_value(true) {
91 new (static_cast<void*>(std::addressof(uvalue))) value_type(std::forward<U>(value));
92 }
93
94 template<
95 typename U = T,
96 typename std::enable_if<!std::is_same<typename std::decay<U>::type, optional<T>>::value, int>::type = 0
97 >
98 optional& operator=(U&& value) {
99 optional o(std::forward<U>(value));
100 swap(o);
101 return *this;
102 }
103
104 optional& operator=(nullopt_t) noexcept {
105 reset();
106 return *this;
107 }
108
109 void swap(optional& other) noexcept {
110 if(holds_value && other.holds_value) {
111 std::swap(uvalue, other.uvalue);
112 } else if(holds_value && !other.holds_value) {
113 new (&other.uvalue) value_type(std::move(uvalue));
114 uvalue.~value_type();
115 } else if(!holds_value && other.holds_value) {
116 new (static_cast<void*>(std::addressof(uvalue))) value_type(std::move(other.uvalue));
117 other.uvalue.~value_type();
118 }
119 std::swap(holds_value, other.holds_value);
120 }
121
122 bool has_value() const {
123 return holds_value;
124 }
125
126 explicit operator bool() const {
127 return holds_value;
128 }
129
130 void reset() {
131 if(holds_value) {
132 uvalue.~value_type();
133 }
134 holds_value = false;
135 }
136
137 NODISCARD T& unwrap() & {
138 ASSERT(holds_value, "Optional does not contain a value");
139 return uvalue;
140 }
141
142 NODISCARD const T& unwrap() const & {
143 ASSERT(holds_value, "Optional does not contain a value");
144 return uvalue;
145 }
146
147 NODISCARD T&& unwrap() && {
148 ASSERT(holds_value, "Optional does not contain a value");
149 return std::move(uvalue);
150 }
151
152 NODISCARD const T&& unwrap() const && {
153 ASSERT(holds_value, "Optional does not contain a value");
154 return std::move(uvalue);
155 }
156
157 template<typename U>
158 NODISCARD T value_or(U&& default_value) const & {
159 return holds_value ? static_cast<T>(uvalue) : static_cast<T>(std::forward<U>(default_value));
160 }
161
162 template<typename U>
163 NODISCARD T value_or(U&& default_value) && {
164 return holds_value ? static_cast<T>(std::move(uvalue)) : static_cast<T>(std::forward<U>(default_value));
165 }
166 };
167 }
168 CPPTRACE_END_NAMESPACE
169
170 #endif