]> git.lyx.org Git - lyx.git/blob - src/support/InGuiThread.h
add generic helper class for calling functions in gui thread
[lyx.git] / src / support / InGuiThread.h
1 // -*- C++ -*-
2 /**
3  * \file InGuiThread.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Peter Kümmel
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef INGUITHREAD_H
13 #define INGUITHREAD_H
14
15 #include <QObject>
16
17 #include "support/bind.h"
18 #include "support/functional.h"
19
20 namespace lyx {
21 namespace frontend {
22
23
24 class IntoGuiThreadMover : public QObject
25 {
26         Q_OBJECT
27
28 protected:
29
30         IntoGuiThreadMover();
31
32         void callInGuiThread();
33
34 Q_SIGNALS:
35         void triggerCall();
36         void called();
37
38 private Q_SLOTS:
39         void doFunctionCall();
40
41 private:
42         virtual void synchronousFunctionCall() = 0;
43 };
44
45
46 template<class R>
47 class InGuiThread : private IntoGuiThreadMover
48 {
49 public:
50
51         InGuiThread() {}
52
53         template<class F>
54         R call(F f)
55         {
56                 func_ = f;
57                 callInGuiThread();
58                 return return_value_;
59         }
60
61         template<class F, class P1>
62         R call(F f, P1 p1)
63         {
64                 return call(bind(f, p1));
65         }
66
67         template<class F, class P1, class P2>
68         R call(F f, P1 p1, P2 p2)
69         {
70                 return call(bind(f, p1, p2));
71         }
72
73         template<class F, class P1, class P2, class P3>
74         R call(F f, P1 p1, P2 p2, P3 p3)
75         {
76                 return call(bind(f, p1, p2, p3));
77         }
78
79         template<class F, class P1, class P2, class P3, class P4>
80         R call(F f, P1 p1, P2 p2, P3 p3, P4 p4)
81         {
82                 return call(bind(f, p1, p2, p3, p4));
83         }
84
85         /*
86           ...
87         */
88
89         template<class F, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8>
90         R call(F f, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8)
91         {
92                 return call(bind(f, p1, p2, p3, p4, p5, p6, p7, p8));
93         }
94
95 private:
96
97         void synchronousFunctionCall()
98         {
99                 return_value_ = func_();
100         }
101
102 private:
103         R return_value_;
104         function<R()> func_;
105 };
106
107
108 // void specialisation
109 template<>
110 class InGuiThread<void> : private IntoGuiThreadMover
111 {
112 public:
113
114         InGuiThread() {}
115
116         template<class F>
117         void call(F f)
118         {
119                 func_ = f;
120                 callInGuiThread();
121         }
122
123         template<class F, class P1>
124         void call(F f, P1 p1)
125         {
126                 call(bind(f, p1));
127         }
128
129         template<class F, class P1, class P2>
130         void call(F f, P1 p1, P2 p2)
131         {
132                 call(bind(f, p1, p2));
133         }
134
135         template<class F, class P1, class P2, class P3>
136         void call(F f, P1 p1, P2 p2, P3 p3)
137         {
138                 call(bind(f, p1, p2, p3));
139         }
140
141         template<class F, class P1, class P2, class P3, class P4>
142         void call(F f, P1 p1, P2 p2, P3 p3, P4 p4)
143         {
144                 call(bind(f, p1, p2, p3, p4));
145         }
146
147         /*
148           ...
149         */
150
151         template<class F, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8>
152         void call(F f, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8)
153         {
154                 call(bind(f, p1, p2, p3, p4, p5, p6, p7, p8));
155         }
156
157 private:
158
159         void synchronousFunctionCall()
160         {
161                 func_();
162         }
163
164 private:
165         function<void()> func_;
166 };
167
168
169 } // namespace frontend
170 } // namespace lyx
171
172 #endif // GUIABOUT_H