]> git.xonotic.org Git - xonotic/netradiant.git/blob - libs/generic/callback.cpp
Callback: remove fixed-arity wrappers
[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 typedef FreeCaller<void(), &test0free> Test0FreeCaller;
157 void test1free( A1 ){
158 }
159 typedef FreeCaller<void(A1), &test1free> Test1FreeCaller;
160 void test2free( A1, A2 ){
161 }
162 typedef Function<void(A1, A2), &test2free> Test2Free;
163 void test3free( A1, A2, A3 ){
164 }
165 typedef Function<void(A1, A2, A3), &test3free> Test3Free;
166
167
168 void test0( Test& test ){
169 }
170 typedef ReferenceCaller<Test, void(), &test0> Test0Caller;
171
172 void test0const( const Test& test ){
173 }
174 typedef ConstReferenceCaller<Test, void(), &test0const> Test0ConstCaller;
175
176 void test0p( Test* test ){
177 }
178 typedef PointerCaller<Test, void(), &test0p> Test0PCaller;
179
180 void test0constp( const Test* test ){
181 }
182 typedef ConstPointerCaller<Test, void(), &test0constp> Test0ConstPCaller;
183
184 void test1( Test& test, A1 ){
185 }
186 typedef ReferenceCaller<Test, void(A1), &test1> Test1Caller;
187
188 void test1const( const Test& test, A1 ){
189 }
190 typedef ConstReferenceCaller<Test, void(A1), &test1const> Test1ConstCaller;
191
192 void test1p( Test* test, A1 ){
193 }
194 typedef PointerCaller<Test, void(A1), &test1p> Test1PCaller;
195
196 void test1constp( const Test* test, A1 ){
197 }
198 typedef ConstPointerCaller<Test, void(A1), &test1constp> Test1ConstPCaller;
199
200 void test2( Test& test, A1, A2 ){
201 }
202 typedef Function<void(Test&, A1, A2), &test2> Test2;
203
204 void test3( Test& test, A1, A2, A3 ){
205 }
206 typedef Function<void(Test&, A1, A2, A3), &test3> Test3;
207
208 void instantiate(){
209         Test test;
210         const Test& testconst = test;
211         {
212                 Callback<void()> a = Test0FreeCaller();
213                 Callback<void()> b = Test::Test0Caller( test );
214                 b = makeCallback( Test::Test0(), test );
215                 Callback<void()> c = Test::Test0ConstCaller( testconst );
216                 c = makeCallback( Test::Test0Const(), test );
217                 Test0Caller{ test };
218                 Test0ConstCaller{ testconst };
219                 Test0PCaller{ &test };
220                 Test0ConstPCaller{ &testconst };
221                 a();
222                 bool u = a != b;
223         }
224         {
225                 typedef Callback<void(A1)> TestCallback1;
226                 TestCallback1 a = Test1FreeCaller();
227                 TestCallback1 b = Test::Test1Caller( test );
228                 b = makeCallback( Test::Test1(), test );
229                 TestCallback1 c = Test::Test1ConstCaller( testconst );
230                 c = makeCallback( Test::Test1Const(), test );
231                 Test1Caller{ test };
232                 Test1ConstCaller{ testconst };
233                 Test1PCaller{ &test };
234                 Test1ConstPCaller{ &testconst };
235                 a( A1() );
236                 bool u = a != b;
237         }
238         {
239                 typedef Callback<void(A1, A2)> TestCallback2;
240                 TestCallback2 a = makeStatelessCallback( Test2Free() );
241                 TestCallback2 b = makeCallback( Test2(), test );
242                 makeCallback( Test::Test2(), test );
243                 makeCallback( Test::Test2Const(), test );
244                 a( A1(), A2() );
245                 bool u = a != b;
246         }
247         {
248                 typedef Callback<void(A1, A2, A3)> TestCallback3;
249                 TestCallback3 a = makeStatelessCallback( Test3Free() );
250                 TestCallback3 b = makeCallback( Test3(), test );
251                 makeCallback( Test::Test3(), test );
252                 makeCallback( Test::Test3Const(), test );
253                 a( A1(), A2(), A3() );
254                 bool u = a != b;
255         }
256 }
257 }