revert 'add Enum:ToString'

This commit is contained in:
Chris Danford
2008-08-20 01:12:59 +00:00
parent feed4c802a
commit f4ef5a7e5d
2 changed files with 19 additions and 46 deletions
+5 -21
View File
@@ -99,24 +99,11 @@ namespace
return 1; 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[] = { static const luaL_Reg EnumLib[] = {
{ "GetName", GetName }, { "GetName", GetName },
{ "Reverse", Reverse }, { "Reverse", Reverse },
{ "ToString", ToString },
{ NULL, NULL } { NULL, NULL }
}; };
@@ -126,17 +113,14 @@ static void PushEnumMethodTable( lua_State *L )
} }
/* Set up the enum table on the stack, and pop the table. */ /* 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 ); EnumTable.PushSelf( L );
lua_newtable( L );
{ {
EnumStringToZeroIndex.PushSelf( L ); lua_newtable( L );
EnumIndexTable.PushSelf( L );
lua_setfield( L, -2, "reverse" ); lua_setfield( L, -2, "reverse" );
EnumStringToString.PushSelf( L );
lua_setfield( L, -2, "tostring" );
lua_pushstring( L, szName ); lua_pushstring( L, szName );
lua_setfield( L, -2, "name" ); 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_setfield( L, -2, "__type" ); // for luaL_pushtype
} }
lua_setmetatable( L, -2 ); lua_setmetatable( L, -2 );
lua_pop( L, 1 ); lua_pop( L, 2 );
} }
/* /*
+14 -25
View File
@@ -18,22 +18,20 @@ int CheckEnum( lua_State *L, LuaReference &table, int iPos, int iInvalid, const
template<typename T> template<typename T>
struct EnumTraits struct EnumTraits
{ {
static LuaReference IndexToEnumString; static LuaReference StringToEnum;
static LuaReference EnumStringToZeroIndex; static LuaReference EnumToString;
static LuaReference EnumStringToString;
static T Invalid; static T Invalid;
static const char *szName; static const char *szName;
}; };
template<typename T> LuaReference EnumTraits<T>::IndexToEnumString; template<typename T> LuaReference EnumTraits<T>::StringToEnum;
template<typename T> LuaReference EnumTraits<T>::EnumStringToZeroIndex; template<typename T> LuaReference EnumTraits<T>::EnumToString;
template<typename T> LuaReference EnumTraits<T>::EnumStringToString; // same as XToString
namespace Enum namespace Enum
{ {
template<typename T> template<typename T>
static T Check( lua_State *L, int iPos, bool bAllowInvalid = false ) static T Check( lua_State *L, int iPos, bool bAllowInvalid = false )
{ {
return (T) CheckEnum( L, EnumTraits<T>::EnumStringToZeroIndex, iPos, EnumTraits<T>::Invalid, EnumTraits<T>::szName, bAllowInvalid ); return (T) CheckEnum( L, EnumTraits<T>::StringToEnum, iPos, EnumTraits<T>::Invalid, EnumTraits<T>::szName, bAllowInvalid );
} }
template<typename T> template<typename T>
static void Push( lua_State *L, T iVal ) static void Push( lua_State *L, T iVal )
@@ -46,12 +44,12 @@ namespace Enum
} }
/* Look up the string value. */ /* Look up the string value. */
EnumTraits<T>::IndexToEnumString.PushSelf( L ); EnumTraits<T>::EnumToString.PushSelf( L );
lua_rawgeti( L, -1, iVal + 1 ); lua_rawgeti( L, -1, iVal + 1 );
lua_remove( L, -2 ); 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<RString> *pNameCache ); // XToString helper const RString &EnumToString( int iVal, int iMax, const char **szNameArray, auto_ptr<RString> *pNameCache ); // XToString helper
@@ -96,7 +94,7 @@ const RString &EnumToString( int iVal, int iMax, const char **szNameArray, auto_
template struct EnumTraits<X>; \ template struct EnumTraits<X>; \
static void Lua##X(lua_State* L) \ 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 ); \ lua_newtable( L ); \
FOREACH_ENUM( X, i ) \ FOREACH_ENUM( X, i ) \
{ \ { \
@@ -104,10 +102,10 @@ static void Lua##X(lua_State* L) \
lua_pushstring( L, (#X "_")+s ); \ lua_pushstring( L, (#X "_")+s ); \
lua_rawseti( L, -2, i+1 ); /* 1-based */ \ lua_rawseti( L, -2, i+1 ); /* 1-based */ \
} \ } \
EnumTraits<X>::IndexToEnumString.SetFromStack( L ); \ EnumTraits<X>::EnumToString.SetFromStack( L ); \
EnumTraits<X>::IndexToEnumString.PushSelf( L ); \ EnumTraits<X>::EnumToString.PushSelf( L ); \
lua_setglobal( L, #X ); \ 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 ); \ lua_newtable( L ); \
FOREACH_ENUM( X, i ) \ FOREACH_ENUM( X, i ) \
{ \ { \
@@ -116,18 +114,9 @@ static void Lua##X(lua_State* L) \
lua_pushnumber( L, i ); /* 0-based */ \ lua_pushnumber( L, i ); /* 0-based */ \
lua_rawset( L, -3 ); \ lua_rawset( L, -3 ); \
} \ } \
EnumTraits<X>::EnumStringToZeroIndex.SetFromStack( L ); \ EnumTraits<X>::StringToEnum.SetFromStack( L ); \
/* Create the EnumStringToString table: { "UnlockEntry_ArcadePoints" = "ArcadePoints", "UnlockEntry_DancePoints" = "DancePoints" } */ \ EnumTraits<X>::StringToEnum.PushSelf( L ); \
lua_newtable( L ); \ Enum::SetMetatable( L, EnumTraits<X>::EnumToString, EnumTraits<X>::StringToEnum, #X ); \
FOREACH_ENUM( X, i ) \
{ \
RString s = X##ToString( i ); \
lua_pushstring( L, (#X "_")+s ); \
lua_pushstring( L, s ); \
lua_rawset( L, -3 ); \
} \
EnumTraits<X>::EnumStringToString.SetFromStack( L ); \
Enum::SetMetatable( L, EnumTraits<X>::IndexToEnumString, EnumTraits<X>::EnumStringToZeroIndex, EnumTraits<X>::EnumStringToString, #X ); \
} \ } \
REGISTER_WITH_LUA_FUNCTION( Lua##X ); \ REGISTER_WITH_LUA_FUNCTION( Lua##X ); \
template<> X EnumTraits<X>::Invalid = X##_Invalid; \ template<> X EnumTraits<X>::Invalid = X##_Invalid; \