aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/cppcoro/task_result.h
blob: 54e66fd57f4348f7004f34d5286940b266c64ac4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#pragma once
#include <util/system/yassert.h>
#include <exception>
#include <variant>

namespace NActors {

    namespace NDetail {

        struct TVoid {};

        template<class T>
        struct TReplaceVoid {
            using TType = T;
        };

        template<>
        struct TReplaceVoid<void> {
            using TType = TVoid;
        };

        template<class T>
        struct TLValue {
            using TType = T&;
        };

        template<>
        struct TLValue<void> {
            using TType = void;
        };

        template<class T>
        struct TRValue {
            using TType = T&&;
        };

        template<>
        struct TRValue<void> {
            using TType = void;
        };

    } // namespace NDetail

    /**
     * Wrapper for the task result
     */
    template<class T>
    class TTaskResult {
    public:
        void SetValue()
            requires (std::same_as<T, void>)
        {
            Result.template emplace<1>();
        }

        template<class TResult>
        void SetValue(TResult&& result)
            requires (!std::same_as<T, void>)
        {
            Result.template emplace<1>(std::forward<TResult>(result));
        }

        void SetException(std::exception_ptr&& e) noexcept {
            Result.template emplace<2>(std::move(e));
        }

        typename NDetail::TLValue<T>::TType Value() & {
            switch (Result.index()) {
                case 0: {
                    Y_FAIL("Task result has no value");
                }
                case 1: {
                    if constexpr (std::same_as<T, void>) {
                        return;
                    } else {
                        return std::get<1>(Result);
                    }
                }
                case 2: {
                    std::exception_ptr& e = std::get<2>(Result);
                    Y_DEBUG_ABORT_UNLESS(e, "Task exception missing");
                    std::rethrow_exception(e);
                }
            }
            Y_FAIL("Task result has an invalid state");
        }

        typename NDetail::TRValue<T>::TType Value() && {
            switch (Result.index()) {
                case 0: {
                    Y_FAIL("Task result has no value");
                }
                case 1: {
                    if constexpr (std::same_as<T, void>) {
                        return;
                    } else {
                        return std::get<1>(std::move(Result));
                    }
                }
                case 2: {
                    std::exception_ptr& e = std::get<2>(Result);
                    Y_DEBUG_ABORT_UNLESS(e, "Task exception missing");
                    std::rethrow_exception(std::move(e));
                }
            }
            Y_FAIL("Task result has an invalid state");
        }

    private:
        std::variant<std::monostate, typename NDetail::TReplaceVoid<T>::TType, std::exception_ptr> Result;
    };

} // namespace NActors