guit  0.1
 All Classes Functions Variables Typedefs Enumerations Friends
gcall.hpp
1 //
2 // Callback functions
3 // Guit GUI Toolkit
4 // Copyright © 2020 Eric Lecolinet. All rights reserved.
5 // http://www.telecom-paris.fr/~elc
6 //
7 
8 #ifndef Guit_Call_hpp
9 #define Guit_Call_hpp
10 #include <thread>
11 #include <gtypes.hpp>
12 namespace guit {
13 
14 
15 class GCaller {
16 public:
17  virtual ~GCaller() = default;
18  virtual void operator()(GEvent&) = 0;
19  GCaller() = default;
20  GCaller(GCaller const&) = delete;
21  GCaller(GObject const&&) = delete;
22  GCaller& operator=(GCaller const&) = delete;
23  GCaller& operator=(GCaller const&&) = delete;
25  uint16_t refcount_{};
26 };
27 
28 
29 class GCallerF0 : public GCaller {
30 public:
31  GCallerF0(GFunction const& fun) : fun_(fun) {}
32  void operator()(GEvent&) override {fun_();}
33  GFunction fun_;
34 };
35 
36 class GCallerF1 : public GCaller {
37 public:
38  GCallerF1(GEventFunction const& fun) : fun_(fun) {}
39  void operator()(GEvent& e) override {fun_(e);}
40  GEventFunction fun_;
41 };
42 
43 template <class T>
44 class GCallerM0 : public GCaller {
45 public:
46  GCallerM0(T* obj, void (T::*fun)()) : obj_(obj), fun_(fun) {}
47  void operator()(GEvent&) override {(obj_->*fun_)();}
48  T* obj_;
49  void (T::*fun_)();
50 };
51 
52 template <class T>
53 class GCallerM1 : public GCaller {
54 public:
55  GCallerM1(T* obj, void (T::*fun)(GEvent&)) : obj_(obj), fun_(fun) {}
56  void operator()(GEvent& e) override {(obj_->*fun_)(e);}
57  T* obj_;
58  void (T::*fun_)(GEvent&);
59 };
60 
61 
62 template <class T, class E>
63 class GCallerM2 : public GCaller {
64 public:
65  GCallerM2(T* obj, void (T::*fun)(E&)) : obj_(obj), fun_(fun) {}
66  void operator()(GEvent& e) override {
67  if (auto* ee = dynamic_cast<E*>(&e)) (obj_->*fun_)(*ee);
68  else gerror("GCaller","Invalid event type");
69  }
70  T* obj_;
71  void (T::*fun_)(E&);
72 };
73 
74 
75 inline GCaller& Call(GFunction const& fun) {
76  return *new GCallerF0(fun);
77 }
78 
79 inline GCaller& Call(GEventFunction const& fun) {
80  return *new GCallerF1(fun);
81 }
82 
83 template <class T>
84 inline GCaller& Call(T* obj, void (T::*fun)()) {
85  return *new GCallerM0<T>(obj, fun);
86 }
87 
88 template <class T>
89 inline GCaller& Call(T* obj, void (T::*fun)(GEvent& e)) {
90  return *new GCallerM1<T>(obj, fun);
91 }
92 
93 template <class T, class E>
94 inline GCaller& Call(T* obj, void (T::*fun)(E& e)) {
95  return *new GCallerM2<T,E>(obj, fun);
96 }
97 
98 
99 
100 class GCallerThread : public GCaller {
101 public:
102  std::thread thread_;
103 };
104 
105 class GCallerThreadF0 : public GCallerThread {
106 public:
107  GCallerThreadF0(GFunction const& fun) : fun_(fun) {}
108  void operator()(GEvent&) override {fun_();}
109  GFunction fun_;
110 };
111 
112 template <class T>
113 class GCallerThreadM0 : public GCallerThread {
114 public:
115  GCallerThreadM0(T* obj, void (T::*fun)()) : obj_(obj), fun_(fun) {}
116  void operator()(GEvent&) override {(obj_->*fun_)();}
117  T* obj_;
118  void (T::*fun_)();
119 };
120 
121 
134 inline GCallerThread& CallThread(GFunction const& fun) {
135  return *new GCallerThreadF0(fun);
136 }
137 
138 template <class T>
139 inline GCallerThread& CallThread(T* obj, void (T::*fun)()) {
140  return *new GCallerThreadM0<T>(obj, fun);
141 }
143 
147 inline GCallerThread& CallWait(GTime ms_delay) {
148  return *new GCallerThreadF0([ms_delay]{gsleep(ms_delay);});
149 }
150 
151 }
152 #endif