|
Lua_icxx
1.02 (Aug 2011)
|
00001 /* 00002 This file is part of the Lua_icxx library. 00003 Copyright 2010 (c) by Oliver Schoenborn. 00004 License terms in LICENSE.txt. 00005 */ 00006 00007 00008 #ifndef LUA_ICXX_ADAPTERS_INCLUDED 00009 #define LUA_ICXX_ADAPTERS_INCLUDED 00010 00011 00012 #include <lua.hpp> 00013 #include <string> 00014 00015 00016 // Use a template class so that get/put of number/string can be based on 00017 // type specified by caller 00018 template <typename TT> 00019 inline TT getObjFromStack(lua_State* lua, int stackPos = -1); 00020 00021 static const int LUA_UNACCEPTABLE_STACK_POS = 0; 00022 00023 // use specializations for individual types for getting values from stack (but no pop) 00024 #define LUA_C_TYPE_ADAPTER_TO(TT, FN) \ 00025 template <> \ 00026 inline TT \ 00027 getObjFromStack<TT>(lua_State* lua, int stackPos) {\ 00028 assert(lua); \ 00029 return stackPos == LUA_UNACCEPTABLE_STACK_POS ? TT(0) : (TT)FN(lua, stackPos);\ 00030 } 00031 00032 #pragma warning(push) 00033 #pragma warning(disable:4800) 00034 LUA_C_TYPE_ADAPTER_TO(bool, lua_toboolean) 00035 #pragma warning(pop) 00036 LUA_C_TYPE_ADAPTER_TO(int, lua_tointeger) 00037 LUA_C_TYPE_ADAPTER_TO(long, lua_tointeger) 00038 LUA_C_TYPE_ADAPTER_TO(float, lua_tonumber) 00039 LUA_C_TYPE_ADAPTER_TO(double, lua_tonumber) 00040 // char* / string need special handling 00041 template <> inline 00042 const char* 00043 getObjFromStack<const char*>(lua_State* lua, int stackPos) 00044 { 00045 assert(lua); 00046 if (stackPos == LUA_UNACCEPTABLE_STACK_POS) return ""; 00047 const char* str = lua_tostring(lua, stackPos); 00048 return ( str == NULL ? "" : str ); 00049 } 00050 template <> inline 00051 std::string 00052 getObjFromStack<std::string>(lua_State* lua, int stackPos) 00053 { 00054 return getObjFromStack<const char*>(lua, stackPos); 00055 } 00056 00057 00058 inline void pushValToStack(lua_State* L, bool value) { assert( lua_checkstack(L, 1) ); lua_pushboolean(L, value); } 00059 inline void pushValToStack(lua_State* L, int value) { assert( lua_checkstack(L, 1) ); lua_pushinteger(L, value); } 00060 inline void pushValToStack(lua_State* L, long value) { assert( lua_checkstack(L, 1) ); lua_pushinteger(L, value); } 00061 inline void pushValToStack(lua_State* L, float value) { assert( lua_checkstack(L, 1) ); lua_pushnumber (L, value); } 00062 inline void pushValToStack(lua_State* L, double value) { assert( lua_checkstack(L, 1) ); lua_pushnumber (L, value); } 00063 inline void pushValToStack(lua_State* L, const char* str) { assert( lua_checkstack(L, 1) ); lua_pushstring (L, str); } 00064 inline void pushValToStack(lua_State* L, const std::string& str) { assert( lua_checkstack(L, 1) ); lua_pushstring (L, str.c_str()); } 00065 00066 00067 #endif // LUA_ICXX_ADAPTERS_INCLUDED