]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/caller.h
b5dd38ef0e63015c57af58f4508c220c0a973b6d
[user/henk/code/inspircd.git] / include / caller.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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 userrec and a chanrec,
25  * and returns bool, simply create it like this:
26  *
27  * caller2<bool, userrec*, chanrec*> 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
51 template <typename ReturnType> class CoreExport HandlerBase0
52 {
53  public:
54         virtual ReturnType Call() = 0;
55         virtual ~HandlerBase0() { }
56 };
57
58 template <typename ReturnType, typename Param1> class CoreExport HandlerBase1
59 {
60  public:
61         virtual ReturnType Call(Param1) = 0;
62         virtual ~HandlerBase1() { }
63 };
64
65 template <typename ReturnType, typename Param1, typename Param2> class CoreExport HandlerBase2
66 {
67  public:
68         virtual ReturnType Call(Param1, Param2) = 0;
69         virtual ~HandlerBase2() { }
70 };
71
72 template <typename ReturnType, typename Param1, typename Param2, typename Param3> class CoreExport HandlerBase3
73 {
74  public:
75         virtual ReturnType Call(Param1, Param2, Param3) = 0;
76         virtual ~HandlerBase3() { }
77 };
78
79 template <typename ReturnType, typename Param1, typename Param2, typename Param3, typename Param4> class CoreExport HandlerBase4
80 {
81  public:
82         virtual ReturnType Call(Param1, Param2, Param3, Param4) = 0;
83         virtual ~HandlerBase4() { }
84 };
85
86 template <typename ReturnType, typename Param1, typename Param2, typename Param3, typename Param4, typename Param5> class CoreExport HandlerBase5
87 {
88  public:
89         virtual ReturnType Call(Param1, Param2, Param3, Param4, Param5) = 0;
90         virtual ~HandlerBase5() { }
91 };
92
93 template <typename ReturnType, typename Param1, typename Param2, typename Param3, typename Param4, typename Param5, typename Param6> class CoreExport HandlerBase6
94 {
95  public:
96         virtual ReturnType Call(Param1, Param2, Param3, Param4, Param5, Param6) = 0;
97         virtual ~HandlerBase6() { }
98 };
99
100 template <typename ReturnType, typename Param1, typename Param2, typename Param3, typename Param4, typename Param5, typename Param6, typename Param7> class CoreExport HandlerBase7
101 {
102  public:
103         virtual ReturnType Call(Param1, Param2, Param3, Param4, Param5, Param6, Param7) = 0;
104         virtual ~HandlerBase7() { }
105 };
106
107 template <typename ReturnType, typename Param1, typename Param2, typename Param3, typename Param4, typename Param5, typename Param6, typename Param7, typename Param8> class CoreExport HandlerBase8
108 {
109  public:
110         virtual ReturnType Call(Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8) = 0;
111         virtual ~HandlerBase8() { }
112 };
113
114 template <typename HandlerType> class CoreExport caller
115 {
116  public:
117         HandlerType* target;
118
119         caller(HandlerType* initial)
120         : target(initial)
121         { }
122
123         virtual ~caller() { }
124
125         caller& operator=(HandlerType* newtarget)
126         {
127                 target = newtarget;
128                 return *this;
129         }
130 };
131
132 template <typename ReturnType> class CoreExport caller0 : public caller< HandlerBase0<ReturnType> >
133 {
134  public:
135         caller0(HandlerBase0<ReturnType>* initial)
136         : caller< HandlerBase0<ReturnType> >::caller(initial)
137         { }
138
139         virtual ReturnType operator() ()
140         {
141                 return this->target->Call();
142         }
143 };
144
145 template <typename ReturnType, typename Param1> class CoreExport caller1 : public caller< HandlerBase1<ReturnType, Param1> >
146 {
147  public:
148         caller1(HandlerBase1<ReturnType, Param1>* initial)
149         : caller< HandlerBase1<ReturnType, Param1> >(initial)
150         { }
151
152         virtual ReturnType operator() (Param1 param1)
153         {
154                 return this->target->Call(param1);
155         }
156 };
157
158 template <typename ReturnType, typename Param1, typename Param2> class CoreExport 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         virtual 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 CoreExport 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         virtual 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 CoreExport 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         virtual 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 CoreExport 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         virtual 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 CoreExport 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         virtual 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 CoreExport 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         virtual 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 CoreExport 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         virtual 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
257 #define DEFINE_HANDLER0(NAME, RETURN) \
258         class CoreExport NAME : public HandlerBase0<RETURN> { InspIRCd* Server; public: NAME(InspIRCd* Srv) : Server(Srv) { } virtual ~NAME() { } virtual RETURN Call(); };
259
260 #define DEFINE_HANDLER1(NAME, RETURN, V1) \
261         class CoreExport NAME : public HandlerBase1<RETURN, V1> { InspIRCd* Server; public: NAME(InspIRCd* Srv) : Server(Srv) { } virtual ~NAME() { } virtual RETURN Call(V1); };
262
263 #define DEFINE_HANDLER2(NAME, RETURN, V1, V2) \
264         class CoreExport NAME : public HandlerBase2<RETURN, V1, V2> { InspIRCd* Server; public: NAME(InspIRCd* Srv) : Server(Srv) { } virtual ~NAME() { } virtual RETURN Call(V1, V2); };
265
266 #define DEFINE_HANDLER3(NAME, RETURN, V1, V2, V3) \
267         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); };
268
269 #define DEFINE_HANDLER4(NAME, RETURN, V1, V2, V3, V4) \
270         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); };
271
272 #define DEFINE_HANDLER5(NAME, RETURN, V1, V2, V3, V4, V5) \
273         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); };
274
275 #define DEFINE_HANDLER6(NAME, RETURN, V1, V2, V3, V4, V5, V6) \
276         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); };
277
278 #define DEFINE_HANDLER7(NAME, RETURN, V1, V2, V3, V4, V5, V6, V7) \
279         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); };
280
281 #define DEFINE_HANDLER8(NAME, RETURN, V1, V2, V3, V4, V5, V6, V7, V8) \
282         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); };
283
284 #endif
285