|
1 | 1 | #pragma once |
2 | 2 |
|
3 | | -#include <expected> |
| 3 | +#include <memory> |
| 4 | +#include <type_traits> |
4 | 5 | #include <utility> |
| 6 | +#include <variant> |
5 | 7 |
|
6 | 8 | #include <QString> |
7 | 9 |
|
8 | 10 | namespace rembg::native_app { |
9 | 11 |
|
| 12 | +class Failure { |
| 13 | +public: |
| 14 | + explicit Failure(QString message) |
| 15 | + : message_(std::move(message)) {} |
| 16 | + |
| 17 | +private: |
| 18 | + template <class T> |
| 19 | + friend class Expected; |
| 20 | + |
| 21 | + QString message_; |
| 22 | +}; |
| 23 | + |
10 | 24 | template <class T> |
11 | | -using Expected = std::expected<T, QString>; |
| 25 | +class Expected { |
| 26 | +public: |
| 27 | + template <class U> |
| 28 | + requires( |
| 29 | + !std::is_same_v<std::remove_cvref_t<U>, Expected> |
| 30 | + && !std::is_same_v<std::remove_cvref_t<U>, Failure> |
| 31 | + && std::is_constructible_v<T, U&&> |
| 32 | + ) |
| 33 | + Expected(U&& value) |
| 34 | + : storage_(std::in_place_index<0>, std::forward<U>(value)) {} |
| 35 | + |
| 36 | + Expected(Failure failure) |
| 37 | + : storage_(std::in_place_index<1>, std::move(failure.message_)) {} |
| 38 | + |
| 39 | + Expected(const Expected&) = default; |
| 40 | + Expected(Expected&&) = default; |
| 41 | + Expected& operator=(const Expected&) = default; |
| 42 | + Expected& operator=(Expected&&) = default; |
| 43 | + ~Expected() = default; |
| 44 | + |
| 45 | + [[nodiscard]] bool has_value() const noexcept { return storage_.index() == 0; } |
| 46 | + [[nodiscard]] explicit operator bool() const noexcept { return has_value(); } |
| 47 | + |
| 48 | + [[nodiscard]] T& value() & { return std::get<0>(storage_); } |
| 49 | + [[nodiscard]] const T& value() const& { return std::get<0>(storage_); } |
| 50 | + [[nodiscard]] T&& value() && { return std::move(std::get<0>(storage_)); } |
| 51 | + |
| 52 | + [[nodiscard]] T& operator*() & { return value(); } |
| 53 | + [[nodiscard]] const T& operator*() const& { return value(); } |
| 54 | + [[nodiscard]] T&& operator*() && { return std::move(*this).value(); } |
| 55 | + |
| 56 | + [[nodiscard]] T* operator->() { return std::addressof(value()); } |
| 57 | + [[nodiscard]] const T* operator->() const { return std::addressof(value()); } |
| 58 | + |
| 59 | + [[nodiscard]] const QString& error() const& { return std::get<1>(storage_); } |
| 60 | + [[nodiscard]] QString&& error() && { return std::move(std::get<1>(storage_)); } |
| 61 | + |
| 62 | +private: |
| 63 | + std::variant<T, QString> storage_; |
| 64 | +}; |
12 | 65 |
|
13 | | -inline std::unexpected<QString> failure(QString message) { |
14 | | - return std::unexpected<QString>(std::move(message)); |
| 66 | +inline Failure failure(QString message) { |
| 67 | + return Failure(std::move(message)); |
15 | 68 | } |
16 | 69 |
|
17 | | -inline std::unexpected<QString> failure(const char* message) { |
| 70 | +inline Failure failure(const char* message) { |
18 | 71 | return failure(QString::fromUtf8(message)); |
19 | 72 | } |
20 | 73 |
|
|
0 commit comments