extern std::atomic<intptr_t> g_resumef_evtctx_count; | extern std::atomic<intptr_t> g_resumef_evtctx_count; | ||||
#endif | #endif | ||||
namespace std | |||||
{ | |||||
#if !_HAS_CXX20 | |||||
template<class T> | |||||
struct remove_cvref | |||||
{ | |||||
typedef std::remove_cv_t<std::remove_reference_t<T>> type; | |||||
}; | |||||
#if _HAS_CXX17 | |||||
template<class T> | |||||
using remove_cvref_t = typename std::remove_cvref<T>::type; | |||||
#endif | |||||
#endif | |||||
} | |||||
namespace resumef | namespace resumef | ||||
{ | { | ||||
#if _HAS_CXX17 | #if _HAS_CXX17 | ||||
template<class... _Mutexes> | template<class... _Mutexes> | ||||
using scoped_lock = std::lock_guard<_Mutexes...>; | using scoped_lock = std::lock_guard<_Mutexes...>; | ||||
#endif | #endif | ||||
template<typename _PromiseT = void> | template<typename _PromiseT = void> | ||||
using coroutine_handle = std::experimental::coroutine_handle<_PromiseT>; | using coroutine_handle = std::experimental::coroutine_handle<_PromiseT>; | ||||
//这个'函数对象'被调用后,返回generator<_Ty>/future_t<_Ty>类型 | //这个'函数对象'被调用后,返回generator<_Ty>/future_t<_Ty>类型 | ||||
//然后'函数对象'作为异步执行的上下文状态保存起来 | //然后'函数对象'作为异步执行的上下文状态保存起来 | ||||
template<class _Ctx> | template<class _Ctx> | ||||
struct ctx_task_t : public task_t<typename std::decay<decltype(std::declval<_Ctx>()())>::type> | |||||
struct ctx_task_t : public task_t<typename std::remove_cvref<decltype(std::declval<_Ctx>()())>::type> | |||||
{ | { | ||||
typedef task_t<typename std::decay<decltype(std::declval<_Ctx>()())>::type> base_type; | |||||
typedef task_t<typename std::remove_cvref<decltype(std::declval<_Ctx>()())>::type> base_type; | |||||
using base_type::_future; | using base_type::_future; | ||||
typedef _Ctx context_type; | typedef _Ctx context_type; |
auto tostring_async(_Input_t&& value, _Callable_t&& token) | auto tostring_async(_Input_t&& value, _Callable_t&& token) | ||||
{ | { | ||||
//适配器类型 | //适配器类型 | ||||
using _Adapter_t = modern_callback_adapter_t<std::decay_t<_Callable_t>, void(std::string)>; | |||||
using _Adapter_t = modern_callback_adapter_t<typename std::remove_cvref<_Callable_t>::type, void(std::string)>; | |||||
//通过适配器获得兼容_Signature_t类型的真正的回调,以及返回值_Return_t | //通过适配器获得兼容_Signature_t类型的真正的回调,以及返回值_Return_t | ||||
auto adapter = typename _Adapter_t::traits(std::forward<_Callable_t>(token)); | auto adapter = typename _Adapter_t::traits(std::forward<_Callable_t>(token)); | ||||
//或者宏版本写法 | //或者宏版本写法 | ||||
#define MODERN_CALLBACK_TRAITS(_Token_value, _Signature_t) \ | #define MODERN_CALLBACK_TRAITS(_Token_value, _Signature_t) \ | ||||
using _Adapter_t = modern_callback_adapter_t<std::decay_t<_Callable_t>, _Signature_t>; \ | |||||
using _Adapter_t = modern_callback_adapter_t<typename std::remove_cvref<_Callable_t>::type, _Signature_t>; \ | |||||
auto _Adapter_value = typename _Adapter_t::traits(std::forward<_Callable_t>(_Token_value)) | auto _Adapter_value = typename _Adapter_t::traits(std::forward<_Callable_t>(_Token_value)) | ||||
#define MODERN_CALLBACK_CALL() std::move(std::get<0>(_Adapter_value)) | #define MODERN_CALLBACK_CALL() std::move(std::get<0>(_Adapter_value)) | ||||
#define MODERN_CALLBACK_RETURN() return std::move(std::get<1>(_Adapter_value)).get() | #define MODERN_CALLBACK_RETURN() return std::move(std::get<1>(_Adapter_value)).get() |