1 #if !defined(BTK_ASYNC_HPP_) 5 #include "../signal/signal.hpp" 8 BTKAPI
void DeferCall(
void(* fn)(
void*),
void *data);
10 template<
bool has_signal,
class RetT>
42 if constexpr(not std::is_destructible<RetT>()){
48 new (&data.data)T(std::forward<T>(t));
56 std::remove_reference_t<RetT> data;
84 template<
bool HasSignal,
class Callable,
class ...Args>
86 public AsyncSignal<HasSignal,std::invoke_result_t<Callable,Args...>>,
91 static constexpr
int has_signal = HasSignal;
98 std::tuple<Args...>(std::forward<Args>(args)...){};
106 template<
bool HasSiganlArg,
class T>
113 bool need_cleanup =
true;
114 bool need_delete =
true;
133 need_cleanup =
false;
145 bool need_delete =
true;
178 template<
bool HasSignal,
class Callable,
class ...Args>
189 AsyncBase<HasSignal,Callable,Args...>(std::forward<Args>(_args)...),
190 callable(std::forward<Callable>(_callable)){
202 static constexpr
bool signal_has_args = not std::is_same<result_type,void>();
209 return std::apply(callable,
210 static_cast<std::tuple<Args...
>&&>(*
this));
217 if constexpr(not HasSignal){
219 std::unique_ptr<AsyncInvoker> ptr(
this);
222 else if constexpr(std::is_same<result_type,void>()){
225 if(not this->signal.empty()){
236 guard.need_cleanup =
false;
237 this->store(invoke());
239 guard.need_cleanup =
true;
240 if(not this->signal.empty()){
251 template<
class T = std::enable_if<HasSignal>>
253 if constexpr(std::is_same<result_type,void>()){
254 std::unique_ptr<AsyncInvoker> ptr(
this);
261 if constexpr(std::is_reference<result_type>()){
264 this->signal.emit(*(this->data.data));
267 this->signal.emit(this->data.data);
276 static void Run(
void *__self){
284 template<
class T = std::enable_if<HasSignal>>
295 BTKAPI
void RtLauch(
void *invoker,
void(*
run)(
void*invoker));
296 BTKAPI
void DeferLauch(
void *invoker,
void(*
run)(
void*invoker));
318 template<
bool HasSignal,
class Callable,
class ...Args>
321 using result_type =
typename std::invoke_result_t<Callable,Args...>;
325 AsyncTask(Callable &&callable,Args&& ...args){
326 invoker =
new invoker_type(
327 std::forward<Callable>(callable),
328 std::forward<Args>(args)...
332 invoker = task.invoker;
333 task.invoker =
nullptr;
344 void lauch(Lauch lauch = Lauch::Async){
345 if(invoker !=
nullptr){
346 if(lauch == Lauch::Async){
347 Impl::RtLauch(invoker,invoker_type::Run);
350 Impl::DeferLauch(invoker,invoker_type::Run);
360 signal_type *operator ->() const noexcept{
361 if constexpr(HasSignal){
362 return &(invoker->signal);
369 invoker_type *invoker;
380 template<
class T,
class ...Args>
383 std::forward<T>(callable),
384 std::forward<Args>(args)...
397 template<
class T,
class ...Args>
400 std::forward<T>(callable),
401 std::forward<Args>(args)...
409 #endif // BTK_ASYNC_HPP_ result_type invoke()
Invoke the callable.
Definition: async.hpp:208
AsyncTask< true, T, Args... > Async(T &&callable, Args ...args)
Create a Async Task.
Definition: async.hpp:381
AsyncBase(Args &&...args)
Construct a new Async Base object.
Definition: async.hpp:97
This header include many useful containers.
Definition: async.hpp:7
void release()
Relase both.
Definition: async.hpp:132
BTKAPI void DeferCall(void(*fn)(void *), void *data)
This function will be called in main EventLoop.
Definition: core.cpp:421
void emit()
Emit the signal impl.
Definition: async.hpp:252
std::invoke_result_t< Callable, Args... > result_type
The result type.
Definition: async.hpp:197
The AsyncInvoker.
Definition: async.hpp:179
A RAII class for impl Invoker.
Definition: async.hpp:107
A type to tell the async don't emit the signal in main thread.
Definition: async.hpp:307
Callable callable
The signal.
Definition: async.hpp:185
Definition: async.hpp:161
The AsyncTask.
Definition: async.hpp:319
void run()
Run the invoker.
Definition: async.hpp:216
static void Run(void *__self)
The main entry for invoker.
Definition: async.hpp:276
BTKAPI int run()
Enter the EventLoop.
Definition: core.cpp:58
The async base,It store the return value and the signal.
Definition: async.hpp:85
static void EmitSignal(void *__self)
Emit Signal in main thread.
Definition: async.hpp:285
void lauch(Lauch lauch=Lauch::Async)
Lauch the asyn task.
Definition: async.hpp:344