7 #include "../generic/static_if.h"
9 #include "../threads/thread.h"
13 namespace promise_impl {
17 typedef std::shared_ptr<PromiseState<T>> ptr;
24 typedef std::shared_ptr<PromiseState<void>> ptr;
29 template<
typename Func,
typename T>
32 void operator()(Func f,
typename PromiseState<T>::ptr state) {
37 template<
typename Func>
40 void operator()(Func f,
typename PromiseState<void>::ptr state) {
49 template <
typename C,
typename Ret,
typename... Args>
51 using result_type = Ret;
53 template <std::
size_t i>
55 using type =
typename std::tuple_element<i, std::tuple<Args...>>::type;
62 void cr_yield_for(
const Seconds& seconds);
63 void cr_run_main(std::function<
void ()> func);
68 bool is_ready()
const {
69 return state_ && state_->value;
73 assert(state_->value);
74 return (state_->value.value());
79 template<
typename Func>
85 while(!state->value) {
89 return func(state->value.value());
98 bool is_initialized()
const {
103 template<
typename Func>
104 friend Promise<
typename std::result_of<Func()>::type> cr_async(Func func);
106 Promise(
typename promise_impl::PromiseState<T>::ptr state):
111 typename promise_impl::PromiseState<T>::ptr state_;
117 template<
typename Func>
118 friend Promise<
typename std::result_of<Func()>::type> cr_async(Func func);
120 Promise(
typename promise_impl::PromiseState<void>::ptr state):
125 typename promise_impl::PromiseState<void>::ptr state_;
130 bool is_initialized()
const {
135 bool is_ready()
const {
136 return state_ && state_->value;
139 void value()
const {}
143 template<
typename Func>
148 while(!state->value) {
159 void _trigger_coroutine(std::function<
void ()> func);
160 void _trigger_idle_updates();
162 template<
typename Func>
163 Promise<
typename std::result_of<Func()>::type> cr_async(Func func) {
164 typedef typename std::result_of<Func()>::type T;
166 auto state = std::make_shared<typename promise_impl::PromiseState<T>>();
169 _trigger_coroutine([state, func]() {
186 T cr_await(
const Promise<T>& promise) {
187 while(!promise.is_ready()) {
188 if(cort::within_coroutine()){
191 _trigger_idle_updates();
196 return promise.value();