-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync.h
More file actions
205 lines (175 loc) · 4.51 KB
/
async.h
File metadata and controls
205 lines (175 loc) · 4.51 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#pragma once
#include <functional>
#include <stdexcept>
#include <memory>
namespace aaa {
enum class resume_mode
{
normal,
cancel,
timeout,
close,
};
using continuation =
std::function<void(resume_mode, std::string)>;
/////////////////////////////////////////////////////////////////////
struct _Place_Holder
{
template<class... T>
void operator()(T&&...) const
{
}
};
// donot throw exception
template<class Ok, class Err = _Place_Holder, class Stop = _Place_Holder>
struct _Action
{
_Action(Ok ok, Err err={}, Stop stop={}) :
_ok(std::move(ok)),
_err(std::move(err)),
_stop(std::move(stop))
{
}
template<class Func>
_Action<Ok, Func, Stop> on_error(Func h) &&
{
return { std::move(_ok), std::move(h), std::move(_stop) };
}
template<class Func>
_Action<Ok, Err, Func> on_stop(Func h) &&
{
return { std::move(_ok), std::move(_err), std::move(h) };
}
Ok _ok;
Err _err;
Stop _stop;
};
template<class Ok>
_Action<Ok> on_success(Ok ok)
{
return _Action<Ok>{std::move(ok)};
}
/////////////////////////////////////////////////////////////////////
template<class T>
struct _Handler_Base
{
virtual void handle_success(T t) const = 0;
virtual void handle_error(std::runtime_error&) const = 0;
virtual void handle_stop(aaa::resume_mode, std::string) const = 0;
};
template<>
struct _Handler_Base<void>
{
virtual void handle_success() const = 0;
virtual void handle_error(std::runtime_error&) const = 0;
virtual void handle_stop(aaa::resume_mode, std::string) const = 0;
};
template<class T, class Ok, class Err, class Stop>
class _Handler : public _Handler_Base<T>
{
static_assert(std::is_invocable_v<Ok, T>, "bad type, Ok");
static_assert(std::is_invocable_v<Err, std::runtime_error&>, "bad type, Err");
static_assert(std::is_invocable_v<Stop, aaa::resume_mode, std::string>, "bad type, Stop");
public:
_Handler(_Action<Ok, Err, Stop> action) :
_action(std::move(action))
{
}
void handle_success(T t) const
{
if constexpr(std::is_reference_v<T>) {
_action._ok(t);
} else {
_action._ok(std::move(t));
}
}
void handle_error(std::runtime_error& err) const
{
_action._err(err);
}
void handle_stop(resume_mode mode, std::string message) const
{
_action._stop(mode, std::move(message));
}
private:
_Action<Ok, Err, Stop> _action;
};
template<class Ok, class Err, class Stop>
class _Handler<void, Ok, Err, Stop> : public _Handler_Base<void>
{
static_assert(std::is_invocable_v<Ok>, "bad type, Ok");
static_assert(std::is_invocable_v<Err, std::runtime_error&>, "bad type, Err");
static_assert(std::is_invocable_v<Stop, aaa::resume_mode, std::string>, "bad type, Stop");
public:
_Handler(_Action<Ok, Err, Stop> h) :
_action(std::move(h))
{
}
void handle_success() const
{
_action._ok();
}
void handle_error(std::runtime_error& err) const
{
_action._err(err);
}
void handle_stop(resume_mode mode, std::string message) const
{
_action._stop(mode, std::move(message));
}
private:
_Action<Ok, Err, Stop> _action;
};
template<class T>
class handler
{
public:
template<class Ok, class Err, class Stop>
handler(_Action<Ok, Err, Stop> action)
{
_ptr = std::make_shared<_Handler<T, Ok, Err, Stop>>(std::move(action));
}
void handle_success(T t) const
{
if constexpr(std::is_reference_v<T>) {
_ptr->handle_success(t);
} else {
_ptr->handle_success(std::move(t));
}
}
void handle_error(std::runtime_error& err) const
{
_ptr->handle_error(err);
}
void handle_stop(aaa::resume_mode mode, std::string message) const
{
_ptr->handle_stop(mode, std::move(message));
}
private:
std::shared_ptr<_Handler_Base<T>> _ptr;
};
template<>
class handler<void>
{
public:
template<class Ok, class Err, class Stop>
handler(_Action<Ok, Err, Stop> h)
{
_ptr = std::make_shared<_Handler<void, Ok, Err, Stop>>(std::move(h));
}
void handle_success() const
{
_ptr->handle_success();
}
void handle_error(std::runtime_error& err)
{
_ptr->handle_error(err);
}
void handle_stop(aaa::resume_mode mode, std::string message)
{
_ptr->handle_stop(mode, std::move(message));
}
private:
std::shared_ptr<_Handler_Base<void>> _ptr;
};
}