| 22 | |
| 23 | template <typename TResult, typename TFailure> |
| 24 | class outcome { |
| 25 | public: |
| 26 | outcome(TResult const& s) : m_s(s), m_success(true) {} |
| 27 | outcome(TResult&& s) : m_s(std::move(s)), m_success(true) {} |
| 28 | |
| 29 | outcome(TFailure const& f) : m_f(f), m_success(false) {} |
| 30 | outcome(TFailure&& f) : m_f(std::move(f)), m_success(false) {} |
| 31 | |
| 32 | outcome(outcome const& other) : m_success(other.m_success) |
| 33 | { |
| 34 | if (m_success) { |
| 35 | new (&m_s) TResult(other.m_s); |
| 36 | } |
| 37 | else { |
| 38 | new (&m_f) TFailure(other.m_f); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | outcome(outcome&& other) noexcept : m_success(other.m_success) |
| 43 | { |
| 44 | if (m_success) { |
| 45 | new (&m_s) TResult(std::move(other.m_s)); |
| 46 | } |
| 47 | else { |
| 48 | new (&m_f) TFailure(std::move(other.m_f)); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | ~outcome() { destroy(); } |
| 53 | |
| 54 | outcome& operator=(outcome&& other) noexcept |
| 55 | { |
| 56 | assert(this != &other); |
| 57 | destroy(); |
| 58 | if (other.m_success) { |
| 59 | new (&m_s) TResult(std::move(other.m_s)); |
| 60 | } |
| 61 | else { |
| 62 | new (&m_f) TFailure(std::move(other.m_f)); |
| 63 | } |
| 64 | m_success = other.m_success; |
| 65 | return *this; |
| 66 | } |
| 67 | |
| 68 | TResult const& get_result() const& |
| 69 | { |
| 70 | assert(m_success); |
| 71 | return m_s; |
| 72 | } |
| 73 | |
| 74 | TResult&& get_result() && |
| 75 | { |
| 76 | assert(m_success); |
| 77 | return std::move(m_s); |
| 78 | } |
| 79 | |
| 80 | TFailure const& get_failure() const& |
| 81 | { |