Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

compiles again

File size: 9.0 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
186
187
188
189////////////////////
190///// WITH RET /////
191////////////////////
192
193
194///////////
195//// 0 ////
196///////////
197//! Executes a Function with a lua_State* parameter.
198template<class T, typename ret> class ExecutorLua0ret : public Executor
199{
200  public:
201    /**
202     * @brief Constructor of a ExecutorXML
203     * @param function a Function to call
204     */
205    ExecutorLua0ret(ret (T::*function)())
206        : Executor()
207    {
208      this->functionPointer = function;
209      this->functorType = Executor_Objective | Executor_NoLoadString;
210    }
211
212    /**
213     * @brief executes the Command on BaseObject
214     * @param object the BaseObject to execute this Executor on
215     * @param loadString ignored in this case
216     */
217    virtual void operator()(BaseObject* object, const SubString& = SubString()) const
218    {
219      PRINTF(1)("no usefull executor\n");
220    }
221
222    virtual void operator()(BaseObject* object, int& count, void* values) const
223    {
224      lua_State* state = (lua_State*)values;
225      count = 1;
226
227      toLua<ret>(state, (dynamic_cast<T*>(object)->*(functionPointer))());
228    }
229
230    /**
231                       * @returns a _new_ Copy of this Executor
232     */
233    virtual Executor* clone () const
234    {
235      return new ExecutorLua0ret<T, ret>(this->functionPointer);
236    }
237  private:
238    ret           (T::*functionPointer)();
239};
240
241
242
243///////////
244//// 1 ////
245///////////
246//! Executes a Function with a lua_State* parameter.
247template<class T, typename ret, typename type0> class ExecutorLua1ret : public Executor
248{
249  public:
250    /**
251     * @brief Constructor of a ExecutorXML
252     * @param function a Function to call
253     */
254    ExecutorLua1ret(ret (T::*function)(type0))
255        : Executor(ExecutorParamType<type0>())
256    {
257      this->functionPointer = function;
258      this->functorType = Executor_Objective | Executor_NoLoadString;
259    }
260
261    /**
262     * @brief executes the Command on BaseObject
263     * @param object the BaseObject to execute this Executor on
264     * @param loadString ignored in this case
265     */
266    virtual void operator()(BaseObject* object, const SubString& = SubString()) const
267    {
268      PRINTF(1)("no usefull executor\n");
269    }
270
271    virtual void operator()(BaseObject* object, int& count, void* values) const
272    {
273      lua_State* state = (lua_State*)values;
274      count = 1;
275
276      toLua<ret>(state, (dynamic_cast<T*>(object)->*(functionPointer))(
277                   fromLua<type0>(state, 1)));
278    }
279
280    /**
281     * @returns a _new_ Copy of this Executor
282     */
283    virtual Executor* clone () const
284    {
285      return new ExecutorLua1ret<T, ret, type0>(this->functionPointer);
286    }
287  private:
288    ret           (T::*functionPointer)(type0);
289};
290
291///////////
292//// 2 ////
293///////////
294//! Executes a Function with a lua_State* parameter.
295template<class T, typename ret, typename type0, typename type1> class ExecutorLua2ret : public Executor
296{
297  public:
298    /**
299     * @brief Constructor of a ExecutorXML
300     * @param function a Function to call
301     */
302    ExecutorLua2ret(ret (T::*function)(type0, type1))
303        : Executor(ExecutorParamType<type0>(), ExecutorParamType<type1>())
304    {
305      this->functionPointer = function;
306      this->functorType = Executor_Objective | Executor_NoLoadString;
307    }
308
309    /**
310     * @brief executes the Command on BaseObject
311     * @param object the BaseObject to execute this Executor on
312     * @param loadString ignored in this case
313     */
314    virtual void operator()(BaseObject* object, const SubString& = SubString()) const
315    {
316      PRINTF(1)("no usefull executor\n");
317    }
318
319    virtual void operator()(BaseObject* object, int& count, void* values) const
320    {
321      lua_State* state = (lua_State*)values;
322      count = 1;
323
324      toLua<ret>(state, (dynamic_cast<T*>(object)->*(functionPointer))(
325                   fromLua<type0>(state, 1),
326                   fromLua<type1>(state, 2) ));
327    }
328
329    /**
330     * @returns a _new_ Copy of this Executor
331     */
332    virtual Executor* clone () const
333    {
334      return new ExecutorLua2ret<T, ret, type0, type1>(this->functionPointer);
335    }
336  private:
337    ret           (T::*functionPointer)(type0, type1);
338};
339
340#endif /* _EXECUTOR_LUA_H */
Note: See TracBrowser for help on using the repository browser.