Add the possibility to have async::Promises that return multiple parameters
This commit is contained in:
parent
8cb3b5b6b3
commit
a1d0ce3f6f
2 changed files with 16 additions and 16 deletions
|
@ -24,8 +24,8 @@
|
|||
|
||||
#include "thirdparty/deto_async/async/promise.h"
|
||||
namespace mu::async {
|
||||
template<typename T>
|
||||
using Promise = deto::async::Promise<T>;
|
||||
template<typename ... T>
|
||||
using Promise = deto::async::Promise<T...>;
|
||||
}
|
||||
|
||||
#endif // MU_ASYNC_PROMISE_H
|
||||
|
|
28
thirdparty/deto_async/async/promise.h
vendored
28
thirdparty/deto_async/async/promise.h
vendored
|
@ -8,33 +8,33 @@
|
|||
|
||||
namespace deto {
|
||||
namespace async {
|
||||
template<typename T>
|
||||
template<typename ... T>
|
||||
class Promise;
|
||||
template<typename T>
|
||||
template<typename ... T>
|
||||
class Promise
|
||||
{
|
||||
public:
|
||||
|
||||
struct Resolve
|
||||
{
|
||||
Resolve(Promise<T> _p)
|
||||
Resolve(Promise<T...> _p)
|
||||
: p(_p) {}
|
||||
|
||||
void operator ()(const T& val) const { p.resolve(val); }
|
||||
void operator ()(const T& ... val) const { p.resolve(val ...); }
|
||||
|
||||
private:
|
||||
mutable Promise<T> p;
|
||||
mutable Promise<T...> p;
|
||||
};
|
||||
|
||||
struct Reject
|
||||
{
|
||||
Reject(Promise<T> _p)
|
||||
Reject(Promise<T...> _p)
|
||||
: p(_p) {}
|
||||
|
||||
void operator ()(int code, const std::string& msg) const { p.reject(code, msg); }
|
||||
|
||||
private:
|
||||
mutable Promise<T> p;
|
||||
mutable Promise<T...> p;
|
||||
};
|
||||
|
||||
template<typename Exec>
|
||||
|
@ -75,14 +75,14 @@ public:
|
|||
}
|
||||
|
||||
template<typename Call>
|
||||
Promise<T>& onResolve(const Asyncable* caller, Call f)
|
||||
Promise<T...>& onResolve(const Asyncable* caller, Call f)
|
||||
{
|
||||
ptr()->addCallBack(OnResolve, const_cast<Asyncable*>(caller), new ResolveCall<Call, T>(f));
|
||||
ptr()->addCallBack(OnResolve, const_cast<Asyncable*>(caller), new ResolveCall<Call, T...>(f));
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename Call>
|
||||
Promise<T>& onReject(const Asyncable* caller, Call f)
|
||||
Promise<T...>& onReject(const Asyncable* caller, Call f)
|
||||
{
|
||||
ptr()->addCallBack(OnReject, const_cast<Asyncable*>(caller), new RejectCall<Call>(f));
|
||||
return *this;
|
||||
|
@ -90,10 +90,10 @@ public:
|
|||
|
||||
private:
|
||||
|
||||
void resolve(const T& d)
|
||||
void resolve(const T& ... d)
|
||||
{
|
||||
NotifyData nd;
|
||||
nd.setArg<T>(0, d);
|
||||
nd.setArg<T...>(0, d ...);
|
||||
nd.setArg<std::shared_ptr<PromiseInvoker> >(1, ptr());
|
||||
ptr()->invoke(OnResolve, nd);
|
||||
}
|
||||
|
@ -118,12 +118,12 @@ private:
|
|||
virtual void resolved(const NotifyData& e) = 0;
|
||||
};
|
||||
|
||||
template<typename Call, typename Arg>
|
||||
template<typename Call, typename ... Arg>
|
||||
struct ResolveCall : public IResolve {
|
||||
Call f;
|
||||
ResolveCall(Call _f)
|
||||
: f(_f) {}
|
||||
void resolved(const NotifyData& e) { f(std::get<0>(e.arg<Arg>())); }
|
||||
void resolved(const NotifyData& e) { std::apply(f, e.arg<Arg...>()); }
|
||||
};
|
||||
|
||||
struct IReject {
|
||||
|
|
Loading…
Reference in a new issue