Loading [MathJax]/extensions/tex2jax.js
Iros
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages Concepts
Loading...
Searching...
No Matches
expected.h
Go to the documentation of this file.
1#pragma once
2
5#include "di/meta/core.h"
6#include "di/meta/language.h"
8#include "di/meta/trivial.h"
9#include "di/meta/util.h"
10#include "di/meta/vocab.h"
11#include "di/util/addressof.h"
13#include "di/util/destroy_at.h"
14#include "di/util/forward.h"
16#include "di/util/move.h"
23
24namespace di::vocab {
25namespace detail {
26 template<typename Expected, typename From, typename To>
29}
30
31template<typename T, typename E>
33class [[nodiscard]] Expected<T, E> : public function::monad::MonadInterface<Expected<T, E>> {
34public:
35 using Value = T;
36 using Error = E;
37
38 constexpr Expected()
40 : m_value() {}
41
42 constexpr Expected(Expected const&) = default;
43 constexpr Expected(Expected&&) = default;
44
45 constexpr Expected(Expected const&)
47 = delete;
48
55
62
63 template<typename U, typename G>
68 internal_construct_from_expected(other);
69 }
70
71 template<typename U, typename G>
76 internal_construct_from_expected(util::move(other));
77 }
78
79 template<typename U = T>
83 : m_value(util::forward<U>(value)) {}
84
85 template<typename G>
88 : m_has_error(true), m_error(error.error()) {}
89
90 template<typename G>
93 : m_has_error(true), m_error(util::move(error).error()) {}
94
95 template<typename... Args>
96 requires(concepts::ConstructibleFrom<T, Args...>)
97 constexpr explicit Expected(types::InPlace, Args&&... args)
98 : m_value(types::in_place, util::forward<Args>(args)...) {}
99
100 template<typename U, typename... Args>
102 constexpr explicit Expected(types::InPlace, std::initializer_list<U> list, Args&&... args)
103 : m_value(types::in_place, list, util::forward<Args>(args)...) {}
104
105 template<typename... Args>
106 requires(concepts::ConstructibleFrom<E, Args...>)
107 constexpr explicit Expected(types::Unexpect, Args&&... args)
108 : m_has_error(true), m_error(types::in_place, util::forward<Args>(args)...) {}
109
110 template<typename U, typename... Args>
112 constexpr explicit Expected(types::Unexpect, std::initializer_list<U> list, Args&&... args)
113 : m_has_error(true), m_error(types::in_place, list, util::forward<Args>(args)...) {}
114
115 constexpr ~Expected() = default;
116
117 constexpr ~Expected()
119 {
120 internal_reset();
121 }
122
123 constexpr auto operator=(Expected const& other)
125 return internal_assign_from_expected(other);
126 }
127
128 constexpr auto operator=(Expected&& other)
130 return internal_assign_from_expected(util::move(other));
131 }
132
133 template<typename U = T>
135 constexpr auto operator=(U&& value) -> Expected& {
136 return internal_assign_from_value(util::forward<U>(value));
137 }
138
139 template<typename G>
141 constexpr auto operator=(Unexpected<G> const& error) -> Expected& {
142 return internal_assign_from_unexpected(error);
143 }
144
145 template<typename G>
147 constexpr auto operator=(Unexpected<G>&& error) -> Expected& {
148 return internal_assign_from_unexpected(util::move(error));
149 }
150
151 constexpr auto operator->() { return util::addressof(value()); }
152 constexpr auto operator->() const { return util::addressof(value()); }
153
154 constexpr auto operator*() & -> T& { return value(); }
155 constexpr auto operator*() const& -> T const& { return value(); }
156 constexpr auto operator*() && -> T&& { return util::move(*this).value(); }
157 constexpr auto operator*() const&& -> T const&& { return util::move(*this).value(); }
158
159 constexpr explicit operator bool() const { return has_value(); }
160 constexpr auto has_value() const -> bool { return !m_has_error; }
161
162 constexpr auto value() & -> T& {
164 return m_value.value();
165 }
166 constexpr auto value() const& -> T const& {
168 return m_value.value();
169 }
170 constexpr auto value() && -> T&& {
172 return util::move(m_value).value();
173 }
174 constexpr auto value() const&& -> T const&& {
176 return util::move(m_value).value();
177 }
178
179 constexpr auto error() & -> E& {
181 return m_error.value();
182 }
183 constexpr auto error() const& -> E const& {
185 return m_error.value();
186 }
187 constexpr auto error() && -> E&& {
189 return util::move(m_error).value();
190 }
191 constexpr auto error() const&& -> E const&& {
193 return util::move(m_error).value();
194 }
195
196 template<concepts::ConvertibleTo<T> U>
198 constexpr auto value_or(U&& default_value) const& -> T {
199 return has_value() ? **this : static_cast<T>(util::forward<U>(default_value));
200 }
201
202 template<concepts::ConvertibleTo<T> U>
204 constexpr auto value_or(U&& default_value) && -> T {
205 return has_value() ? *util::move(*this) : static_cast<T>(util::forward<U>(default_value));
206 }
207
208 constexpr auto optional_value() const& -> Optional<T>
209 requires(concepts::CopyConstructible<T>)
210 {
211 return has_value() ? Optional<T> { in_place, **this } : nullopt;
212 }
213
214 constexpr auto optional_value() && -> Optional<T>
216 {
217 return has_value() ? Optional<T> { in_place, *util::move(*this) } : nullopt;
218 }
219
220 template<typename... Args>
221 requires(concepts::ConstructibleFrom<T, Args...>)
222 constexpr auto emplace(Args&&... args) -> T& {
223 return internal_emplace(util::forward<Args>(args)...);
224 }
225
226 template<typename U, typename... Args>
228 constexpr auto emplace(std::initializer_list<U> list, Args&&... args) -> T& {
229 return internal_emplace(list, util::forward<Args>(args)...);
230 }
231
232 constexpr auto __try_did_fail() && -> Unexpected<E> {
233 return Unexpected<E> { in_place, util::move(*this).error() };
234 }
235 constexpr auto __try_did_succeed() && -> Expected { return Expected { in_place, util::move(*this).value() }; }
236 constexpr auto __try_move_out() && -> T&& { return util::move(*this).value(); }
237
238private:
239 template<typename U, typename G>
240 friend class Expected;
241
242 template<concepts::EqualityComparableWith<T> U, concepts::EqualityComparableWith<E> G>
243 constexpr friend auto operator==(Expected const& a, Expected<U, G> const& b) -> bool {
244 if (a.has_value() != b.has_value()) {
245 return false;
246 }
247 if (a.has_value() == b.has_value()) {
248 return a.value() == b.value();
249 }
250 return a.error() == b.error();
251 }
252
253 template<typename U>
255 constexpr friend auto operator==(Expected const& a, U const& b) -> bool {
256 return a.has_value() && a.value() == b;
257 }
258
259 template<concepts::EqualityComparableWith<E> G>
260 constexpr friend auto operator==(Expected const& a, Unexpected<G> const& b) -> bool {
261 return !a.has_value() && a.error() == b.error();
262 }
263
264 template<concepts::RemoveCVRefSameAs<Expected> Self, typename F,
265 typename U = meta::UnwrapRefDecay<meta::InvokeResult<F, meta::Like<Self, T>>>>
267 constexpr friend auto tag_invoke(types::Tag<function::monad::fmap>, Self&& self, F&& function) -> Expected<U, E> {
268 if (!self) {
269 return Expected<U, E> { types::unexpect, util::forward<Self>(self).error() };
270 }
271 if constexpr (concepts::LanguageVoid<U>) {
272 function::invoke(util::forward<F>(function), util::forward<Self>(self).value());
273 return {};
274 } else {
275 return function::invoke(util::forward<F>(function), util::forward<Self>(self).value());
276 }
277 }
278
279 template<concepts::RemoveCVRefSameAs<Expected> Self, typename F,
280 typename R = meta::InvokeResult<F, meta::Like<Self, T>>>
282 constexpr friend auto tag_invoke(types::Tag<function::monad::bind>, Self&& self, F&& function) -> R {
283 if (!self) {
284 return R { types::unexpect, util::forward<Self>(self).error() };
285 }
286 return function::invoke(util::forward<F>(function), util::forward<Self>(self).value());
287 }
288
289 template<concepts::RemoveCVRefSameAs<Expected> Self, typename F,
290 typename R = meta::InvokeResult<F, meta::Like<Self, E>>>
292 constexpr friend auto tag_invoke(types::Tag<function::monad::fail>, Self&& self, F&& function) -> R {
293 if (self) {
294 return R { types::in_place, util::forward<Self>(self).value() };
295 }
296 return function::invoke(util::forward<F>(function), util::forward<Self>(self).error());
297 }
298
299 template<concepts::RemoveCVRefSameAs<Expected> Self, typename F,
300 typename G = meta::UnwrapRefDecay<meta::InvokeResult<F, meta::Like<Self, E>>>>
301 constexpr friend auto tag_invoke(types::Tag<function::monad::fmap_right>, Self&& self, F&& function)
302 -> Expected<T, G> {
303 if (self) {
304 return Expected<T, G> { types::in_place, util::forward<Self>(self).value() };
305 }
306 if constexpr (concepts::LanguageVoid<G>) {
307 function::invoke(util::forward<F>(function), util::forward<Self>(self).error());
308 return Expected<T, G> { types::in_place, util::forward<Self>(self).value() };
309 } else {
311 function::invoke(util::forward<F>(function), util::forward<Self>(self).error()) };
312 }
313 }
314
315 template<typename U>
316 constexpr void internal_construct_from_expected(U&& other) {
317 auto other_has_value = other.has_value();
318 if (other_has_value) {
319 util::construct_at(&this->m_value, di::forward<U>(other).value());
320 } else {
321 util::construct_at(&this->m_error, di::forward<U>(other).error());
322 }
323 this->m_has_error = !other_has_value;
324 }
325
326 constexpr void internal_reset() {
327 if (has_value()) {
328 util::destroy_at(&m_value);
329 } else {
330 util::destroy_at(&m_error);
331 }
332 }
333
334 template<typename U>
335 constexpr auto internal_assign_from_expected(U&& other) -> Expected& {
336 if (this->has_value() && other.has_value()) {
337 this->m_value = util::forward<U>(other).value();
338 } else if (this->has_value() && !other.has_value()) {
339 internal_reset();
340 util::construct_at(&this->m_error, util::forward<U>(other).error());
341 } else if (!this->has_value() && other.has_value()) {
342 internal_reset();
343 util::construct_at(&this->m_value, util::forward<U>(other).value());
344 } else {
345 this->m_error = util::forward<U>(other).error();
346 }
347 this->m_has_error = !other.has_value();
348 return *this;
349 }
350
351 template<typename U>
352 constexpr auto internal_assign_from_value(U&& value) -> Expected& {
353 if (this->has_value()) {
354 this->m_value = util::forward<U>(value);
355 } else {
356 internal_reset();
357 util::construct_at(&this->m_value, util::forward<U>(value));
358 }
359 this->m_has_error = false;
360 return *this;
361 }
362
363 template<typename U>
364 constexpr auto internal_assign_from_unexpected(U&& unexpected) -> Expected& {
365 if (this->has_value()) {
366 internal_reset();
367 util::construct_at(&this->m_error, util::forward<U>(unexpected).error());
368 } else {
369 this->m_error = util::forward<U>(unexpected).error();
370 }
371 this->m_has_error = true;
372 return *this;
373 }
374
375 template<typename... Args>
376 constexpr auto internal_emplace(Args&&... args) -> T& {
377 if (this->has_value()) {
378 m_value.emplace(util::forward<Args>(args)...);
379 } else {
380 internal_reset();
381 util::construct_at(&this->m_value, types::in_place, util::forward<Args>(args)...);
382 }
383 this->m_has_error = false;
384 return value();
385 }
386
387 template<typename U, typename... Args>
388 constexpr auto internal_emplace(std::initializer_list<U> list, Args&&... args) -> T& {
389 if (this->has_value()) {
390 m_value.emplace(list, util::forward<Args>(args)...);
391 } else {
392 internal_reset();
393 util::construct_at(&this->m_value, types::in_place, list, util::forward<Args>(args)...);
394 }
395 this->m_has_error = false;
396 return value();
397 }
398
399 bool m_has_error { false };
404};
405}
#define DI_ASSERT(...)
Definition assert_bool.h:7
Definition rebindable_box.h:42
Definition expected_forward_declaration.h:8
E Error
Definition expected.h:36
constexpr friend auto tag_invoke(types::Tag< function::monad::fmap >, Self &&self, F &&function) -> Expected< U, E >
Definition expected.h:267
constexpr auto __try_move_out() &&-> T &&
Definition expected.h:236
constexpr auto has_value() const -> bool
Definition expected.h:160
constexpr auto value() &-> T &
Definition expected.h:162
constexpr G const & other
Definition expected.h:67
constexpr auto value_or(U &&default_value) const &-> T
Definition expected.h:198
constexpr auto operator*() &-> T &
Definition expected.h:154
constexpr Expected(types::InPlace, std::initializer_list< U > list, Args &&... args)
Definition expected.h:102
friend class Expected
Definition expected.h:240
constexpr auto optional_value() &&-> Optional< T > requires(concepts::MoveConstructible< T >)
Definition expected.h:214
constexpr friend auto tag_invoke(types::Tag< function::monad::fail >, Self &&self, F &&function) -> R
Definition expected.h:292
constexpr auto value_or(U &&default_value) &&-> T
Definition expected.h:204
constexpr auto emplace(Args &&... args) -> T &
Definition expected.h:222
constexpr auto operator*() &&-> T &&
Definition expected.h:156
constexpr auto value() const &&-> T const &&
Definition expected.h:174
constexpr auto operator*() const &-> T const &
Definition expected.h:155
constexpr Expected(types::InPlace, Args &&... args)
Definition expected.h:97
constexpr auto value() const &-> T const &
Definition expected.h:166
constexpr friend auto operator==(Expected const &a, Expected< U, G > const &b) -> bool
Definition expected.h:243
constexpr auto optional_value() const &-> Optional< T > requires(concepts::CopyConstructible< T >)
Definition expected.h:208
constexpr auto error() &&-> E &&
Definition expected.h:187
constexpr friend auto operator==(Expected const &a, Unexpected< G > const &b) -> bool
Definition expected.h:260
constexpr auto error() const &-> E const &
Definition expected.h:183
constexpr ~Expected()=default
constexpr Expected(Expected &&)=default
T Value
Definition expected.h:35
constexpr auto operator=(Expected const &other) -> Expected &requires(concepts::CopyConstructible< T > &&concepts::CopyConstructible< E >)
Definition expected.h:123
constexpr friend auto tag_invoke(types::Tag< function::monad::fmap_right >, Self &&self, F &&function) -> Expected< T, G >
Definition expected.h:301
constexpr auto value() &&-> T &&
Definition expected.h:170
constexpr Expected(Expected &&other)
Definition expected.h:56
util::RebindableBox< T > m_value
Definition expected.h:401
constexpr auto __try_did_succeed() &&-> Expected
Definition expected.h:235
constexpr auto error() const &&-> E const &&
Definition expected.h:191
constexpr auto emplace(std::initializer_list< U > list, Args &&... args) -> T &
Definition expected.h:228
constexpr Expected(Expected const &)=default
constexpr Expected(types::Unexpect, Args &&... args)
Definition expected.h:107
constexpr Expected(Expected const &)=delete
constexpr auto operator=(Expected &&other) -> Expected &requires(concepts::MoveConstructible< T > &&concepts::MoveConstructible< E >)
Definition expected.h:128
constexpr Expected(Expected const &other)
Definition expected.h:49
constexpr auto operator*() const &&-> T const &&
Definition expected.h:157
constexpr Expected()
Definition expected.h:38
constexpr ~Expected()
Definition expected.h:117
util::RebindableBox< E > m_error
Definition expected.h:402
constexpr auto __try_did_fail() &&-> Unexpected< E >
Definition expected.h:232
constexpr auto operator->()
Definition expected.h:151
constexpr Expected(types::Unexpect, std::initializer_list< U > list, Args &&... args)
Definition expected.h:112
constexpr friend auto tag_invoke(types::Tag< function::monad::bind >, Self &&self, F &&function) -> R
Definition expected.h:282
constexpr auto error() &-> E &
Definition expected.h:179
constexpr auto operator->() const
Definition expected.h:152
Definition optional_forward_declaration.h:5
Definition unexpected.h:14
Definition operations.h:11
Definition operations.h:99
Definition operations.h:34
Definition operations.h:24
Definition vocab.h:30
Definition core.h:128
Definition operations.h:43
Definition core.h:114
Definition vocab.h:152
Definition expected_can_convert_constructor.h:9
Definition any_storable.h:9
Definition as_bool.h:8
constexpr auto invoke
Definition invoke.h:100
constexpr auto value
Definition value.h:34
meta::RemoveCVRef< T >::Error ExpectedError
Definition vocab.h:38
constexpr auto ExpectedRank
Definition vocab.h:41
meta::RemoveCVRef< T >::Value ExpectedValue
Definition vocab.h:35
Definition method.h:5
constexpr auto in_place
Definition in_place.h:8
di::meta::Decay< decltype(T)> Tag
Definition tag_invoke.h:28
constexpr auto unexpect
Definition unexpect.h:8
Definition vocab.h:96
constexpr auto destroy_at
Definition destroy_at.h:24
constexpr auto construct_at
Definition construct_at.h:27
Definition erasure_cast.h:7
Definition lazy.h:165
constexpr auto nullopt
Definition nullopt.h:15
Expected(T &&) -> Expected< meta::UnwrapRefDecay< T >, void >
constexpr auto in_place
Definition in_place.h:8
@ U
Definition key.h:30
@ T
Definition key.h:29
Definition in_place.h:4
Definition unexpect.h:4