#pragma once | #pragma once | ||||
#define RESUMEF_LIB_INCLUDE 1 | |||||
//#include <yvals.h> | //#include <yvals.h> | ||||
#include <atomic> | #include <atomic> | ||||
#include <memory> | #include <memory> |
{ | { | ||||
_future = std::move(_context()); | _future = std::move(_context()); | ||||
} | } | ||||
ctx_task_t(const context_type & ctx) | |||||
: _context(ctx) | |||||
{ | |||||
_future = std::move(_context()); | |||||
} | |||||
}; | }; | ||||
} | } |
inline void operator + (_Ty && t_) | inline void operator + (_Ty && t_) | ||||
{ | { | ||||
typedef typename std::conditional< | typedef typename std::conditional< | ||||
std::is_callable_v<_Ty>, | |||||
std::is_callable<_Ty>::value, | |||||
ctx_task_t<_Ty>, | ctx_task_t<_Ty>, | ||||
task_t<_Ty> >::type task_type; | task_t<_Ty> >::type task_type; | ||||
return new_task(new task_type(std::forward<_Ty>(t_))); | return new_task(new task_type(std::forward<_Ty>(t_))); |
public: | public: | ||||
timer_handler() = default; | timer_handler() = default; | ||||
timer_handler(const timer_handler &) = default; | timer_handler(const timer_handler &) = default; | ||||
timer_handler(timer_handler && right_); | |||||
timer_handler(timer_handler && right_) noexcept; | |||||
timer_handler & operator = (const timer_handler &) = default; | timer_handler & operator = (const timer_handler &) = default; | ||||
timer_handler & operator = (timer_handler && right_); | |||||
timer_handler & operator = (timer_handler && right_) noexcept; | |||||
timer_handler(timer_manager * manager_, const detail::timer_target_ptr & target_); | timer_handler(timer_manager * manager_, const detail::timer_target_ptr & target_); | ||||
, _target(target_) | , _target(target_) | ||||
{ | { | ||||
} | } | ||||
inline timer_handler::timer_handler(timer_handler && right_) | |||||
inline timer_handler::timer_handler(timer_handler && right_) noexcept | |||||
: _manager(std::move(right_._manager)) | : _manager(std::move(right_._manager)) | ||||
, _target(std::move(right_._target)) | , _target(std::move(right_._target)) | ||||
{ | { | ||||
} | } | ||||
inline timer_handler & timer_handler::operator = (timer_handler && right_) | |||||
inline timer_handler & timer_handler::operator = (timer_handler && right_) noexcept | |||||
{ | { | ||||
if (this != &right_) | if (this != &right_) | ||||
{ | { |
namespace std | namespace std | ||||
{ | { | ||||
template<typename _Function> | template<typename _Function> | ||||
inline auto _IsCallable(const _Function & _Func, int) -> decltype(_Func(), true_type()) | |||||
inline auto _IsCallable(_Function && _Func, int) -> decltype(_Func(), true_type()) | |||||
{ | { | ||||
(_Func); | (_Func); | ||||
return true_type(); | return true_type(); | ||||
} | } | ||||
template<typename _Function> | template<typename _Function> | ||||
inline false_type _IsCallable(const _Function &, ...) | |||||
inline false_type _IsCallable(_Function &&, ...) | |||||
{ | { | ||||
return false_type(); | return false_type(); | ||||
} | } | ||||
template<typename _Function> | template<typename _Function> | ||||
using is_callable = decltype(_IsCallable(std::declval<_Function>(), 0)); | using is_callable = decltype(_IsCallable(std::declval<_Function>(), 0)); | ||||
#if _HAS_CXX17 | |||||
template<typename _Function> | template<typename _Function> | ||||
_INLINE_VAR constexpr bool is_callable_v = is_callable<_Function>::value; | _INLINE_VAR constexpr bool is_callable_v = is_callable<_Function>::value; | ||||
#endif | |||||
} | } | ||||
GO | GO | ||||
{ | { | ||||
co_await when_all(); | co_await when_all(); | ||||
std::cout << "zero!" << std::endl << std::endl; | |||||
std::cout << "when all: zero!" << std::endl << std::endl; | |||||
auto [a, b] = co_await when_all(my_sleep("a"), my_sleep_v("b")); | auto [a, b] = co_await when_all(my_sleep("a"), my_sleep_v("b")); | ||||
b; //b is std::ignore | |||||
std::cout << a << std::endl << std::endl; | |||||
(void)b; //b is std::ignore | |||||
std::cout << "when all:" << a << std::endl << std::endl; | |||||
auto c = co_await my_sleep("c"); | auto c = co_await my_sleep("c"); | ||||
std::cout << c << std::endl << std::endl; | |||||
std::cout << "when all:" << c << std::endl << std::endl; | |||||
auto [d, e, f] = co_await when_all(my_sleep("d"), my_sleep_v("e"), my_sleep("f")); | auto [d, e, f] = co_await when_all(my_sleep("d"), my_sleep_v("e"), my_sleep("f")); | ||||
e; //e is std::ignore | |||||
std::cout << d << "," << f << std::endl << std::endl; | |||||
(void)e; //e is std::ignore | |||||
std::cout << "when all:" << d << "," << f << std::endl << std::endl; | |||||
std::vector<future_t<int> > v{ my_sleep("g"), my_sleep("h"), my_sleep("i") }; | std::vector<future_t<int> > v{ my_sleep("g"), my_sleep("h"), my_sleep("i") }; | ||||
auto vals = co_await when_all(std::begin(v), std::end(v)); | auto vals = co_await when_all(std::begin(v), std::end(v)); | ||||
std::cout << vals[0] << "," << vals[1] << "," << vals[2] << "," << std::endl << std::endl; | |||||
std::cout << "when all:" << vals[0] << "," << vals[1] << "," << vals[2] << "," << std::endl << std::endl; | |||||
std::cout << "all range done!" << std::endl; | std::cout << "all range done!" << std::endl; | ||||
}; | }; |
} | } | ||||
*/ | */ | ||||
#define co_yield_void co_yield nullptr | |||||
#define co_return_void co_return nullptr | |||||
auto test_yield_void() | auto test_yield_void() | ||||
{ | { | ||||
std::cout << "block 1 will yield return" << std::endl; | std::cout << "block 1 will yield return" << std::endl; |
int main(int argc, const char * argv[]) | int main(int argc, const char * argv[]) | ||||
{ | { | ||||
resumable_main_sleep(); | |||||
//resumable_main_sleep(); | |||||
//resumable_main_resumable(); | //resumable_main_resumable(); | ||||
/* | |||||
resumable_main_when_all(); | resumable_main_when_all(); | ||||
resumable_main_multi_thread(); | resumable_main_multi_thread(); | ||||
resumable_main_yield_return(); | resumable_main_yield_return(); | ||||
resumable_main_channel(); | resumable_main_channel(); | ||||
resumable_main_cb(); | resumable_main_cb(); | ||||
resumable_main_exception(); | resumable_main_exception(); | ||||
*/ | |||||
return 0; | return 0; | ||||
} | } |