-
Notifications
You must be signed in to change notification settings - Fork 0
/
future.hxx
183 lines (158 loc) · 5.53 KB
/
future.hxx
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
#pragma once
namespace experimental {
namespace {
/*
* Call either:
* - return s.get_value()
* - s.get_value(); return;
*/
template<typename U>
struct value_getter
{
static U apply(impl::shared_state<U> &s){
return std::forward<U>(s.get_value());
}
};
template<>
struct value_getter<void> {
static void apply(impl::shared_state<void> &s){
s.get_value();
return;
}
};
}
template<typename T>
promise<T>::promise()
: _state(std::make_shared<impl::shared_state<T>>())
{
}
template<typename T>
void promise<T>::set_value(T &&t)
{
_state->set_value(std::forward<T>(t));
}
template<typename T>
void promise<T>::set_exception(std::exception_ptr p)
{
_state->set_exception(p);
}
template<typename T>
future<T> promise<T>::get_future()
{
return future<T>(_state);
}
template<typename T>
void promise<T>::swap(promise<T> &other) noexcept
{
std::swap(_state, other._state);
}
template<typename T>
future<T>::future() noexcept
{
}
template<typename T>
future<T>::future(future<T> &&f) noexcept
: _state(std::move(f._state))
{
}
template<typename T>
T future<T>::get()
{
_state->wait();
return value_getter<T>::apply(*_state);
}
template<typename T>
void future<T>::wait() const
{
_state->wait();
}
template<typename T>
template< class Rep, class Period >
future_status future<T>::wait_for( const std::chrono::duration<Rep,Period>& timeout_duration) const
{
return _state->wait_for(timeout_duration) ? future_status::ready : future_status::timeout;
}
template<typename T>
template< class Clock, class Duration >
future_status future<T>::wait_until( const std::chrono::time_point<Clock,Duration>& timeout_time ) const
{
return _state->wait_until(timeout_time) ? future_status::ready : future_status::timeout;
}
template<typename T>
bool future<T>::valid() const noexcept
{
return static_cast<bool>(_state);
}
template<typename T>
bool future<T>::is_ready() const
{
return valid() && _state->isReady();
}
template<typename T>
template<typename F>
auto future<T>::then(F && func) -> future<typename impl::utils::unwrap_future<decltype(func())>::value_type>
{
using RetTraits = impl::utils::unwrap_future<decltype(func())>;
using RetType = typename RetTraits::value_type;
auto state = std::make_shared<impl::shared_state<RetType>>();
_state->set_then([state=state, func=std::move(func)](std::exception_ptr &&p) mutable -> void
{
if(p) {
state->set_exception(p);
} else {
try {
impl::utils::apply_continuation<!std::is_same<RetType, void>::value, RetTraits>::invoke(*state,
func);
} catch(...) {
state->set_exception(std::current_exception());
}
}
});
return future<RetType>(std::move(state));
}
template<typename T>
template<typename F>
auto future<T>::then(F && func) -> future<typename impl::utils::unwrap_future<decltype(func(std::declval<T>()))>::value_type>
{
using RetTraits = impl::utils::unwrap_future<decltype(func(std::declval<T>()))>;
using RetType = typename RetTraits::value_type;
auto state = std::make_shared<impl::shared_state<RetType>>();
_state->set_then([state=state, func=std::move(func)](T const&t, std::exception_ptr &&p) mutable -> void
{
if(p) {
state->set_exception(p);
} else {
try {
impl::utils::apply_continuation<!std::is_same<RetType, void>::value, RetTraits>::invoke(*state,
func,
t);
} catch(...) {
state->set_exception(std::current_exception());
}
}
});
return future<RetType>(std::move(state));
}
template<typename T>
future<T>::future(std::shared_ptr<impl::shared_state<T>> state)
: _state(std::move(state))
{}
promise<void>::promise()
: _state(std::make_shared<impl::shared_state<void>>())
{
}
void promise<void>::set_value() {
_state->set_value();
}
void promise<void>::set_exception(std::exception_ptr p) {
_state->set_exception(p);
}
future<void> promise<void>::get_future()
{
return future<void>(_state);
}
void promise<void>::swap(promise<void> &other) noexcept
{
std::swap(_state, other._state);
}
}//!experimental