diff --git a/Include/Aurora/Async/Async.hpp b/Include/Aurora/Async/Async.hpp index c7a26af0..599ba3a4 100644 --- a/Include/Aurora/Async/Async.hpp +++ b/Include/Aurora/Async/Async.hpp @@ -64,6 +64,50 @@ namespace Aurora::Async std::function onFailure = 0; }; + template + static inline FJob JobFromConsumer(const AuConsumer &onSuccess) + { + FJob ret; + ret.onSuccess = [=](const Info_t &in, const Result_t &a) + { + onSuccess(a); + }; + return ret; + } + + template + static inline FJob JobFromConsumer(const AuConsumer &onSuccess, const AuConsumer &onFailure) + { + FJob ret; + ret.onSuccess = [=](const Info_t &in, const Result_t &a) + { + onSuccess(a); + }; + ret.onFailure = [=](const Result_t &a, bool neverDispatched) + { + onFailure(a, neverDispatched); + }; + return ret; + } + + template + static inline FJob JobFromConsumer(const AuConsumer &onSuccess, const AuConsumer &onFailure) + { + FJob ret; + ret.onSuccess = [=](const Info_t &in, const Result_t &a) + { + onSuccess(a); + }; + ret.onFailure = [=](const Result_t &a, bool neverDispatched) + { + onFailure(a); + }; + return ret; + } + + using FVoidJob = FJob; + + template struct CJob { @@ -78,8 +122,8 @@ namespace Aurora::Async }; using FVoidTask = FTask; - - template + + template static inline FTask TaskFromConsumerRefT(const AuConsumer &func) { FTask ret; @@ -91,6 +135,31 @@ namespace Aurora::Async return ret; } + + template + static inline FTask TaskFromConsumerRefT(const AuSupplierConsumer, const In_t &> &func) + { + FTask ret; + ret.onFrame = [=](const In_t &a) -> AuOptional + { + return func(a); + }; + return ret; + } + + static inline FTask TaskFromFunctional(const AuVoidFunc &func) + { + FTask ret; + ret.onFrame = [=](const AVoid &a) -> AuOptional + { + func(); + return AVoid{}; + }; + return ret; + } + + + template struct CTask { @@ -192,11 +261,41 @@ namespace Aurora::Async { caller = GetAsyncApp()->GetCurrentThread(); } - + BasicWorkCallback(Task_t &&task) : task(std::move(task)) { caller = GetAsyncApp()->GetCurrentThread(); } + + BasicWorkCallback(Task_t &&task, Job_t &&callback) : task(std::move(task)), callback(std::move(callback)) + { + caller = GetAsyncApp()->GetCurrentThread(); + } + + BasicWorkCallback(const Task_t &task) : task(task) + { + caller = GetAsyncApp()->GetCurrentThread(); + } + + BasicWorkCallback(const Task_t &task, const Job_t &callback) : task(task), callback(callback) + { + caller = GetAsyncApp()->GetCurrentThread(); + } + + BasicWorkCallback(const Task_t &task, const Job_t &callback, const Info_t &info) : task(task), callback(callback), input(info) + { + caller = GetAsyncApp()->GetCurrentThread(); + } + + BasicWorkCallback(Task_t &&task, Job_t &&callback, Info_t &&info) : task(std::move(task)), callback(std::move(callback)), input(std::move(info)) + { + caller = GetAsyncApp()->GetCurrentThread(); + } + + BasicWorkCallback(const Task_t &task, const Job_t &callback, Info_t &&info) : task(task), callback(callback), input(std::move(info)) + { + caller = GetAsyncApp()->GetCurrentThread(); + } Info_t input; Task_t task; @@ -249,20 +348,20 @@ namespace Aurora::Async auto pin = std::static_pointer_cast>(this->shared_from_this()); - std::function func = [resultValue_, pin]() + std::function func = [pin]() { try { - if (resultValue_.has_value()) + if (pin->resultValue_.has_value()) { - pin->secretContext_.opt = resultValue_.value(); + pin->secretContext_.opt = &pin->resultValue_.value(); if constexpr (IsCallbackPtr) { - pin->callback->onSuccess(pin->input, *resultValue_); + pin->callback->onSuccess(pin->input, *pin->resultValue_); } else { - pin->callback.onSuccess(pin->input, *resultValue_); + pin->callback.onSuccess(pin->input, *pin->resultValue_); } } else @@ -418,6 +517,19 @@ namespace Aurora::Async #endif + template, typename Job_t = FJob> + static AuSPtr DispatchBasicWorkCallback(const DispatchTarget_t &worker, const Task_t &task, const Job_t &job) + { + return Async::NewWorkItem(worker, AuMakeShared>(task, job))->Dispatch(); + } + + template, typename Job_t = FJob> + static AuSPtr DispatchBasicWorkCallback(const DispatchTarget_t &worker, const Task_t &task, const Job_t &job, const Info_t &inputParameters) + { + return Async::NewWorkItem(worker, AuMakeShared>(task, job, inputParameters))->Dispatch(); + } + + class IAsyncApp { public: