By default, don't return Invalid values from Enum::Check. It's the exception
that Invalid has a meaning for an API call; most do not. As it was, every function that used Enum::Check should have been checking for invalid return values; this way, the check can be omitted. (This applies to FromString, Pop and FromStack, too; I havn't decided whether I want to propagate this into those.)
This commit is contained in:
@@ -3,11 +3,17 @@
|
|||||||
#include "LuaManager.h"
|
#include "LuaManager.h"
|
||||||
#include "RageUtil.h"
|
#include "RageUtil.h"
|
||||||
|
|
||||||
int CheckEnum( lua_State *L, LuaReference &table, int iPos, int iInvalid, const char *szType )
|
int CheckEnum( lua_State *L, LuaReference &table, int iPos, int iInvalid, const char *szType, bool bAllowInvalid )
|
||||||
{
|
{
|
||||||
if( lua_isnil(L, iPos) )
|
if( lua_isnil(L, iPos) )
|
||||||
|
{
|
||||||
|
if( bAllowInvalid )
|
||||||
return iInvalid;
|
return iInvalid;
|
||||||
|
|
||||||
|
LuaHelpers::Push( L, ssprintf("Expected %s; got nil", szType) );
|
||||||
|
lua_error( L );
|
||||||
|
}
|
||||||
|
|
||||||
iPos = LuaHelpers::AbsIndex( L, iPos );
|
iPos = LuaHelpers::AbsIndex( L, iPos );
|
||||||
|
|
||||||
table.PushSelf( L );
|
table.PushSelf( L );
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ extern "C"
|
|||||||
#define FOREACH_ENUM_N( e, max, var ) for( e var=(e)0; var<max; enum_add<e>( var, +1 ) )
|
#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 ) )
|
#define FOREACH_ENUM( e, var ) for( e var=(e)0; var<NUM_##e; enum_add<e>( var, +1 ) )
|
||||||
|
|
||||||
int CheckEnum( lua_State *L, LuaReference &table, int iPos, int iInvalid, const char *szType );
|
int CheckEnum( lua_State *L, LuaReference &table, int iPos, int iInvalid, const char *szType, bool bAllowInvalid );
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct EnumTraits
|
struct EnumTraits
|
||||||
@@ -28,9 +28,9 @@ template<typename T> LuaReference EnumTraits<T>::EnumToString;
|
|||||||
namespace Enum
|
namespace Enum
|
||||||
{
|
{
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static T Check( lua_State *L, int iPos )
|
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 );
|
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 )
|
||||||
|
|||||||
Reference in New Issue
Block a user