| 1 |
#ifndef INCLUDED_PROMISEBASE_ |
| 2 |
#define INCLUDED_PROMISEBASE_ |
| 3 |
|
| 4 |
//base |
| 5 |
#include <cstddef> |
| 6 |
#include <future> |
| 7 |
#include <coroutine> |
| 8 |
|
| 9 |
template <typename Handler, typename State> |
| 10 |
struct PromiseBase |
| 11 |
{ |
| 12 |
Handler get_return_object(); |
| 13 |
|
| 14 |
static std::suspend_always initial_suspend(); |
| 15 |
static std::suspend_always final_suspend() noexcept; |
| 16 |
static void unhandled_exception(); |
| 17 |
static void return_void(); |
| 18 |
}; |
| 19 |
|
| 20 |
|
| 21 |
template <typename Handler, typename State> |
| 22 |
inline void PromiseBase<Handler, State>::return_void() |
| 23 |
{} |
| 24 |
|
| 25 |
template <typename Handler, typename State> |
| 26 |
inline std::suspend_always PromiseBase<Handler, State>::initial_suspend() |
| 27 |
{ |
| 28 |
return {}; |
| 29 |
} |
| 30 |
|
| 31 |
template <typename Handler, typename State> |
| 32 |
inline std::suspend_always PromiseBase<Handler, State>::final_suspend() noexcept |
| 33 |
{ |
| 34 |
return {}; |
| 35 |
} |
| 36 |
|
| 37 |
template <typename Handler, typename State> |
| 38 |
inline void PromiseBase<Handler, State>::unhandled_exception() |
| 39 |
{ |
| 40 |
std::rethrow_exception(std::current_exception()); |
| 41 |
} |
| 42 |
|
| 43 |
template <typename Handler, typename State> |
| 44 |
inline Handler PromiseBase<Handler, State>::get_return_object() |
| 45 |
{ |
| 46 |
return Handler{ std::coroutine_handle<State>::from_promise( |
| 47 |
static_cast<State &>(*this) ) |
| 48 |
}; |
| 49 |
} |
| 50 |
//= |
| 51 |
#endif |