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