]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/generic/callback.cpp
423efcea500e12a9383e4876fb8152654314d650
[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 #include "globaldefs.h"
24
25 #if GDEF_DEBUG || defined( DOXYGEN )
26
27 namespace ExampleMemberCaller
28 {
29 // MemberCaller example
30 class Integer
31 {
32 public:
33 int value;
34
35 void printValue() const {
36         // print this->value here;
37 }
38
39 void setValue(){
40         value = 3;
41 }
42 // a typedef to make things more readable
43 typedef MemberCaller<Integer, void(), &Integer::setValue> SetValueCaller;
44 };
45
46 void example(){
47         Integer foo = { 0 };
48
49         {
50                 Callback<void()> bar = ConstMemberCaller<Integer, void(), &Integer::printValue>( foo );
51
52                 // invoke the callback
53                 bar(); // foo.printValue()
54         }
55
56
57         {
58                 // use the typedef to improve readability
59                 Callback<void()> bar = Integer::SetValueCaller( foo );
60
61                 // invoke the callback
62                 bar(); // foo.setValue()
63         }
64 }
65 // end example
66 }
67
68 namespace ExampleReferenceCaller
69 {
70 // ReferenceCaller example
71 void Int_printValue( const int& value ){
72         // print value here;
73 }
74
75 void Int_setValue( int& value ){
76         value = 3;
77 }
78
79 // a typedef to make things more readable
80 typedef ReferenceCaller<int, void(), Int_setValue> IntSetValueCaller;
81
82 void example(){
83         int foo = 0;
84
85         {
86                 Callback<void()> bar = ConstReferenceCaller<int, void(), Int_printValue>( foo );
87
88                 // invoke the callback
89                 bar(); // Int_printValue(foo)
90         }
91
92
93         {
94                 // use the typedef to improve readability
95                 Callback<void()> bar = IntSetValueCaller( foo );
96
97                 // invoke the callback
98                 bar(); // Int_setValue(foo)
99         }
100 }
101 // end example
102 }
103
104 #endif
105
106 namespace
107 {
108 class A1
109 {
110 };
111 class A2
112 {
113 };
114 class A3
115 {
116 };
117 class A4
118 {
119 };
120
121 class Test
122 {
123 public:
124 void test0(){
125 }
126 typedef Member<Test, void(), &Test::test0> Test0;
127 typedef MemberCaller<Test, void(), &Test::test0> Test0Caller;
128 void test0const() const {
129 }
130 typedef ConstMember<Test, void(), &Test::test0const> Test0Const;
131 typedef ConstMemberCaller<Test, void(), &Test::test0const> Test0ConstCaller;
132 void test1( A1 ){
133 }
134 typedef Member<Test, void(A1), &Test::test1> Test1;
135 typedef MemberCaller<Test, void(A1), &Test::test1> Test1Caller;
136 void test1const( A1 ) const {
137 }
138 typedef ConstMember<Test, void(A1), &Test::test1const> Test1Const;
139 typedef ConstMemberCaller<Test, void(A1), &Test::test1const> Test1ConstCaller;
140 void test2( A1, A2 ){
141 }
142 typedef Member<Test, void(A1, A2), &Test::test2> Test2;
143 void test2const( A1, A2 ) const {
144 }
145 typedef ConstMember<Test, void(A1, A2), &Test::test2const> Test2Const;
146 void test3( A1, A2, A3 ){
147 }
148 typedef Member<Test, void(A1, A2, A3), &Test::test3> Test3;
149 void test3const( A1, A2, A3 ) const {
150 }
151 typedef ConstMember<Test, void(A1, A2, A3), &Test::test3const> Test3Const;
152 };
153
154 void test0free(){
155 }
156 void test1free( A1 ){
157 }
158 void test2free( A1, A2 ){
159 }
160 typedef Function<void(A1, A2), &test2free> Test2Free;
161 void test3free( A1, A2, A3 ){
162 }
163 typedef Function<void(A1, A2, A3), &test3free> Test3Free;
164
165
166 void test0( Test& test ){
167 }
168 typedef ReferenceCaller<Test, void(), &test0> Test0Caller;
169
170 void test0const( const Test& test ){
171 }
172 typedef ConstReferenceCaller<Test, void(), &test0const> Test0ConstCaller;
173
174 void test0p( Test* test ){
175 }
176 typedef PointerCaller<Test, void(), &test0p> Test0PCaller;
177
178 void test0constp( const Test* test ){
179 }
180 typedef ConstPointerCaller<Test, void(), &test0constp> Test0ConstPCaller;
181
182 void test1( Test& test, A1 ){
183 }
184 typedef ReferenceCaller<Test, void(A1), &test1> Test1Caller;
185
186 void test1const( const Test& test, A1 ){
187 }
188 typedef ConstReferenceCaller<Test, void(A1), &test1const> Test1ConstCaller;
189
190 void test1p( Test* test, A1 ){
191 }
192 typedef PointerCaller<Test, void(A1), &test1p> Test1PCaller;
193
194 void test1constp( const Test* test, A1 ){
195 }
196 typedef ConstPointerCaller<Test, void(A1), &test1constp> Test1ConstPCaller;
197
198 void test2( Test& test, A1, A2 ){
199 }
200 typedef Function<void(Test&, A1, A2), &test2> Test2;
201
202 void test3( Test& test, A1, A2, A3 ){
203 }
204 typedef Function<void(Test&, A1, A2, A3), &test3> Test3;
205
206 void instantiate(){
207         Test test;
208         const Test& testconst = test;
209         {
210                 Callback<void()> a = makeCallbackF(&test0free)();
211                 Callback<void()> b = Test::Test0Caller( test );
212                 b = makeCallback( Test::Test0(), test );
213                 Callback<void()> c = Test::Test0ConstCaller( testconst );
214                 c = makeCallback( Test::Test0Const(), test );
215                 Test0Caller{ test };
216                 Test0ConstCaller{ testconst };
217                 Test0PCaller{ &test };
218                 Test0ConstPCaller{ &testconst };
219                 a();
220                 bool u = a != b;
221         }
222         {
223                 typedef Callback<void(A1)> TestCallback1;
224                 TestCallback1 a = makeCallbackF(&test1free));
225                 TestCallback1 b = Test::Test1Caller( test );
226                 b = makeCallback( Test::Test1(), test );
227                 TestCallback1 c = Test::Test1ConstCaller( testconst );
228                 c = makeCallback( Test::Test1Const(), test );
229                 Test1Caller{ test };
230                 Test1ConstCaller{ testconst };
231                 Test1PCaller{ &test };
232                 Test1ConstPCaller{ &testconst };
233                 a( A1() );
234                 bool u = a != b;
235         }
236         {
237                 typedef Callback<void(A1, A2)> TestCallback2;
238                 TestCallback2 a = makeStatelessCallback( Test2Free() );
239                 TestCallback2 b = makeCallback( Test2(), test );
240                 makeCallback( Test::Test2(), test );
241                 makeCallback( Test::Test2Const(), test );
242                 a( A1(), A2() );
243                 bool u = a != b;
244         }
245         {
246                 typedef Callback<void(A1, A2, A3)> TestCallback3;
247                 TestCallback3 a = makeStatelessCallback( Test3Free() );
248                 TestCallback3 b = makeCallback( Test3(), test );
249                 makeCallback( Test::Test3(), test );
250                 makeCallback( Test::Test3Const(), test );
251                 a( A1(), A2(), A3() );
252                 bool u = a != b;
253         }
254 }
255 }