Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/util/executor/executor_lua.h @ 8822

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

merged the script_engine back here

File size: 10.5 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#endif /* _EXECUTOR_LUA_H */
Note: See TracBrowser for help on using the repository browser.