From ed6141c48fde7ca0d2cec97e3ce362bc2053c6fc Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Wed, 11 Oct 2006 05:18:12 +0000 Subject: [PATCH] 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.) --- stepmania/src/EnumHelper.cpp | 10 ++++++++-- stepmania/src/EnumHelper.h | 6 +++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/stepmania/src/EnumHelper.cpp b/stepmania/src/EnumHelper.cpp index 06d6b4a0e5..bda233d950 100644 --- a/stepmania/src/EnumHelper.cpp +++ b/stepmania/src/EnumHelper.cpp @@ -3,10 +3,16 @@ #include "LuaManager.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) ) - return iInvalid; + { + if( bAllowInvalid ) + return iInvalid; + + LuaHelpers::Push( L, ssprintf("Expected %s; got nil", szType) ); + lua_error( L ); + } iPos = LuaHelpers::AbsIndex( L, iPos ); diff --git a/stepmania/src/EnumHelper.h b/stepmania/src/EnumHelper.h index cb7ddc9d22..1e8fd4053b 100644 --- a/stepmania/src/EnumHelper.h +++ b/stepmania/src/EnumHelper.h @@ -12,7 +12,7 @@ extern "C" #define FOREACH_ENUM_N( e, max, var ) for( e var=(e)0; var( var, +1 ) ) #define FOREACH_ENUM( e, var ) for( e var=(e)0; var( 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 struct EnumTraits @@ -28,9 +28,9 @@ template LuaReference EnumTraits::EnumToString; namespace Enum { template - static T Check( lua_State *L, int iPos ) + static T Check( lua_State *L, int iPos, bool bAllowInvalid = false ) { - return (T) CheckEnum( L, EnumTraits::StringToEnum, iPos, EnumTraits::Invalid, EnumTraits::szName ); + return (T) CheckEnum( L, EnumTraits::StringToEnum, iPos, EnumTraits::Invalid, EnumTraits::szName, bAllowInvalid ); } template static void Push( lua_State *L, T iVal )