Files
itgmania212121/stepmania/src/EnumHelper.h
T

152 lines
5.3 KiB
C++
Raw Normal View History

2006-09-01 02:09:49 +00:00
#ifndef ENUM_HELPER_H
#define ENUM_HELPER_H
2004-03-29 09:44:00 +00:00
#include "LuaReference.h"
2006-09-27 05:45:54 +00:00
#include "RageUtil.h"
2006-09-28 06:28:46 +00:00
extern "C"
{
#include "lua-5.1/src/lua.h"
}
2006-09-27 05:27:28 +00:00
#define FOREACH_ENUM_N( e, max, var ) for( e var=(e)0; var<max; enum_add<e>( var, +1 ) )
#define FOREACH_ENUM( e, var ) for( e var=(e)0; var<NUM_##e; enum_add<e>( var, +1 ) )
2004-03-29 09:44:00 +00:00
int CheckEnum( lua_State *L, LuaReference &table, int iPos, int iInvalid, const char *szType, bool bAllowInvalid );
template<typename T>
struct EnumTraits
{
static LuaReference StringToEnum;
static LuaReference EnumToString;
static T Invalid;
static const char *szName;
};
2006-10-05 10:32:57 +00:00
template<typename T> LuaReference EnumTraits<T>::StringToEnum;
template<typename T> LuaReference EnumTraits<T>::EnumToString;
namespace Enum
{
template<typename T>
static T Check( lua_State *L, int iPos, bool bAllowInvalid = false )
{
return (T) CheckEnum( L, EnumTraits<T>::StringToEnum, iPos, EnumTraits<T>::Invalid, EnumTraits<T>::szName, bAllowInvalid );
}
template<typename T>
static void Push( lua_State *L, T iVal )
{
/* Enum_Invalid values are nil in Lua. */
if( iVal == EnumTraits<T>::Invalid )
{
lua_pushnil( L );
return;
}
/* Look up the string value. */
EnumTraits<T>::EnumToString.PushSelf( L );
lua_rawgeti( L, -1, iVal + 1 );
2006-09-30 21:55:31 +00:00
lua_remove( L, -2 );
}
2006-10-05 23:08:42 +00:00
void SetMetatable( lua_State *L, LuaReference &EnumTable, LuaReference &EnumIndexTable, const char *szName );
};
2004-03-29 09:44:00 +00:00
2006-09-27 04:19:47 +00:00
const RString &EnumToString( int iVal, int iMax, const char **szNameArray, auto_ptr<RString> *pNameCache ); // XToString helper
2006-10-15 00:09:18 +00:00
#define XToString(X) \
2006-09-27 04:19:47 +00:00
COMPILE_ASSERT( NUM_##X == ARRAYLEN(X##Names) ); \
2006-01-22 01:00:06 +00:00
const RString& X##ToString( X x ) \
2004-03-29 09:44:00 +00:00
{ \
2006-09-27 04:19:47 +00:00
static auto_ptr<RString> as_##X##Name[NUM_##X+2]; \
return EnumToString( x, NUM_##X, X##Names, as_##X##Name ); \
2006-10-07 06:21:39 +00:00
} \
2006-10-07 08:56:12 +00:00
namespace StringConversion { template<> RString ToString<X>( const X &value ) { return X##ToString(value); } }
2004-03-29 09:44:00 +00:00
#define XToLocalizedString(X) \
2006-01-22 01:00:06 +00:00
const RString &X##ToLocalizedString( X x ) \
{ \
static auto_ptr<LocalizedString> g_##X##Name[NUM_##X]; \
2006-07-14 07:49:59 +00:00
if( g_##X##Name[0].get() == NULL ) { \
for( unsigned i = 0; i < NUM_##X; ++i ) \
2006-07-14 07:49:59 +00:00
{ \
auto_ptr<LocalizedString> ap( new LocalizedString(#X, X##ToString((X)i)) ); \
g_##X##Name[i] = ap; \
} \
} \
return g_##X##Name[x]->GetValue(); \
}
2004-03-29 09:44:00 +00:00
#define StringToX(X) \
2006-01-22 01:00:06 +00:00
X StringTo##X( const RString& s ) \
2004-03-29 09:44:00 +00:00
{ \
2006-10-08 04:29:09 +00:00
for( unsigned i = 0; i < ARRAYLEN(X##Names); ++i ) \
if( !s.CompareNoCase(X##Names[i]) ) \
2004-03-29 09:44:00 +00:00
return (X)i; \
2006-10-08 04:29:09 +00:00
return X##_Invalid; \
2006-10-07 06:21:39 +00:00
} \
2006-10-07 08:56:12 +00:00
namespace StringConversion { template<> bool FromString<X>( const RString &sValue, X &out ) { out = StringTo##X(sValue); return out != X##_Invalid; } }
2004-03-29 09:44:00 +00:00
// currently unused
#define LuaDeclareType(X)
2006-09-26 07:13:54 +00:00
#define LuaXType(X) \
template struct EnumTraits<X>; \
2006-09-26 06:36:08 +00:00
static void Lua##X(lua_State* L) \
{ \
/* Create the EnumToString table: { "UnlockEntry_ArcadePoints", "UnlockEntry_DancePoints" } */ \
lua_newtable( L ); \
2006-10-07 08:56:58 +00:00
FOREACH_ENUM( X, i ) \
{ \
2006-09-27 06:09:52 +00:00
RString s = X##ToString( i ); \
2006-09-26 07:13:54 +00:00
lua_pushstring( L, (#X "_")+s ); \
lua_rawseti( L, -2, i+1 ); /* 1-based */ \
} \
EnumTraits<X>::EnumToString.SetFromStack( L ); \
EnumTraits<X>::EnumToString.PushSelf( L ); \
lua_setglobal( L, #X ); \
2006-09-26 04:15:39 +00:00
/* Create the StringToEnum table: { "UnlockEntry_ArcadePoints" = 0, "UnlockEntry_DancePoints" = 1 } */ \
lua_newtable( L ); \
2006-10-07 08:56:58 +00:00
FOREACH_ENUM( X, i ) \
{ \
2006-09-27 06:09:52 +00:00
RString s = X##ToString( i ); \
2006-09-26 07:13:54 +00:00
lua_pushstring( L, (#X "_")+s ); \
lua_pushnumber( L, i ); /* 0-based */ \
lua_rawset( L, -3 ); \
} \
2006-09-26 05:03:55 +00:00
EnumTraits<X>::StringToEnum.SetFromStack( L ); \
EnumTraits<X>::StringToEnum.PushSelf( L ); \
2006-10-05 23:08:42 +00:00
Enum::SetMetatable( L, EnumTraits<X>::EnumToString, EnumTraits<X>::StringToEnum, #X ); \
} \
2006-09-26 06:36:08 +00:00
REGISTER_WITH_LUA_FUNCTION( Lua##X ); \
2006-10-07 04:40:14 +00:00
template<> X EnumTraits<X>::Invalid = X##_Invalid; \
template<> const char *EnumTraits<X>::szName = #X; \
namespace LuaHelpers { template<> bool FromStack<X>( lua_State *L, X &Object, int iOffset ) { Object = Enum::Check<X>( L, iOffset, true ); return Object != EnumTraits<X>::Invalid; } } \
namespace LuaHelpers { template<> void Push<X>( lua_State *L, const X &Object ) { Enum::Push<X>( L, Object ); } }
2004-03-29 09:44:00 +00:00
#endif
2004-05-31 22:42:12 +00:00
/*
2006-09-30 21:55:31 +00:00
* (c) 2004-2006 Chris Danford, Glenn Maynard
2004-05-31 22:42:12 +00:00
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/