]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/generic/callback.cpp
fixed compile errors
[xonotic/netradiant.git] / libs / generic / callback.cpp
1 /*
2 Copyright (C) 2001-2006, William Joseph.
3 All Rights Reserved.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 #include "callback.h"
23
24 #if defined(_DEBUG) || defined(DOXYGEN)
25
26 namespace ExampleMemberCaller
27 {
28   // MemberCaller example
29   class Integer
30   {
31   public:
32     int value;
33
34     void printValue() const
35     {
36       // print this->value here;
37     }
38
39     void setValue()
40     {
41       value = 3;
42     }
43     // a typedef to make things more readable
44     typedef MemberCaller<Integer, &Integer::setValue> SetValueCaller;
45   };
46
47   void example()
48   {
49     Integer foo = { 0 };
50
51     {
52       Callback bar = ConstMemberCaller<Integer, &Integer::printValue>(foo);
53
54       // invoke the callback
55       bar(); // foo.printValue()
56     }
57
58
59     {
60       // use the typedef to improve readability
61       Callback bar = Integer::SetValueCaller(foo);
62
63       // invoke the callback
64       bar(); // foo.setValue()
65     }
66   }
67   // end example
68 }
69
70 namespace ExampleReferenceCaller
71 {
72   // ReferenceCaller example
73   void Int_printValue(const int& value)
74   {
75     // print value here;
76   }
77
78   void Int_setValue(int& value)
79   {
80     value = 3;
81   }
82
83   // a typedef to make things more readable
84   typedef ReferenceCaller<int, Int_setValue> IntSetValueCaller;
85
86   void example()
87   {
88     int foo = 0;
89
90     {
91       Callback bar = ConstReferenceCaller<int, Int_printValue>(foo);
92
93       // invoke the callback
94       bar(); // Int_printValue(foo)
95     }
96
97
98     {
99       // use the typedef to improve readability
100       Callback bar = IntSetValueCaller(foo);
101
102       // invoke the callback
103       bar(); // Int_setValue(foo)
104     }
105   }
106   // end example
107 }
108
109 #endif
110
111 namespace
112 {
113   class A1
114   {
115   };
116   class A2
117   {
118   };
119   class A3
120   {
121   };
122   class A4
123   {
124   };
125
126   class Test
127   {
128   public:
129     void test0()
130     {
131     }
132     typedef Member<Test, void, &Test::test0> Test0;
133     typedef MemberCaller<Test, &Test::test0> Test0Caller;
134     void test0const() const
135     {
136     }
137     typedef ConstMember<Test, void, &Test::test0const> Test0Const;
138     typedef ConstMemberCaller<Test, &Test::test0const> Test0ConstCaller;
139     void test1(A1)
140     {
141     }
142     typedef Member1<Test, A1, void, &Test::test1> Test1;
143     typedef MemberCaller1<Test, A1, &Test::test1> Test1Caller;
144     void test1const(A1) const
145     {
146     }
147     typedef ConstMember1<Test, A1, void, &Test::test1const> Test1Const;
148     typedef ConstMemberCaller1<Test, A1, &Test::test1const> Test1ConstCaller;
149     void test2(A1, A2)
150     {
151     }
152     typedef Member2<Test, A1, A2, void, &Test::test2> Test2;
153     void test2const(A1, A2) const
154     {
155     }
156     typedef ConstMember2<Test, A1, A2, void, &Test::test2const> Test2Const;
157     void test3(A1, A2, A3)
158     {
159     }
160     typedef Member3<Test, A1, A2, A3, void, &Test::test3> Test3;
161     void test3const(A1, A2, A3) const
162     {
163     }
164     typedef ConstMember3<Test, A1, A2, A3, void, &Test::test3const> Test3Const;
165   };
166
167   void test0free()
168   {
169   }
170   typedef FreeCaller<&test0free> Test0FreeCaller;
171   void test1free(A1)
172   {
173   }
174   typedef FreeCaller1<A1, &test1free> Test1FreeCaller;
175   void test2free(A1, A2)
176   {
177   }
178   typedef Function2<A1, A2, void, &test2free> Test2Free;
179   void test3free(A1, A2, A3)
180   {
181   }
182   typedef Function3<A1, A2, A3, void, &test3free> Test3Free;
183
184
185   void test0(Test& test)
186   {
187   }
188   typedef ReferenceCaller<Test, &test0> Test0Caller;
189
190   void test0const(const Test& test)
191   {
192   }
193   typedef ConstReferenceCaller<Test, &test0const> Test0ConstCaller;
194
195   void test0p(Test* test)
196   {
197   }
198   typedef PointerCaller<Test, &test0p> Test0PCaller;
199
200   void test0constp(const Test* test)
201   {
202   }
203   typedef ConstPointerCaller<Test, &test0constp> Test0ConstPCaller;
204
205   void test1(Test& test, A1)
206   {
207   }
208   typedef ReferenceCaller1<Test, A1, &test1> Test1Caller;
209
210   void test1const(const Test& test, A1)
211   {
212   }
213   typedef ConstReferenceCaller1<Test, A1, &test1const> Test1ConstCaller;
214
215   void test1p(Test* test, A1)
216   {
217   }
218   typedef PointerCaller1<Test, A1, &test1p> Test1PCaller;
219
220   void test1constp(const Test* test, A1)
221   {
222   }
223   typedef ConstPointerCaller1<Test, A1, &test1constp> Test1ConstPCaller;
224
225   void test2(Test& test, A1, A2)
226   {
227   }
228   typedef Function3<Test&, A1, A2, void, &test2> Test2;
229
230   void test3(Test& test, A1, A2, A3)
231   {
232   }
233   typedef Function4<Test&, A1, A2, A3, void, &test3> Test3;
234
235   void instantiate()
236   {
237     Test test;
238     const Test& testconst = test;
239     {
240       Callback a = Test0FreeCaller();
241       Callback b = Test::Test0Caller(test);
242       b = makeCallback0(Test::Test0(), test);
243       Callback c = Test::Test0ConstCaller(testconst);
244       c = makeCallback0(Test::Test0Const(), test);
245       Callback d = Test0Caller(test);
246       Callback e = Test0ConstCaller(testconst);
247       Callback f = Test0PCaller(&test);
248       Callback g = Test0ConstPCaller(&testconst);
249       a();
250       bool u = a != b;
251     }
252     {
253       typedef Callback1<A1> TestCallback1;
254       TestCallback1 a = Test1FreeCaller();
255       TestCallback1 b = Test::Test1Caller(test);
256       b = makeCallback1(Test::Test1(), test);
257       TestCallback1 c = Test::Test1ConstCaller(testconst);
258       c = makeCallback1(Test::Test1Const(), test);
259       TestCallback1 d = Test1Caller(test);
260       TestCallback1 e = Test1ConstCaller(testconst);
261       TestCallback1 f = Test1PCaller(&test);
262       TestCallback1 g = Test1ConstPCaller(&testconst);
263       a(A1());
264       bool u = a != b;
265     }
266     {
267       typedef Callback2<A1, A2> TestCallback2;
268       TestCallback2 a = makeStatelessCallback2(Test2Free());
269       TestCallback2 b = makeCallback2(Test2(), test);
270       TestCallback2 c = makeCallback2(Test::Test2(), test);
271       TestCallback2 d = makeCallback2(Test::Test2Const(), test);
272       a(A1(), A2());
273       bool u = a != b;
274     }
275     {
276       typedef Callback3<A1, A2, A3> TestCallback3;
277       TestCallback3 a = makeStatelessCallback3(Test3Free());
278       TestCallback3 b = makeCallback3(Test3(), test);
279       TestCallback3 c = makeCallback3(Test::Test3(), test);
280       TestCallback3 d = makeCallback3(Test::Test3Const(), test);
281       a(A1(), A2(), A3());
282       bool u = a != b;
283     }
284   }
285 }