Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/single_player_map/src/lib/util/executor/executor_lua.h @ 8830

Last change on this file since 8830 was 8830, checked in by snellen, 18 years ago

Implemented the executor for 5 arguments and one return value

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