[4838] | 1 | /*! |
---|
[9748] | 2 | * @file executor_lua_state.h |
---|
| 3 | * Definition of a Executor that takes lua_State* as input. |
---|
[5391] | 4 | */ |
---|
[1853] | 5 | |
---|
[7716] | 6 | /* |
---|
| 7 | orxonox - the future of 3D-vertical-scrollers |
---|
[1853] | 8 | |
---|
[7716] | 9 | Copyright (C) 2004 orx |
---|
[1853] | 10 | |
---|
[7716] | 11 | This program is free software; you can redistribute it and/or modify |
---|
| 12 | it under the terms of the GNU General Public License as published by |
---|
| 13 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 14 | any later version. |
---|
[5141] | 15 | |
---|
[7716] | 16 | ### File Specific: |
---|
| 17 | main-programmer: Benjamin Grauer |
---|
| 18 | co-programmer: ... |
---|
| 19 | */ |
---|
[5652] | 20 | |
---|
[5328] | 21 | |
---|
[9740] | 22 | |
---|
[9743] | 23 | #ifndef __EXECUTOR_LUA_STATE_H_ |
---|
| 24 | #define __EXECUTOR_LUA_STATE_H_ |
---|
[5641] | 25 | |
---|
[9740] | 26 | |
---|
[9754] | 27 | #include "executor/executor_generic.h" |
---|
[9747] | 28 | |
---|
[9743] | 29 | #include "luaincl.h" |
---|
[9747] | 30 | |
---|
[9745] | 31 | #ifdef FUNCTOR_CALL_TYPE |
---|
| 32 | #undef FUNCTOR_CALL_TYPE |
---|
| 33 | #endif |
---|
[9748] | 34 | //! Define the Functor call type as lua_State*. |
---|
[9745] | 35 | #define FUNCTOR_CALL_TYPE lua_State* |
---|
[5641] | 36 | |
---|
[9752] | 37 | /** |
---|
| 38 | * @brief Converts a lua_State into any type. |
---|
| 39 | * @param state the State to get the value from. |
---|
| 40 | * @param index the position inside of the lua_State to get the value from. |
---|
| 41 | * @returns The value if found. |
---|
| 42 | */ |
---|
[9743] | 43 | template<typename type> type fromLua(lua_State* state, int index); |
---|
[9752] | 44 | /** @see template<typename type> inline type fromLua(lua_State* state, int index) */ |
---|
| 45 | template<> inline bool fromLua<bool>(lua_State* state, int index) { return lua_toboolean(state, index); }; |
---|
| 46 | /** @see template<typename type> inline type fromLua(lua_State* state, int index) */ |
---|
| 47 | template<> inline int fromLua<int>(lua_State* state, int index) { return (int)lua_tonumber(state, index); }; |
---|
| 48 | /** @see template<typename type> inline type fromLua(lua_State* state, int index) */ |
---|
| 49 | template<> inline unsigned int fromLua<unsigned int>(lua_State* state, int index) { return (unsigned int)lua_tonumber(state, index); }; |
---|
| 50 | /** @see template<typename type> inline type fromLua(lua_State* state, int index) */ |
---|
| 51 | template<> inline float fromLua<float>(lua_State* state, int index) { return (float)lua_tonumber(state, index); }; |
---|
| 52 | /** @see template<typename type> inline type fromLua(lua_State* state, int index) */ |
---|
| 53 | template<> inline char fromLua<char>(lua_State* state, int index) { return (char)lua_tonumber(state, index); }; |
---|
[9753] | 54 | /** @see template<typename type> inline type fromLua(lua_State* state, int index) */ |
---|
| 55 | template<> const std::string& fromLua<const std::string&>(lua_State* state, int index); |
---|
[5161] | 56 | |
---|
[9752] | 57 | |
---|
| 58 | /** |
---|
| 59 | * @brief writes a value into a lua_State. |
---|
| 60 | * @param state the state to write into. |
---|
| 61 | * @param value the Value of type to write into the State. |
---|
| 62 | */ |
---|
[9743] | 63 | template<typename type> void toLua(lua_State* state, type value); |
---|
[9752] | 64 | /** @see template<typename type> void toLua(lua_State* state, type value) */ |
---|
| 65 | template<> inline void toLua<bool>(lua_State* state, bool value) { lua_pushboolean(state, (int) value); }; |
---|
| 66 | /** @see template<typename type> void toLua(lua_State* state, type value) */ |
---|
| 67 | template<> inline void toLua<int>(lua_State* state, int value) { lua_pushnumber(state, (lua_Number) value); }; |
---|
| 68 | /** @see template<typename type> void toLua(lua_State* state, type value) */ |
---|
| 69 | template<> inline void toLua<unsigned int>(lua_State* state, unsigned int value){ lua_pushnumber(state, (lua_Number) value); }; |
---|
| 70 | /** @see template<typename type> void toLua(lua_State* state, type value) */ |
---|
| 71 | template<> inline void toLua<float>(lua_State* state, float value) { lua_pushnumber(state, (lua_Number) value); }; |
---|
| 72 | /** @see template<typename type> void toLua(lua_State* state, type value) */ |
---|
| 73 | template<> inline void toLua<char>(lua_State* state, char value) { lua_pushnumber(state, (lua_Number) value); }; |
---|
| 74 | /** @see template<typename type> void toLua(lua_State* state, type value) */ |
---|
| 75 | template<> inline void toLua<const std::string&>(lua_State* state, const std::string& value) { lua_pushstring (state, value.c_str()); } |
---|
[5641] | 76 | |
---|
[9752] | 77 | |
---|
| 78 | |
---|
[9747] | 79 | //! A Class, that evaluates a lua_State and converts indices into different Types. |
---|
[9743] | 80 | template<> class ExecutorEvaluater <lua_State*> |
---|
[9730] | 81 | { |
---|
[9731] | 82 | public: |
---|
[9736] | 83 | /** @brief Executes the Evaluater |
---|
[9735] | 84 | * @param CallValue the Value that should be converted |
---|
| 85 | * @param defaults the default Values. |
---|
| 86 | */ |
---|
[9736] | 87 | template <typename ToType, int index> |
---|
[9747] | 88 | static ToType getValue(lua_State*& CallValue, const MultiType* const defaults) |
---|
[9731] | 89 | { |
---|
[9747] | 90 | return (fromLua<ToType>(CallValue, index+1)); |
---|
[9731] | 91 | } |
---|
[9748] | 92 | /** |
---|
| 93 | * @param state the state to write into |
---|
| 94 | * @param value the Value to write there. |
---|
| 95 | */ |
---|
[9747] | 96 | template <typename FromType> |
---|
| 97 | static void storeRet(lua_State*& state, FromType value) |
---|
[9746] | 98 | { |
---|
[9747] | 99 | toLua<FromType>(state, value); |
---|
[9746] | 100 | } |
---|
[9748] | 101 | /** @returns the Null Value of a lua_State*, namely (pointer-type) NULL */ |
---|
[9743] | 102 | static lua_State*& defaultValue() { static lua_State* nullState; return nullState; }; |
---|
[9730] | 103 | }; |
---|
| 104 | |
---|
[9743] | 105 | #endif /* __EXECUTOR_LUA_STATE_H_ */ |
---|