]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/caller.h
Release v2.0.23
[user/henk/code/inspircd.git] / include / caller.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007 Craig Edwards <craigedwards@brainbox.cc>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #ifndef CALLER_H
22 #define CALLER_H
23
24 /** The templates below can be auto generated by tools/create_templates.pl.
25  * They are used to represent a functor with a given number of parameters and
26  * a specific return type. To prevent passing the wrong number of parameters
27  * and have the compiler detect this error at build-time, each class is numbered
28  * according to the number of parameters it takes, e.g. caller0, caller1, caller2.
29  * These have been generated from zero parameters to eight.
30  *
31  * If you want to declare a functor which takes two parameters, a User and a Channel,
32  * and returns bool, simply create it like this:
33  *
34  * caller2<bool, User*, Channel*> MyFunction;
35  *
36  * and initialize it correctly, when placed into a class you will be able to call it:
37  *
38  * bool n = someclass->MyFunction(someuser, somechan);
39  *
40  * These functor templates work this way so that you can simply and easily allow
41  * for these class methods to be overridden from within a module, e.g. have a module
42  * which completely replaces the code for IsNick, etc. For example, with the example
43  * above:
44  *
45  * MyNewFunction replaceme(ServerInstance);
46  *
47  * someclass->MyFunction = \&replaceme;
48  *
49  * After this point, calls to someclass->MyFunction will call the new code in your
50  * replacement functor.
51  *
52  * This is a very powerful feature which should be considered 'advanced' and not for
53  * beginners. If you do not understand these templates, STAY AWAY from playing with
54  * this until you do, as if you get this wrong, this can generate some pretty long
55  * winded and confusing error messages at compile time.
56  */
57 template <typename ReturnType> class CoreExport HandlerBase0 : public classbase
58 {
59  public:
60         virtual ReturnType Call() = 0;
61         virtual ~HandlerBase0() { }
62 };
63
64 template <typename ReturnType, typename Param1> class CoreExport HandlerBase1 : public classbase
65 {
66  public:
67         virtual ReturnType Call(Param1) = 0;
68         virtual ~HandlerBase1() { }
69 };
70
71 template <typename ReturnType, typename Param1, typename Param2> class CoreExport HandlerBase2 : public classbase
72 {
73  public:
74         virtual ReturnType Call(Param1, Param2) = 0;
75         virtual ~HandlerBase2() { }
76 };
77
78 template <typename ReturnType, typename Param1, typename Param2, typename Param3> class CoreExport HandlerBase3 : public classbase
79 {
80  public:
81         virtual ReturnType Call(Param1, Param2, Param3) = 0;
82         virtual ~HandlerBase3() { }
83 };
84
85 template <typename ReturnType, typename Param1, typename Param2, typename Param3, typename Param4> class CoreExport HandlerBase4 : public classbase
86 {
87  public:
88         virtual ReturnType Call(Param1, Param2, Param3, Param4) = 0;
89         virtual ~HandlerBase4() { }
90 };
91
92 template <typename ReturnType, typename Param1, typename Param2, typename Param3, typename Param4, typename Param5> class CoreExport HandlerBase5 : public classbase
93 {
94  public:
95         virtual ReturnType Call(Param1, Param2, Param3, Param4, Param5) = 0;
96         virtual ~HandlerBase5() { }
97 };
98
99 template <typename ReturnType, typename Param1, typename Param2, typename Param3, typename Param4, typename Param5, typename Param6> class CoreExport HandlerBase6 : public classbase
100 {
101  public:
102         virtual ReturnType Call(Param1, Param2, Param3, Param4, Param5, Param6) = 0;
103         virtual ~HandlerBase6() { }
104 };
105
106 template <typename ReturnType, typename Param1, typename Param2, typename Param3, typename Param4, typename Param5, typename Param6, typename Param7> class CoreExport HandlerBase7 : public classbase
107 {
108  public:
109         virtual ReturnType Call(Param1, Param2, Param3, Param4, Param5, Param6, Param7) = 0;
110         virtual ~HandlerBase7() { }
111 };
112
113 template <typename ReturnType, typename Param1, typename Param2, typename Param3, typename Param4, typename Param5, typename Param6, typename Param7, typename Param8> class CoreExport HandlerBase8 : public classbase
114 {
115  public:
116         virtual ReturnType Call(Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8) = 0;
117         virtual ~HandlerBase8() { }
118 };
119
120 template <typename HandlerType> class caller
121 {
122  public:
123         HandlerType* target;
124
125         caller(HandlerType* initial)
126         : target(initial)
127         { }
128
129         virtual ~caller() { }
130 };
131
132 template <typename ReturnType> class caller0 : public caller< HandlerBase0<ReturnType> >
133 {
134  public:
135         caller0(HandlerBase0<ReturnType>* initial)
136         : caller< HandlerBase0<ReturnType> >::caller(initial)
137         { }
138
139         ReturnType operator() ()
140         {
141                 return this->target->Call();
142         }
143 };
144
145 template <typename ReturnType, typename Param1> class caller1 : public caller< HandlerBase1<ReturnType, Param1> >
146 {
147  public:
148         caller1(HandlerBase1<ReturnType, Param1>* initial)
149         : caller< HandlerBase1<ReturnType, Param1> >(initial)
150         { }
151
152         ReturnType operator() (Param1 param1)
153         {
154                 return this->target->Call(param1);
155         }
156 };
157
158 template <typename ReturnType, typename Param1, typename Param2> class caller2 : public caller< HandlerBase2<ReturnType, Param1, Param2> >
159 {
160  public:
161         caller2(HandlerBase2<ReturnType, Param1, Param2>* initial)
162         : caller< HandlerBase2<ReturnType, Param1, Param2> >(initial)
163         { }
164
165         ReturnType operator() (Param1 param1, Param2 param2)
166         {
167                 return this->target->Call(param1, param2);
168         }
169 };
170
171 template <typename ReturnType, typename Param1, typename Param2, typename Param3> class caller3 : public caller< HandlerBase3<ReturnType, Param1, Param2, Param3> >
172 {
173  public:
174         caller3(HandlerBase3<ReturnType, Param1, Param2, Param3>* initial)
175         : caller< HandlerBase3<ReturnType, Param1, Param2, Param3> >(initial)
176         { }
177
178         ReturnType operator() (Param1 param1, Param2 param2, Param3 param3)
179         {
180                 return this->target->Call(param1, param2, param3);
181         }
182 };
183
184 template <typename ReturnType, typename Param1, typename Param2, typename Param3, typename Param4> class caller4 : public caller< HandlerBase4<ReturnType, Param1, Param2, Param3, Param4> >
185 {
186  public:
187         caller4(HandlerBase4<ReturnType, Param1, Param2, Param3, Param4>* initial)
188         : caller< HandlerBase4<ReturnType, Param1, Param2, Param3, Param4> >(initial)
189         { }
190
191         ReturnType operator() (Param1 param1, Param2 param2, Param3 param3, Param4 param4)
192         {
193                 return this->target->Call(param1, param2, param3, param4);
194         }
195 };
196
197 template <typename ReturnType, typename Param1, typename Param2, typename Param3, typename Param4, typename Param5> class caller5 : public caller< HandlerBase5<ReturnType, Param1, Param2, Param3, Param4, Param5> >
198 {
199  public:
200         caller5(HandlerBase5<ReturnType, Param1, Param2, Param3, Param4, Param5>* initial)
201         : caller< HandlerBase5<ReturnType, Param1, Param2, Param3, Param4, Param5> >(initial)
202         { }
203
204         ReturnType operator() (Param1 param1, Param2 param2, Param3 param3, Param4 param4, Param5 param5)
205         {
206                 return this->target->Call(param1, param2, param3, param4, param5);
207         }
208 };
209
210 template <typename ReturnType, typename Param1, typename Param2, typename Param3, typename Param4, typename Param5, typename Param6> class caller6 : public caller< HandlerBase6<ReturnType, Param1, Param2, Param3, Param4, Param5, Param6> >
211 {
212  public:
213         caller6(HandlerBase6<ReturnType, Param1, Param2, Param3, Param4, Param5, Param6>* initial)
214         : caller< HandlerBase6<ReturnType, Param1, Param2, Param3, Param4, Param5, Param6> >(initial)
215         { }
216
217         ReturnType operator() (Param1 param1, Param2 param2, Param3 param3, Param4 param4, Param5 param5, Param6 param6)
218         {
219                 return this->target->Call(param1, param2, param3, param4, param5, param6);
220         }
221 };
222
223 template <typename ReturnType, typename Param1, typename Param2, typename Param3, typename Param4, typename Param5, typename Param6, typename Param7> class caller7 : public caller< HandlerBase7<ReturnType, Param1, Param2, Param3, Param4, Param5, Param6, Param7> >
224 {
225  public:
226         caller7(HandlerBase7<ReturnType, Param1, Param2, Param3, Param4, Param5, Param6, Param7>* initial)
227         : caller< HandlerBase7<ReturnType, Param1, Param2, Param3, Param4, Param5, Param6, Param7> >(initial)
228         { }
229
230         ReturnType operator() (Param1 param1, Param2 param2, Param3 param3, Param4 param4, Param5 param5, Param6 param6, Param7 param7)
231         {
232                 return this->target->Call(param1, param2, param3, param4, param5, param6, param7);
233         }
234 };
235
236 template <typename ReturnType, typename Param1, typename Param2, typename Param3, typename Param4, typename Param5, typename Param6, typename Param7, typename Param8> class caller8 : public caller< HandlerBase8<ReturnType, Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8> >
237 {
238  public:
239         caller8(HandlerBase8<ReturnType, Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8>* initial)
240         : caller< HandlerBase8<ReturnType, Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8> >(initial)
241         { }
242
243         ReturnType operator() (Param1 param1, Param2 param2, Param3 param3, Param4 param4, Param5 param5, Param6 param6, Param7 param7, Param8 param8)
244         {
245                 return this->target->Call(param1, param2, param3, param4, param5, param6, param7, param8);
246         }
247 };
248
249 /** These shorthand macros are used to define a functor class which only implements Call(). Most functors are like this.
250  * If you want something more complex, define them by hand.
251  *
252  * The first parameter to each macro is the class name to define, the second parameter is the return value of Call().
253  * The following parameters are the parameter types for Call(), and again, the macro is numbered to match the number of
254  * parameters, to prevent mistakes.
255  */
256 #define DEFINE_HANDLER0(NAME, RETURN) \
257         class CoreExport NAME : public HandlerBase0<RETURN> { public: NAME() { } virtual ~NAME() { } virtual RETURN Call(); }
258
259 #define DEFINE_HANDLER1(NAME, RETURN, V1) \
260         class CoreExport NAME : public HandlerBase1<RETURN, V1> { public: NAME() { } virtual ~NAME() { } virtual RETURN Call(V1); }
261
262 #define DEFINE_HANDLER2(NAME, RETURN, V1, V2) \
263         class CoreExport NAME : public HandlerBase2<RETURN, V1, V2> { public: NAME() { } virtual ~NAME() { } virtual RETURN Call(V1, V2); }
264
265 #define DEFINE_HANDLER3(NAME, RETURN, V1, V2, V3) \
266         class CoreExport NAME : public HandlerBase3<RETURN, V1, V2, V3> { public: NAME() { } virtual ~NAME() { } virtual RETURN Call(V1, V2, V3); }
267
268 #define DEFINE_HANDLER4(NAME, RETURN, V1, V2, V3, V4) \
269         class CoreExport NAME : public HandlerBase4<RETURN, V1, V2, V3, V4> { public: NAME() { } virtual ~NAME() { } virtual RETURN Call(V1, V2, V3, V4); }
270
271 #define DEFINE_HANDLER5(NAME, RETURN, V1, V2, V3, V4, V5) \
272         class CoreExport NAME : public HandlerBase5<RETURN, V1, V2, V3, V4, V5> { public: NAME() { } virtual ~NAME() { } virtual RETURN Call(V1, V2, V3, V4, V5); }
273
274 #define DEFINE_HANDLER6(NAME, RETURN, V1, V2, V3, V4, V5, V6) \
275         class CoreExport NAME : public HandlerBase6<RETURN, V1, V2, V3, V4, V5, V6> { public: NAME() { } virtual ~NAME() { } virtual RETURN Call(V1, V2, V3, V4, V5, V6); }
276
277 #define DEFINE_HANDLER7(NAME, RETURN, V1, V2, V3, V4, V5, V6, V7) \
278         class CoreExport NAME : public HandlerBase7<RETURN, V1, V2, V3, V4, V5, V6, V7> { public: NAME() { } virtual ~NAME() { } virtual RETURN Call(V1, V2, V3, V4, V5, V6, V7); }
279
280 #define DEFINE_HANDLER8(NAME, RETURN, V1, V2, V3, V4, V5, V6, V7, V8) \
281         class CoreExport NAME : public HandlerBase8<RETURN, V1, V2, V3, V4, V5, V6, V7, V8> { public: NAME() { } virtual ~NAME() { } virtual RETURN Call(V1, V2, V3, V4, V5, V6, V7, V8); }
282
283 #endif