[*] Update async header

This commit is contained in:
Reece Wilson 2021-09-29 09:02:27 +01:00
parent 097f805209
commit 1360e5627d

View File

@ -70,6 +70,8 @@ namespace Aurora::Async
std::function<AuOptional<Result_t>(const Info_t &)> onFrame = 0;
};
using FVoidTask = FTask<AVoid, AVoid>;
template<class Info_t = AVoid, class Result_t = AVoid>
struct CTask
{
@ -93,6 +95,7 @@ namespace Aurora::Async
virtual bool BlockUntilComplete() = 0;
virtual bool HasFinished() = 0;
virtual bool HasFailed() = 0;
virtual void Cancel() = 0;
};
AUKN_SYM AuSPtr<IWorkItem> NewWorkItem(const DispatchTarget_t &worker, const AuSPtr<IWorkItemHandler> &task, bool supportsBlocking = false);
@ -154,6 +157,11 @@ namespace Aurora::Async
caller = GetAsyncApp()->GetCurrentThread();
}
BasicWorkCallback(Task_t &&task) : task(std::move(task))
{
caller = GetAsyncApp()->GetCurrentThread();
}
Task_t task;
Job_t callback;
@ -309,28 +317,38 @@ namespace Aurora::Async
}
};
template<typename... Args>
static std::function<void(Args...)> TranslateAsyncFunctionToDispatcher(std::function<void(Args...)> func)
#define ASYNC_ERROR(exp) if constexpr (std::is_same_v<T, bool>) { SysPushErrorGen(exp); return {}; } else { throw std::exception(exp); }
#define ASYNC_FINISH if constexpr (std::is_same_v<T, bool>) { return true; }
template<typename T = void, typename... Args, AU_TEMPLATE_ENABLE_WHEN(std::is_same_v<T, bool> || std::is_void<T>::value)>
static std::function<T(Args...)> TranslateAsyncFunctionToDispatcherWithThread(WorkerId_t id, std::function<void(Args...)> func)
{
auto cur = GetAsyncApp()->GetCurrentThread();
return [=](Args... in) -> void
return [=](Args... in) -> T
{
auto work = AuMakeShared<BasicWorkStdFunc>([=]() -> void {
func(in...);
});
auto workItem = NewWorkItem(cur, work);
if (!workItem) throw "can't dispatch async call; out of memory";
if (!work) ASYNC_ERROR("can't dispatch async call; out of memory");
auto workItem = NewWorkItem(id, work);
if (!workItem) ASYNC_ERROR("can't dispatch async call; out of memory");
workItem->Dispatch();
ASYNC_FINISH;
};
}
template<typename B, typename... Args>
static std::function<void(std::function<void(const B&)>, Args...)> TranslateAsyncReturnableFunctionToDispatcher(std::function<B(Args...)> func)
template<typename T = void, typename... Args, AU_TEMPLATE_ENABLE_WHEN(std::is_same_v<T, bool> || std::is_void<T>::value)>
static std::function<T(Args...)> TranslateAsyncFunctionToDispatcher(std::function<void(Args...)> func)
{
auto cur = GetAsyncApp()->GetCurrentThread();
return [=](std::function<void(const B&)> callback, Args... in) -> void
return TranslateAsyncFunctionToDispatcherWithThread<T, Args>(GetAsyncApp()->GetCurrentThread(), func);
}
template<typename B = void, typename T, typename... Args, AU_TEMPLATE_ENABLE_WHEN(std::is_same_v<T, bool> || std::is_void<T>::value)>
static std::function<T(std::function<void(const B&)>, Args...)> TranslateAsyncReturnableFunctionToDispatcherWithThread(WorkerId_t id, std::function<AuOptional<B>(Args...)> func)
{
return [=](std::function<T(const B&)> callback, Args... in) -> T
{
auto work = AuMakeShared<BasicWorkCallback<AVoid, B>>();
if (!work) ASYNC_ERROR("can't dispatch async call; out of memory");
work.task.onProcess = [](const AVoid &) -> AuOptional<B>
{
return func(in...);
@ -339,11 +357,21 @@ namespace Aurora::Async
{
callback(ret);
};
auto workItem = NewWorkItem(cur, work);
if (!workItem) throw "can't dispatch async call; out of memory";
auto workItem = NewWorkItem(id, work);
if (!workItem) ASYNC_ERROR("can't dispatch async call; out of memory");
workItem->Dispatch();
ASYNC_FINISH;
};
}
template<typename B = void, typename T, typename... Args, AU_TEMPLATE_ENABLE_WHEN(std::is_same_v<T, bool> || std::is_void<T>::value)>
static std::function<T(std::function<void(const B&)>, Args...)> TranslateAsyncReturnableFunctionToDispatcher(std::function<AuOptional<B>(Args...)> func)
{
return TranslateAsyncReturnableFunctionToDispatcherWithThread<B, T, Args>(GetAsyncApp()->GetCurrentThread(), func);
}
#undef ASYNC_ERROR
#undef ASYNC_FINISH
#endif