| 1 | /*! |
|---|
| 2 | * @file executor_lua_state.h |
|---|
| 3 | * Definition of a Executor that takes lua_State* as input. |
|---|
| 4 | */ |
|---|
| 5 | |
|---|
| 6 | /* |
|---|
| 7 | orxonox - the future of 3D-vertical-scrollers |
|---|
| 8 | |
|---|
| 9 | Copyright (C) 2004 orx |
|---|
| 10 | |
|---|
| 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. |
|---|
| 15 | |
|---|
| 16 | ### File Specific: |
|---|
| 17 | main-programmer: Benjamin Grauer |
|---|
| 18 | co-programmer: ... |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | #ifndef __EXECUTOR_LUA_STATE_H_ |
|---|
| 24 | #define __EXECUTOR_LUA_STATE_H_ |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | #include "executor/executor_generic.h" |
|---|
| 28 | |
|---|
| 29 | #include "luaincl.h" |
|---|
| 30 | |
|---|
| 31 | #ifdef FUNCTOR_CALL_TYPE |
|---|
| 32 | #undef FUNCTOR_CALL_TYPE |
|---|
| 33 | #endif |
|---|
| 34 | //! Define the Functor call type as lua_State*. |
|---|
| 35 | #define FUNCTOR_CALL_TYPE lua_State* |
|---|
| 36 | |
|---|
| 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 | */ |
|---|
| 43 | template<typename type> type fromLua(lua_State* state, int index); |
|---|
| 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); }; |
|---|
| 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); |
|---|
| 56 | |
|---|
| 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 | */ |
|---|
| 63 | template<typename type> void toLua(lua_State* state, type value); |
|---|
| 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()); } |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | //! A Class, that evaluates a lua_State and converts indices into different Types. |
|---|
| 80 | template<> class ExecutorEvaluater <lua_State*> |
|---|
| 81 | { |
|---|
| 82 | public: |
|---|
| 83 | /** @brief Executes the Evaluater |
|---|
| 84 | * @param CallValue the Value that should be converted |
|---|
| 85 | * @param defaults the default Values. |
|---|
| 86 | */ |
|---|
| 87 | template <typename ToType, int index> |
|---|
| 88 | static ToType getValue(lua_State*& CallValue, const MultiType* const defaults) |
|---|
| 89 | { |
|---|
| 90 | return (fromLua<ToType>(CallValue, index+1)); |
|---|
| 91 | } |
|---|
| 92 | /** |
|---|
| 93 | * @param state the state to write into |
|---|
| 94 | * @param value the Value to write there. |
|---|
| 95 | */ |
|---|
| 96 | template <typename FromType> |
|---|
| 97 | static void storeRet(lua_State*& state, FromType value) |
|---|
| 98 | { |
|---|
| 99 | toLua<FromType>(state, value); |
|---|
| 100 | } |
|---|
| 101 | /** @returns the Null Value of a lua_State*, namely (pointer-type) NULL */ |
|---|
| 102 | static lua_State*& defaultValue() { static lua_State* nullState; return nullState; }; |
|---|
| 103 | }; |
|---|
| 104 | |
|---|
| 105 | #endif /* __EXECUTOR_LUA_STATE_H_ */ |
|---|