/*! * @file executor_lua_state.h * Definition of a Executor that takes lua_State* as input. */ /* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Benjamin Grauer co-programmer: ... */ #ifndef __EXECUTOR_LUA_STATE_H_ #define __EXECUTOR_LUA_STATE_H_ #include "executor/executor_generic.h" #include "luaincl.h" #ifdef FUNCTOR_CALL_TYPE #undef FUNCTOR_CALL_TYPE #endif //! Define the Functor call type as lua_State*. #define FUNCTOR_CALL_TYPE lua_State* /** * @brief Converts a lua_State into any type. * @param state the State to get the value from. * @param index the position inside of the lua_State to get the value from. * @returns The value if found. */ template type fromLua(lua_State* state, int index); /** @see template inline type fromLua(lua_State* state, int index) */ template<> inline bool fromLua(lua_State* state, int index) { return lua_toboolean(state, index); }; /** @see template inline type fromLua(lua_State* state, int index) */ template<> inline int fromLua(lua_State* state, int index) { return (int)lua_tonumber(state, index); }; /** @see template inline type fromLua(lua_State* state, int index) */ template<> inline unsigned int fromLua(lua_State* state, int index) { return (unsigned int)lua_tonumber(state, index); }; /** @see template inline type fromLua(lua_State* state, int index) */ template<> inline float fromLua(lua_State* state, int index) { return (float)lua_tonumber(state, index); }; /** @see template inline type fromLua(lua_State* state, int index) */ template<> inline char fromLua(lua_State* state, int index) { return (char)lua_tonumber(state, index); }; /** @see template inline type fromLua(lua_State* state, int index) */ template<> const std::string& fromLua(lua_State* state, int index); /** * @brief writes a value into a lua_State. * @param state the state to write into. * @param value the Value of type to write into the State. */ template void toLua(lua_State* state, type value); /** @see template void toLua(lua_State* state, type value) */ template<> inline void toLua(lua_State* state, bool value) { lua_pushboolean(state, (int) value); }; /** @see template void toLua(lua_State* state, type value) */ template<> inline void toLua(lua_State* state, int value) { lua_pushnumber(state, (lua_Number) value); }; /** @see template void toLua(lua_State* state, type value) */ template<> inline void toLua(lua_State* state, unsigned int value){ lua_pushnumber(state, (lua_Number) value); }; /** @see template void toLua(lua_State* state, type value) */ template<> inline void toLua(lua_State* state, float value) { lua_pushnumber(state, (lua_Number) value); }; /** @see template void toLua(lua_State* state, type value) */ template<> inline void toLua(lua_State* state, char value) { lua_pushnumber(state, (lua_Number) value); }; /** @see template void toLua(lua_State* state, type value) */ template<> inline void toLua(lua_State* state, const std::string& value) { lua_pushstring (state, value.c_str()); } //! A Class, that evaluates a lua_State and converts indices into different Types. template<> class ExecutorEvaluater { public: /** @brief Executes the Evaluater * @param CallValue the Value that should be converted * @param defaults the default Values. */ template static ToType getValue(lua_State*& CallValue, const MultiType* const defaults) { return (fromLua(CallValue, index+1)); } /** * @param state the state to write into * @param value the Value to write there. */ template static void storeRet(lua_State*& state, FromType value) { toLua(state, value); } /** @returns the Null Value of a lua_State*, namely (pointer-type) NULL */ static lua_State*& defaultValue() { static lua_State* nullState; return nullState; }; }; #endif /* __EXECUTOR_LUA_STATE_H_ */