Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/util/executor/executor_lua.h @ 9727

Last change on this file since 9727 was 9727, checked in by bensch, 18 years ago

orxonox/new_class_id: new Executor construct, that is much more typesafe, faster, and easier to extend…

Also changed the LoadParam process, and adapted ScriptEngine calls

Then at the end, some missing headers appeared, and appended them to all the cc-files again.

File size: 16.4 KB
Line 
1/*!
2 * @file executor.h
3 * Definition of a on-screen-shell
4 */
5
6#ifndef _EXECUTOR_LUA_H
7#define _EXECUTOR_LUA_H
8
9#include "executor.h"
10#include "compiler.h"
11#include "debug.h"
12#include "luaincl.h"
13
14
15
16template<typename type> type fromLua(lua_State* state, int index);
17template<> bool fromLua<bool>(lua_State* state, int index);
18template<> int fromLua<int>(lua_State* state, int index);
19template<> unsigned int fromLua<unsigned int>(lua_State* state, int index);
20template<> float fromLua<float>(lua_State* state, int index);
21template<> char fromLua<char>(lua_State* state, int index);
22template<> const std::string& fromLua<const std::string&>(lua_State* state, int index);
23
24template<typename type> void toLua(lua_State* state, type value);
25template<> void toLua<bool>(lua_State* state, bool value);
26template<> void toLua<int>(lua_State* state, int value);
27template<> void toLua<unsigned int>(lua_State* state, unsigned int value);
28template<> void toLua<float>(lua_State* state, float value);
29template<> void toLua<char>(lua_State* state, char value);
30template<> void toLua<const std::string&>(lua_State* state, const std::string& value);
31
32// FORWARD DECLARATION
33
34///////////////////////
35///// WITHOUT RET /////
36///////////////////////
37
38///////////
39//// 0 ////
40///////////
41//! Executes a Function with a lua_State* parameter.
42template<class T> class ExecutorLua0 : public Executor<lua_State*>
43{
44public:
45  /**
46  * @brief Constructor of a ExecutorXML
47  * @param function a Function to call
48   */
49  ExecutorLua0(void(T::*function)())
50      : Executor<lua_State*>(false)
51  {
52    this->functionPointer = function;
53    this->functorType = Executor_Objective;
54  }
55
56
57  virtual void operator()(BaseObject* object, lua_State*& state) const
58  {
59    (dynamic_cast<T*>(object)->*(functionPointer))();
60  }
61
62  /**
63   * @returns a _new_ Copy of this Executor
64   */
65  virtual Executor<lua_State*>* clone () const
66  {
67    return new ExecutorLua0<T>(ExecutorLua0<T>(this->functionPointer));
68  }
69private:
70  void          (T::*functionPointer)();
71};
72
73
74
75///////////
76//// 1 ////
77///////////
78//! Executes a Function with a lua_State* parameter.
79template<class T, typename type0> class ExecutorLua1 : public Executor<lua_State*>
80{
81public:
82  /**
83  * @brief Constructor of a ExecutorXML
84  * @param function a Function to call
85   */
86  ExecutorLua1(void(T::*function)(type0))
87      : Executor<lua_State*>(false, ExecutorParamType<type0>())
88  {
89    this->functionPointer = function;
90    this->functorType = Executor_Objective;
91  }
92
93  virtual void operator()(BaseObject* object, lua_State*& state) const
94  {
95    (dynamic_cast<T*>(object)->*(functionPointer))(fromLua<type0>(state, 1));
96  }
97
98  /**
99   * @returns a _new_ Copy of this Executor
100   */
101  virtual Executor<lua_State*>* clone () const
102  {
103    return new ExecutorLua1<T, type0>((this->functionPointer));
104  }
105private:
106  void          (T::*functionPointer)(type0);
107};
108
109
110
111///////////
112//// 2 ////
113///////////
114//! Executes a Function with a lua_State* parameter.
115template<class T, typename type0, typename type1> class ExecutorLua2 : public Executor<lua_State*>
116{
117public:
118  /**
119  * @brief Constructor of a ExecutorXML
120  * @param function a Function to call
121   */
122  ExecutorLua2(void(T::*function)(type0, type1))
123      : Executor<lua_State*>(false, ExecutorParamType<type0>(), ExecutorParamType<type1>())
124  {
125    this->functionPointer = function;
126    this->functorType = Executor_Objective;
127  }
128
129  /**
130   * @brief executes the Command on BaseObject
131   * @param object the BaseObject to execute this Executor on
132   * @param loadString ignored in this case
133   */
134  virtual void operator()(BaseObject* object, const SubString& = SubString()) const
135  {
136    PRINTF(1)("no usefull executor\n");
137  }
138
139  virtual void operator()(BaseObject* object, lua_State*& state) const
140  {
141    (dynamic_cast<T*>(object)->*(functionPointer))(
142      fromLua<type0>(state, 1),
143      fromLua<type1>(state, 2) );
144  }
145
146  /**
147   * @returns a _new_ Copy of this Executor
148   */
149  virtual Executor<lua_State*>* clone () const
150  {
151    return new ExecutorLua2<T, type0, type1>(this->functionPointer);
152  }
153private:
154  void          (T::*functionPointer)(type0, type1);
155};
156
157
158///////////
159//// 3 ////
160///////////
161//! Executes a Function with a lua_State* parameter.
162template<class T, typename type0, typename type1, typename type2> class ExecutorLua3 : public Executor<lua_State*>
163{
164public:
165  /**
166  * @brief Constructor of a ExecutorXML
167  * @param function a Function to call
168   */
169  ExecutorLua3(void(T::*function)(type0, type1, type2))
170      : Executor<lua_State*>(false, ExecutorParamType<type0>(), ExecutorParamType<type1>(), ExecutorParamType<type2>())
171  {
172    this->functionPointer = function;
173    this->functorType = Executor_Objective;
174  }
175
176  /**
177   * @brief executes the Command on BaseObject
178   * @param object the BaseObject to execute this Executor on
179   * @param loadString ignored in this case
180   */
181  virtual void operator()(BaseObject* object, const SubString& = SubString()) const
182  {
183    PRINTF(1)("no usefull executor\n");
184  }
185
186  virtual void operator()(BaseObject* object, lua_State*& state) const
187  {
188    (dynamic_cast<T*>(object)->*(functionPointer))(
189      fromLua<type0>(state, 1),
190      fromLua<type1>(state, 2),
191      fromLua<type2>(state, 3) );
192  }
193
194  /**
195   * @returns a _new_ Copy of this Executor
196   */
197  virtual Executor<lua_State*>* clone () const
198  {
199    return new ExecutorLua3<T, type0, type1, type2>(this->functionPointer);
200  }
201private:
202  void          (T::*functionPointer)(type0, type1, type2);
203};
204
205
206///////////
207//// 4 ////
208///////////
209//! Executes a Function with a lua_State* parameter.
210template<class T, typename type0, typename type1, typename type2, typename type3> class ExecutorLua4 : public Executor<lua_State*>
211{
212public:
213  /**
214  * @brief Constructor of a ExecutorXML
215  * @param function a Function to call
216   */
217  ExecutorLua4(void(T::*function)(type0, type1, type2, type3))
218      : Executor<lua_State*>(false, ExecutorParamType<type0>(), ExecutorParamType<type1>(), ExecutorParamType<type2>(), ExecutorParamType<type3>())
219  {
220    this->functionPointer = function;
221    this->functorType = Executor_Objective;
222  }
223
224  virtual void operator()(BaseObject* object, lua_State*& state) const
225  {
226    (dynamic_cast<T*>(object)->*(functionPointer))(
227      fromLua<type0>(state, 1),
228      fromLua<type1>(state, 2),
229      fromLua<type2>(state, 3),
230      fromLua<type3>(state, 4) );
231  }
232
233  /**
234   * @returns a _new_ Copy of this Executor
235   */
236  virtual Executor<lua_State*>* clone () const
237  {
238    return new ExecutorLua4<T, type0, type1, type2, type3>(this->functionPointer);
239  }
240private:
241  void          (T::*functionPointer)(type0, type1, type2, type3);
242};
243
244
245
246
247////////////////////
248///// WITH RET /////
249////////////////////
250
251
252///////////
253//// 0 ////
254///////////
255//! Executes a Function with a lua_State* parameter.
256template<class T, typename ret> class ExecutorLua0ret : public Executor<lua_State*>
257{
258public:
259  /**
260  * @brief Constructor of a ExecutorXML
261  * @param function a Function to call
262   */
263  ExecutorLua0ret(ret (T::*function)())
264      : Executor<lua_State*>(true)
265  {
266    this->functionPointer = function;
267    this->functorType = Executor_Objective;
268  }
269
270  virtual void operator()(BaseObject* object, lua_State*& state) const
271  {
272    toLua<ret>(state, (dynamic_cast<T*>(object)->*(functionPointer))());
273  }
274
275  /**
276   * @returns a _new_ Copy of this Executor
277   */
278  virtual Executor<lua_State*>* clone () const
279  {
280    return new ExecutorLua0ret<T, ret>(this->functionPointer);
281  }
282private:
283  ret           (T::*functionPointer)();
284};
285
286
287
288///////////
289//// 1 ////
290///////////
291//! Executes a Function with a lua_State* parameter.
292template<class T, typename ret, typename type0> class ExecutorLua1ret : public Executor<lua_State*>
293{
294public:
295  /**
296  * @brief Constructor of a ExecutorXML
297  * @param function a Function to call
298   */
299  ExecutorLua1ret(ret (T::*function)(type0))
300      : Executor<lua_State*>(true, ExecutorParamType<type0>())
301  {
302    this->functionPointer = function;
303    this->functorType = Executor_Objective;
304  }
305
306  virtual void operator()(BaseObject* object, lua_State*& state) const
307  {
308    toLua<ret>(state, (dynamic_cast<T*>(object)->*(functionPointer))(
309                 fromLua<type0>(state, 1)));
310  }
311
312  /**
313   * @returns a _new_ Copy of this Executor
314   */
315  virtual Executor<lua_State*>* clone () const
316  {
317    return new ExecutorLua1ret<T, ret, type0>(this->functionPointer);
318  }
319private:
320  ret           (T::*functionPointer)(type0);
321};
322
323///////////
324//// 2 ////
325///////////
326//! Executes a Function with a lua_State* parameter.
327template<class T, typename ret, typename type0, typename type1> class ExecutorLua2ret : public Executor<lua_State*>
328{
329public:
330  /**
331  * @brief Constructor of a ExecutorXML
332  * @param function a Function to call
333   */
334  ExecutorLua2ret(ret (T::*function)(type0, type1))
335      : Executor<lua_State*>(true, ExecutorParamType<type0>(), ExecutorParamType<type1>())
336  {
337    this->functionPointer = function;
338    this->functorType = Executor_Objective;
339  }
340
341  virtual void operator()(BaseObject* object, lua_State*& state) const
342  {
343    toLua<ret>(state, (dynamic_cast<T*>(object)->*(functionPointer))(
344                 fromLua<type0>(state, 1),
345                 fromLua<type1>(state, 2) ));
346  }
347
348  /**
349   * @returns a _new_ Copy of this Executor
350   */
351  virtual Executor<lua_State*>* clone () const
352  {
353    return new ExecutorLua2ret<T, ret, type0, type1>(this->functionPointer);
354  }
355private:
356  ret           (T::*functionPointer)(type0, type1);
357};
358
359
360///////////
361//// 3 ////
362///////////
363//! Executes a Function with a lua_State* parameter.
364template<class T, typename ret, typename type0, typename type1, typename type2> class ExecutorLua3ret : public Executor<lua_State*>
365{
366public:
367  /**
368  * @brief Constructor of a ExecutorXML
369  * @param function a Function to call
370   */
371  ExecutorLua3ret(ret (T::*function)(type0, type1, type2))
372      : Executor<lua_State*>(true, ExecutorParamType<type0>(), ExecutorParamType<type1>(), ExecutorParamType<type2>())
373  {
374    this->functionPointer = function;
375    this->functorType = Executor_Objective;
376  }
377
378  /**
379   * @brief executes the Command on BaseObject
380   * @param object the BaseObject to execute this Executor on
381   * @param loadString ignored in this case
382   */
383  virtual void operator()(BaseObject* object, const SubString& = SubString()) const
384  {
385    PRINTF(1)("no usefull executor\n");
386  }
387
388  virtual void operator()(BaseObject* object, lua_State*& state) const
389  {
390    toLua<ret>(state, (dynamic_cast<T*>(object)->*(functionPointer))(
391                 fromLua<type0>(state, 1),
392                 fromLua<type1>(state, 2),
393                 fromLua<type2>(state, 3) ));
394  }
395
396  /**
397   * @returns a _new_ Copy of this Executor
398   */
399  virtual Executor<lua_State*>* clone () const
400  {
401    return new ExecutorLua3ret<T, ret, type0, type1, type2>(this->functionPointer);
402  }
403private:
404  ret          (T::*functionPointer)(type0, type1, type2);
405};
406
407
408///////////
409//// 4 ////
410///////////
411//! Executes a Function with a lua_State* parameter.
412template<class T, typename ret, typename type0, typename type1, typename type2, typename type3> class ExecutorLua4ret : public Executor<lua_State*>
413{
414public:
415  /**
416  * @brief Constructor of a ExecutorXML
417  * @param function a Function to call
418   */
419  ExecutorLua4ret(ret (T::*function)(type0, type1, type2, type3))
420      : Executor<lua_State*>(true, ExecutorParamType<type0>(), ExecutorParamType<type1>(), ExecutorParamType<type2>(), ExecutorParamType<type3>())
421  {
422    this->functionPointer = function;
423    this->functorType = Executor_Objective;
424  }
425
426  virtual void operator()(BaseObject* object, lua_State*& state) const
427  {
428    toLua<ret>(state, (dynamic_cast<T*>(object)->*(functionPointer))(
429                 fromLua<type0>(state, 1),
430                 fromLua<type1>(state, 2),
431                 fromLua<type2>(state, 3),
432                 fromLua<type3>(state, 4) ));
433  }
434
435  /**
436   * @returns a _new_ Copy of this Executor
437   */
438  virtual Executor<lua_State*>* clone () const
439  {
440    return new ExecutorLua4ret<T, ret, type0, type1, type2, type3>(this->functionPointer);
441  }
442private:
443  ret          (T::*functionPointer)(type0, type1, type2, type3);
444};
445
446///////////
447//// 5 ////
448///////////
449//! Executes a Function with a lua_State* parameter.
450template<class T, typename ret, typename type0, typename type1, typename type2, typename type3, typename type4> class ExecutorLua5ret : public Executor<lua_State*>
451{
452public:
453  /**
454  * @brief Constructor of a ExecutorXML
455  * @param function a Function to call
456   */
457  ExecutorLua5ret(ret (T::*function)(type0, type1, type2, type3, type4))
458      : Executor<lua_State*>(true, ExecutorParamType<type0>(), ExecutorParamType<type1>(), ExecutorParamType<type2>(), ExecutorParamType<type3>(), ExecutorParamType<type4>())
459  {
460    this->functionPointer = function;
461    this->functorType = Executor_Objective;
462  }
463
464  virtual void operator()(BaseObject* object, lua_State*& state) const
465  {
466    toLua<ret>(state, (dynamic_cast<T*>(object)->*(functionPointer))(
467                 fromLua<type0>(state, 1),
468                 fromLua<type1>(state, 2),
469                 fromLua<type2>(state, 3),
470                 fromLua<type3>(state, 4),
471                 fromLua<type4>(state, 5) ));
472  }
473
474  /**
475   * @returns a _new_ Copy of this Executor
476   */
477  virtual Executor<lua_State*>* clone () const
478  {
479    return new ExecutorLua5ret<T, ret, type0, type1, type2, type3, type4>(this->functionPointer);
480  }
481private:
482  ret          (T::*functionPointer)(type0, type1, type2, type3, type4);
483};
484
485///////////
486//// 6 ////
487///////////
488//! Executes a Function with a lua_State* parameter.
489template<class T, typename ret, typename type0, typename type1, typename type2, typename type3, typename type4, typename type5> class ExecutorLua6ret : public Executor<lua_State*>
490{
491public:
492  /**
493  * @brief Constructor of a ExecutorXML
494  * @param function a Function to call
495   */
496  ExecutorLua6ret(ret (T::*function)(type0, type1, type2, type3, type4, type5))
497      : Executor<lua_State*>(true, ExecutorParamType<type0>(), ExecutorParamType<type1>(),
498                             ExecutorParamType<type2>(), ExecutorParamType<type3>(),
499                             ExecutorParamType<type4>(), ExecutorParamType<type5>())
500  {
501    this->functionPointer = function;
502    this->functorType = Executor_Objective;
503  }
504
505  virtual void operator()(BaseObject* object, lua_State*& state) const
506  {
507    toLua<ret>(state, (dynamic_cast<T*>(object)->*(functionPointer))(
508                 fromLua<type0>(state, 1),
509                 fromLua<type1>(state, 2),
510                 fromLua<type2>(state, 3),
511                 fromLua<type3>(state, 4),
512                 fromLua<type4>(state, 5),
513                 fromLua<type5>(state, 6) ));
514  }
515
516  /**
517            * @returns a _new_ Copy of this Executor
518   */
519  virtual Executor<lua_State*>* clone () const
520  {
521    return new ExecutorLua6ret<T, ret, type0, type1, type2, type3, type4, type5>(this->functionPointer);
522  }
523private:
524  ret           (T::*functionPointer)(type0, type1, type2, type3, type4, type5);
525};
526
527///////////
528//// 7 ////
529///////////
530//! Executes a Function with a lua_State* parameter.
531template<class T, typename ret, typename type0, typename type1, typename type2, typename type3, typename type4, typename type5, typename type6> class ExecutorLua7ret : public Executor<lua_State*>
532{
533public:
534  /**
535  * @brief Constructor of a ExecutorXML
536  * @param function a Function to call
537   */
538  ExecutorLua7ret(ret (T::*function)(type0, type1, type2, type3, type4, type5, type6))
539      : Executor<lua_State*>(true, ExecutorParamType<type0>(), ExecutorParamType<type1>(),
540                             ExecutorParamType<type2>(), ExecutorParamType<type3>(),
541                             ExecutorParamType<type4>(), ExecutorParamType<type5>(),
542                             ExecutorParamType<type6>())
543  {
544    this->functionPointer = function;
545    this->functorType = Executor_Objective;
546  }
547
548  virtual void operator()(BaseObject* object, lua_State*& state) const
549  {
550    toLua<ret>(state, (dynamic_cast<T*>(object)->*(functionPointer))(
551                 fromLua<type0>(state, 1),
552                 fromLua<type1>(state, 2),
553                 fromLua<type2>(state, 3),
554                 fromLua<type3>(state, 4),
555                 fromLua<type4>(state, 5),
556                 fromLua<type5>(state, 6),
557                 fromLua<type6>(state, 7) ));
558  }
559
560  /**
561            * @returns a _new_ Copy of this Executor
562   */
563  virtual Executor<lua_State*>* clone () const
564  {
565    return new ExecutorLua7ret<T, ret, type0, type1, type2, type3, type4, type5, type6>(this->functionPointer);
566  }
567private:
568  ret           (T::*functionPointer)(type0, type1, type2, type3, type4, type5, type6);
569};
570
571
572#endif /* _EXECUTOR_LUA_H */
Note: See TracBrowser for help on using the repository browser.