libfilezilla
invoker.hpp
Go to the documentation of this file.
1 #ifndef LIBFILEZILLA_INVOKER_HEADER
2 #define LIBFILEZILLA_INVOKER_HEADER
3 
8 #include "event_handler.hpp"
9 
10 namespace fz {
11 
13 struct invoker_event_type{};
14 
16 typedef simple_event<invoker_event_type, std::function<void()>> invoker_event;
17 
19 class FZ_PUBLIC_SYMBOL thread_invoker final : public event_handler
20 {
21 public:
22  thread_invoker(event_loop& loop);
23  virtual ~thread_invoker();
24 
25  virtual void operator()(event_base const& ev) override;
26 };
27 
35 template<typename F>
36 auto make_invoker(event_loop& loop, F && f)
37 {
38  return [handler = thread_invoker(loop), cf = std::forward<F>(f)](auto &&... args) mutable -> decltype(f(std::forward<decltype(args)>(args)...), void())
39  {
40  auto cb = [cf = std::move(cf), targs = std::make_tuple(std::forward<decltype(args)>(args)...)] {
41  std::apply(cf, targs);
42  };
43  handler.send_event<invoker_event>(std::move(cb));
44  };
45 }
46 
47 template<typename F>
48 auto make_invoker(event_handler& h, F && f)
49 {
50  return make_invoker(h.event_loop_, std::forward<F>(f));
51 }
52 
53 
54 typedef std::function<void(std::function<void()>)> invoker_factory;
55 
62 invoker_factory FZ_PUBLIC_SYMBOL get_invoker_factory(event_loop& loop);
63 
71 template<typename F>
72 auto make_invoker(invoker_factory const& inv, F && f)
73 {
74  return [inv, cf = std::forward<F>(f)](auto &&... args) mutable -> decltype(f(std::forward<decltype(args)>(args)...), void())
75  {
76  auto cb = [cf = std::move(cf), targs = std::make_tuple(std::forward<decltype(args)>(args)...)] {
77  std::apply(cf, targs);
78  };
79  inv(cb);
80  };
81 }
82 
83 }
84 
85 #endif
A threaded event loop that supports sending events and timers.
Definition: event_loop.hpp:34
This is the recommended event class.
Definition: event.hpp:66
Declares the event_handler class.
The namespace used by libfilezilla.
Definition: apply.hpp:17
auto apply(Obj &&obj, F &&f, Tuple &&args) -> decltype(apply_(std::forward< Obj >(obj), std::forward< F >(f), std::forward< Tuple >(args), Seq()))
Apply tuple to pointer to member.
Definition: apply.hpp:48
invoker_factory get_invoker_factory(event_loop &loop)
Creates an invoker factory.
auto make_invoker(event_loop &loop, F &&f)
Wraps the passed function, so that it is always invoked in the context of the loop.
Definition: invoker.hpp:36