diff --git a/stepmania/src/EnumHelper.cpp b/stepmania/src/EnumHelper.cpp index 592d047ab7..fa2111912f 100644 --- a/stepmania/src/EnumHelper.cpp +++ b/stepmania/src/EnumHelper.cpp @@ -99,24 +99,11 @@ namespace return 1; } - - int ToString( lua_State *L ) - { - luaL_checktype( L, 1, LUA_TTABLE ); - - /* Look up the tostring table. If there is no metafield, then we were - * called on the wrong type. */ - if( !luaL_getmetafield(L, 1, "tostring") ) - luaL_typerror( L, 1, "enum" ); - - return 1; - } } static const luaL_Reg EnumLib[] = { { "GetName", GetName }, { "Reverse", Reverse }, - { "ToString", ToString }, { NULL, NULL } }; @@ -126,17 +113,14 @@ static void PushEnumMethodTable( lua_State *L ) } /* Set up the enum table on the stack, and pop the table. */ -void Enum::SetMetatable( lua_State *L, LuaReference &IndexToEnumString, LuaReference &EnumStringToZeroIndex, LuaReference &EnumStringToString, const char *szName ) +void Enum::SetMetatable( lua_State *L, LuaReference &EnumTable, LuaReference &EnumIndexTable, const char *szName ) { - IndexToEnumString.PushSelf( L ); - lua_newtable( L ); + EnumTable.PushSelf( L ); { - EnumStringToZeroIndex.PushSelf( L ); + lua_newtable( L ); + EnumIndexTable.PushSelf( L ); lua_setfield( L, -2, "reverse" ); - EnumStringToString.PushSelf( L ); - lua_setfield( L, -2, "tostring" ); - lua_pushstring( L, szName ); lua_setfield( L, -2, "name" ); @@ -148,7 +132,7 @@ void Enum::SetMetatable( lua_State *L, LuaReference &IndexToEnumString, LuaRefer lua_setfield( L, -2, "__type" ); // for luaL_pushtype } lua_setmetatable( L, -2 ); - lua_pop( L, 1 ); + lua_pop( L, 2 ); } /* diff --git a/stepmania/src/EnumHelper.h b/stepmania/src/EnumHelper.h index b3e93f13fd..710da063eb 100644 --- a/stepmania/src/EnumHelper.h +++ b/stepmania/src/EnumHelper.h @@ -18,22 +18,20 @@ int CheckEnum( lua_State *L, LuaReference &table, int iPos, int iInvalid, const template struct EnumTraits { - static LuaReference IndexToEnumString; - static LuaReference EnumStringToZeroIndex; - static LuaReference EnumStringToString; + static LuaReference StringToEnum; + static LuaReference EnumToString; static T Invalid; static const char *szName; }; -template LuaReference EnumTraits::IndexToEnumString; -template LuaReference EnumTraits::EnumStringToZeroIndex; -template LuaReference EnumTraits::EnumStringToString; // same as XToString +template LuaReference EnumTraits::StringToEnum; +template LuaReference EnumTraits::EnumToString; namespace Enum { template static T Check( lua_State *L, int iPos, bool bAllowInvalid = false ) { - return (T) CheckEnum( L, EnumTraits::EnumStringToZeroIndex, iPos, EnumTraits::Invalid, EnumTraits::szName, bAllowInvalid ); + return (T) CheckEnum( L, EnumTraits::StringToEnum, iPos, EnumTraits::Invalid, EnumTraits::szName, bAllowInvalid ); } template static void Push( lua_State *L, T iVal ) @@ -46,12 +44,12 @@ namespace Enum } /* Look up the string value. */ - EnumTraits::IndexToEnumString.PushSelf( L ); + EnumTraits::EnumToString.PushSelf( L ); lua_rawgeti( L, -1, iVal + 1 ); lua_remove( L, -2 ); } - void SetMetatable( lua_State *L, LuaReference &EnumTable, LuaReference &EnumIndexTable, LuaReference &EnumStringToString, const char *szName ); + void SetMetatable( lua_State *L, LuaReference &EnumTable, LuaReference &EnumIndexTable, const char *szName ); }; const RString &EnumToString( int iVal, int iMax, const char **szNameArray, auto_ptr *pNameCache ); // XToString helper @@ -96,7 +94,7 @@ const RString &EnumToString( int iVal, int iMax, const char **szNameArray, auto_ template struct EnumTraits; \ static void Lua##X(lua_State* L) \ { \ - /* Create the IndexToEnumString table: { 1 = "UnlockEntry_ArcadePoints", 2 = "UnlockEntry_DancePoints" } */ \ + /* Create the EnumToString table: { "UnlockEntry_ArcadePoints", "UnlockEntry_DancePoints" } */ \ lua_newtable( L ); \ FOREACH_ENUM( X, i ) \ { \ @@ -104,10 +102,10 @@ static void Lua##X(lua_State* L) \ lua_pushstring( L, (#X "_")+s ); \ lua_rawseti( L, -2, i+1 ); /* 1-based */ \ } \ - EnumTraits::IndexToEnumString.SetFromStack( L ); \ - EnumTraits::IndexToEnumString.PushSelf( L ); \ + EnumTraits::EnumToString.SetFromStack( L ); \ + EnumTraits::EnumToString.PushSelf( L ); \ lua_setglobal( L, #X ); \ - /* Create the EnumStringToZeroIndex table: { "UnlockEntry_ArcadePoints" = 0, "UnlockEntry_DancePoints" = 1 } */ \ + /* Create the StringToEnum table: { "UnlockEntry_ArcadePoints" = 0, "UnlockEntry_DancePoints" = 1 } */ \ lua_newtable( L ); \ FOREACH_ENUM( X, i ) \ { \ @@ -116,18 +114,9 @@ static void Lua##X(lua_State* L) \ lua_pushnumber( L, i ); /* 0-based */ \ lua_rawset( L, -3 ); \ } \ - EnumTraits::EnumStringToZeroIndex.SetFromStack( L ); \ - /* Create the EnumStringToString table: { "UnlockEntry_ArcadePoints" = "ArcadePoints", "UnlockEntry_DancePoints" = "DancePoints" } */ \ - lua_newtable( L ); \ - FOREACH_ENUM( X, i ) \ - { \ - RString s = X##ToString( i ); \ - lua_pushstring( L, (#X "_")+s ); \ - lua_pushstring( L, s ); \ - lua_rawset( L, -3 ); \ - } \ - EnumTraits::EnumStringToString.SetFromStack( L ); \ - Enum::SetMetatable( L, EnumTraits::IndexToEnumString, EnumTraits::EnumStringToZeroIndex, EnumTraits::EnumStringToString, #X ); \ + EnumTraits::StringToEnum.SetFromStack( L ); \ + EnumTraits::StringToEnum.PushSelf( L ); \ + Enum::SetMetatable( L, EnumTraits::EnumToString, EnumTraits::StringToEnum, #X ); \ } \ REGISTER_WITH_LUA_FUNCTION( Lua##X ); \ template<> X EnumTraits::Invalid = X##_Invalid; \