diff --git a/stepmania/src/EnumHelper.h b/stepmania/src/EnumHelper.h index 4c190c6d54..fc12c1dfd1 100644 --- a/stepmania/src/EnumHelper.h +++ b/stepmania/src/EnumHelper.h @@ -127,9 +127,8 @@ const RString &EnumToString( int iVal, int iMax, const char **szNameArray, auto_ return (X)(i+1); /*invalid*/ \ } -#define LuaDeclareType(X) \ -namespace LuaHelpers { bool FromStack( lua_State *L, X &Object, int iOffset ); } \ -namespace LuaHelpers { void Push( lua_State *L, const X &Object ); } +// currently unused +#define LuaDeclareType(X) #define LuaXType(X) \ template struct EnumTraits; \ @@ -162,8 +161,8 @@ static void Lua##X(lua_State* L) \ REGISTER_WITH_LUA_FUNCTION( Lua##X ); \ template<> X EnumTraits::Invalid = enum_add2( NUM_##X, 1 ); \ template<> const char *EnumTraits::szName = #X; \ -bool LuaHelpers::FromStack( lua_State *L, X &Object, int iOffset ) { Object = Enum::Check( L, iOffset ); return true; } \ -void LuaHelpers::Push( lua_State *L, const X &Object ) { Enum::Push( L, Object ); } +namespace LuaHelpers { template<> bool FromStack( lua_State *L, X &Object, int iOffset ) { Object = Enum::Check( L, iOffset ); return true; } } \ +namespace LuaHelpers { template<> void Push( lua_State *L, const X &Object ) { Enum::Push( L, Object ); } } #endif diff --git a/stepmania/src/LuaBinding.h b/stepmania/src/LuaBinding.h index 180d558d75..c14bb12f52 100644 --- a/stepmania/src/LuaBinding.h +++ b/stepmania/src/LuaBinding.h @@ -156,7 +156,7 @@ public: void T::PushSelf( lua_State *L ) { Luna::PushObject( L, Luna::m_sClassName, this ); } \ static Luna##T registera##T; \ /* Call PushSelf, so we always call the derived Luna::Push. */ \ - namespace LuaHelpers { template<> void Push( lua_State *L, T *pObject ) { pObject->PushSelf( L ); } } + namespace LuaHelpers { template<> void Push( lua_State *L, T *const &pObject ) { pObject->PushSelf( L ); } } #define DEFINE_METHOD( method_name, expr ) \ static int method_name( T* p, lua_State *L ) { LuaHelpers::Push( L, p->expr ); return 1; } diff --git a/stepmania/src/LuaManager.cpp b/stepmania/src/LuaManager.cpp index c8cf7c2463..02a8f19496 100644 --- a/stepmania/src/LuaManager.cpp +++ b/stepmania/src/LuaManager.cpp @@ -21,6 +21,19 @@ static LuaFunctionList *g_LuaFunctions = NULL; #pragma warning (disable : 4611) #endif +namespace LuaHelpers +{ + template<> void Push( lua_State *L, const bool &Object ); + template<> void Push( lua_State *L, const float &Object ); + template<> void Push( lua_State *L, const int &Object ); + template<> void Push( lua_State *L, const RString &Object ); + + template<> bool FromStack( Lua *L, bool &Object, int iOffset ); + template<> bool FromStack( Lua *L, float &Object, int iOffset ); + template<> bool FromStack( Lua *L, int &Object, int iOffset ); + template<> bool FromStack( Lua *L, RString &Object, int iOffset ); +} + struct ChunkReaderString { ChunkReaderString( const RString &sBuf ): m_sBuf(sBuf) { m_bDone = false; } @@ -68,25 +81,27 @@ void LuaManager::UnsetGlobal( const RString &sName ) LUA->Release( L ); } - -void LuaHelpers::Push( lua_State *L, const bool &Object ) { lua_pushboolean( L, Object ); } -void LuaHelpers::Push( lua_State *L, const float &Object ) { lua_pushnumber( L, Object ); } -void LuaHelpers::Push( lua_State *L, const int &Object ) { lua_pushinteger( L, Object ); } -void LuaHelpers::Push( lua_State *L, const RString &Object ) { lua_pushlstring( L, Object.data(), Object.size() ); } - -bool LuaHelpers::FromStack( Lua *L, bool &Object, int iOffset ) { Object = !!lua_toboolean( L, iOffset ); return true; } -bool LuaHelpers::FromStack( Lua *L, float &Object, int iOffset ) { Object = (float)lua_tonumber( L, iOffset ); return true; } -bool LuaHelpers::FromStack( Lua *L, int &Object, int iOffset ) { Object = lua_tointeger( L, iOffset ); return true; } -bool LuaHelpers::FromStack( Lua *L, RString &Object, int iOffset ) +namespace LuaHelpers { - size_t iLen; - const char *pStr = lua_tolstring( L, iOffset, &iLen ); - if( pStr != NULL ) - Object.assign( pStr, iLen ); - else - Object.clear(); + template<> void Push( lua_State *L, const bool &Object ) { lua_pushboolean( L, Object ); } + template<> void Push( lua_State *L, const float &Object ) { lua_pushnumber( L, Object ); } + template<> void Push( lua_State *L, const int &Object ) { lua_pushinteger( L, Object ); } + template<> void Push( lua_State *L, const RString &Object ) { lua_pushlstring( L, Object.data(), Object.size() ); } - return pStr != NULL; + template<> bool FromStack( Lua *L, bool &Object, int iOffset ) { Object = !!lua_toboolean( L, iOffset ); return true; } + template<> bool FromStack( Lua *L, float &Object, int iOffset ) { Object = (float)lua_tonumber( L, iOffset ); return true; } + template<> bool FromStack( Lua *L, int &Object, int iOffset ) { Object = lua_tointeger( L, iOffset ); return true; } + template<> bool FromStack( Lua *L, RString &Object, int iOffset ) + { + size_t iLen; + const char *pStr = lua_tolstring( L, iOffset, &iLen ); + if( pStr != NULL ) + Object.assign( pStr, iLen ); + else + Object.clear(); + + return pStr != NULL; + } } void LuaHelpers::CreateTableFromArrayB( Lua *L, const vector &aIn ) diff --git a/stepmania/src/LuaManager.h b/stepmania/src/LuaManager.h index 5b0a593d2a..ad0211fea4 100644 --- a/stepmania/src/LuaManager.h +++ b/stepmania/src/LuaManager.h @@ -81,17 +81,10 @@ namespace LuaHelpers void PushValueFunc( lua_State *L, int iArgs ); template - void Push( lua_State *L, T *pObject ); + void Push( lua_State *L, const T &Object ); - void Push( lua_State *L, const bool &Object ); - void Push( lua_State *L, const float &Object ); - void Push( lua_State *L, const int &Object ); - void Push( lua_State *L, const RString &Object ); - - bool FromStack( Lua *L, bool &Object, int iOffset ); - bool FromStack( Lua *L, float &Object, int iOffset ); - bool FromStack( Lua *L, int &Object, int iOffset ); - bool FromStack( Lua *L, RString &Object, int iOffset ); + template + bool FromStack( lua_State *L, T &Object, int iOffset ); template bool Pop( lua_State *L, T &val ) diff --git a/stepmania/src/LuaReference.cpp b/stepmania/src/LuaReference.cpp index 5a95011404..7e4744dc45 100644 --- a/stepmania/src/LuaReference.cpp +++ b/stepmania/src/LuaReference.cpp @@ -163,19 +163,22 @@ RString LuaReference::Serialize() const return sRet; } -bool LuaHelpers::FromStack( lua_State *L, LuaReference &Object, int iOffset ) +namespace LuaHelpers { - lua_pushvalue( L, iOffset ); - Object.SetFromStack( L ); - return true; -} + template<> bool FromStack( lua_State *L, LuaReference &Object, int iOffset ) + { + lua_pushvalue( L, iOffset ); + Object.SetFromStack( L ); + return true; + } -bool LuaHelpers::FromStack( lua_State *L, apActorCommands &Object, int iOffset ) -{ - LuaReference *pRef = new LuaReference; - FromStack( L, *pRef, iOffset ); - Object = apActorCommands( pRef ); - return true; + template<> bool FromStack( lua_State *L, apActorCommands &Object, int iOffset ) + { + LuaReference *pRef = new LuaReference; + FromStack( L, *pRef, iOffset ); + Object = apActorCommands( pRef ); + return true; + } } LuaTable::LuaTable() diff --git a/stepmania/src/LuaReference.h b/stepmania/src/LuaReference.h index 8163445af0..27b8bbe390 100644 --- a/stepmania/src/LuaReference.h +++ b/stepmania/src/LuaReference.h @@ -51,12 +51,6 @@ private: typedef AutoPtrCopyOnWrite apActorCommands; -namespace LuaHelpers -{ - bool FromStack( lua_State *L, LuaReference &Object, int iOffset ); - bool FromStack( lua_State *L, apActorCommands &Object, int iOffset ); -} - class LuaTable: public LuaReference { public: diff --git a/stepmania/src/MessageManager.h b/stepmania/src/MessageManager.h index 7df081cf68..6447432d87 100644 --- a/stepmania/src/MessageManager.h +++ b/stepmania/src/MessageManager.h @@ -3,6 +3,7 @@ #ifndef MessageManager_H #define MessageManager_H +#include "LuaManager.h" struct lua_State; enum Message @@ -201,6 +202,8 @@ public: bool operator != ( const T &other ) const { return val != other; } }; +namespace LuaHelpers { template void Push( lua_State *L, const BroadcastOnChange &Object ) { LuaHelpers::Push( L, Object.Get() ); } } + template class BroadcastOnChange1D { diff --git a/stepmania/src/Preference.h b/stepmania/src/Preference.h index d2b3991e05..683c5c674e 100644 --- a/stepmania/src/Preference.h +++ b/stepmania/src/Preference.h @@ -5,6 +5,7 @@ #include "EnumHelper.h" #include "RageUtil.h" +#include "LuaManager.h" class XNode; struct lua_State; @@ -115,6 +116,8 @@ private: void (*m_pfnValidate)(T& val); }; +namespace LuaHelpers { template void Push( lua_State *L, const Preference &Object ) { LuaHelpers::Push( L, Object.Get() ); } } + template class Preference1D { diff --git a/stepmania/src/RageTypes.cpp b/stepmania/src/RageTypes.cpp index f8c5a38fd8..458ce3deb9 100644 --- a/stepmania/src/RageTypes.cpp +++ b/stepmania/src/RageTypes.cpp @@ -36,10 +36,13 @@ void RageColor::FromStack( lua_State *L, int iPos ) lua_pop( L, 5 ); } -bool LuaHelpers::FromStack( lua_State *L, RageColor &Object, int iOffset ) +namespace LuaHelpers { - Object.FromStack( L, iOffset ); - return true; + template<> bool FromStack( lua_State *L, RageColor &Object, int iOffset ) + { + Object.FromStack( L, iOffset ); + return true; + } } static const char *CullModeNames[] = diff --git a/stepmania/src/RageTypes.h b/stepmania/src/RageTypes.h index 50d191047c..b186c4a800 100644 --- a/stepmania/src/RageTypes.h +++ b/stepmania/src/RageTypes.h @@ -200,11 +200,6 @@ public: float r, g, b, a; } SM_ALIGN(16); -namespace LuaHelpers -{ - bool FromStack( lua_State *L, RageColor &Object, int iOffset ); -} - /* Convert floating-point 0..1 value to integer 0..255 value. * * * As a test case, diff --git a/stepmania/src/ThemeMetric.h b/stepmania/src/ThemeMetric.h index 48a4b4547e..364c2d8876 100644 --- a/stepmania/src/ThemeMetric.h +++ b/stepmania/src/ThemeMetric.h @@ -72,10 +72,9 @@ public: { if( m_sName != "" && THEME && THEME->IsThemeLoaded() ) { - using namespace LuaHelpers; Lua *L = LUA->Get(); THEME->PushMetric( L, m_sGroup, m_sName ); - if( !FromStack(L, m_currentValue, -1) ) + if( !LuaHelpers::FromStack(L, m_currentValue, -1) ) m_currentValue = T(); lua_pop( L, 1 ); LUA->Release(L);