[*] Added idioms required by the Aurora engine

This commit is contained in:
Reece Wilson 2021-09-29 13:36:25 +01:00
parent a1fc9ae49a
commit 1a18847363

View File

@ -64,6 +64,50 @@ namespace Aurora::Async
std::function<void(const Info_t &, bool)> onFailure = 0;
};
template<class Info_t = AVoid, class Result_t = AVoid>
static inline FJob<Info_t, Result_t> JobFromConsumer(const AuConsumer<const Result_t &> &onSuccess)
{
FJob<Info_t, Result_t> ret;
ret.onSuccess = [=](const Info_t &in, const Result_t &a)
{
onSuccess(a);
};
return ret;
}
template<class Info_t = AVoid, class Result_t = AVoid>
static inline FJob<Info_t, Result_t> JobFromConsumer(const AuConsumer<const Result_t &> &onSuccess, const AuConsumer<const Result_t &, bool/*neverDispatched*/> &onFailure)
{
FJob<Info_t, Result_t> 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<class Info_t = AVoid, class Result_t = AVoid>
static inline FJob<Info_t, Result_t> JobFromConsumer(const AuConsumer<const Result_t &> &onSuccess, const AuConsumer<const Result_t &> &onFailure)
{
FJob<Info_t, Result_t> 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<AVoid, AVoid>;
template<class Info_t = AVoid, class Result_t = AVoid>
struct CJob
{
@ -78,8 +122,8 @@ namespace Aurora::Async
};
using FVoidTask = FTask<AVoid, AVoid>;
template<class In_t>
template<class In_t = Async::AVoid>
static inline FTask<In_t, AVoid> TaskFromConsumerRefT(const AuConsumer<const In_t &> &func)
{
FTask<In_t, AVoid> ret;
@ -91,6 +135,31 @@ namespace Aurora::Async
return ret;
}
template<class In_t = Async::AVoid, class Out_t = Async::AVoid>
static inline FTask<In_t, AVoid> TaskFromConsumerRefT(const AuSupplierConsumer<AuOptional<Out_t>, const In_t &> &func)
{
FTask<In_t, Out_t> ret;
ret.onFrame = [=](const In_t &a) -> AuOptional<Out_t>
{
return func(a);
};
return ret;
}
static inline FTask<AVoid, AVoid> TaskFromFunctional(const AuVoidFunc &func)
{
FTask<AVoid, AVoid> ret;
ret.onFrame = [=](const AVoid &a) -> AuOptional<AVoid>
{
func();
return AVoid{};
};
return ret;
}
template<class Info_t = AVoid, class Result_t = AVoid>
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<std::remove_pointer_t<decltype(this)>>(this->shared_from_this());
std::function<void()> func = [resultValue_, pin]()
std::function<void()> 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 Info_t = AVoid, typename Result_t = AVoid, typename Task_t = FTask<Info_t, Result_t>, typename Job_t = FJob<Info_t, Result_t>>
static AuSPtr<Async::IWorkItem> DispatchBasicWorkCallback(const DispatchTarget_t &worker, const Task_t &task, const Job_t &job)
{
return Async::NewWorkItem(worker, AuMakeShared<Async::BasicWorkCallback<Info_t, Result_t>>(task, job))->Dispatch();
}
template<typename Info_t = AVoid, typename Result_t = AVoid, typename Task_t = FTask<Info_t, Result_t>, typename Job_t = FJob<Info_t, Result_t>>
static AuSPtr<Async::IWorkItem> DispatchBasicWorkCallback(const DispatchTarget_t &worker, const Task_t &task, const Job_t &job, const Info_t &inputParameters)
{
return Async::NewWorkItem(worker, AuMakeShared<Async::BasicWorkCallback<Info_t, Result_t>>(task, job, inputParameters))->Dispatch();
}
class IAsyncApp
{
public: