From 480052c058f3a8ebc70f024933ba5deb6baf0b08 Mon Sep 17 00:00:00 2001 From: tycho Date: Tue, 19 May 2015 15:25:18 +0100 Subject: Added LuaState support for all integral types All so added error handling for out of range values --- src/Bindings/LuaState.h | 52 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 14 deletions(-) (limited to 'src/Bindings/LuaState.h') diff --git a/src/Bindings/LuaState.h b/src/Bindings/LuaState.h index 5b4ec3ae4..bbd0294ef 100644 --- a/src/Bindings/LuaState.h +++ b/src/Bindings/LuaState.h @@ -187,16 +187,37 @@ public: void Push(cLuaUDPEndpoint * a_UDPEndpoint); // GetStackValue() retrieves the value at a_StackPos, if it is a valid type. If not, a_Value is unchanged. + // Returns whether value was changed // Enum values are clamped to their allowed range. - void GetStackValue(int a_StackPos, AString & a_Value); - void GetStackValue(int a_StackPos, BLOCKTYPE & a_Value); - void GetStackValue(int a_StackPos, bool & a_Value); - void GetStackValue(int a_StackPos, cPluginManager::CommandResult & a_Result); - void GetStackValue(int a_StackPos, cRef & a_Ref); - void GetStackValue(int a_StackPos, double & a_Value); - void GetStackValue(int a_StackPos, eWeather & a_Value); - void GetStackValue(int a_StackPos, float & a_ReturnedVal); - void GetStackValue(int a_StackPos, int & a_Value); + bool GetStackValue(int a_StackPos, AString & a_Value); + bool GetStackValue(int a_StackPos, bool & a_Value); + bool GetStackValue(int a_StackPos, cPluginManager::CommandResult & a_Result); + bool GetStackValue(int a_StackPos, cRef & a_Ref); + bool GetStackValue(int a_StackPos, double & a_Value); + bool GetStackValue(int a_StackPos, eWeather & a_Value); + bool GetStackValue(int a_StackPos, float & a_ReturnedVal); + + // template to catch all of the various c++ integral types without overload conflicts + template + bool GetStackValue(int a_StackPos, T & a_ReturnedVal, typename std::enable_if::value>::type * unused = nullptr) + { + UNUSED(unused); + if (lua_isnumber(m_LuaState, a_StackPos)) + { + lua_Number Val = tolua_tonumber(m_LuaState, a_StackPos, a_ReturnedVal); + if (Val > std::numeric_limits::max()) + { + return false; + } + if (Val < std::numeric_limits::min()) + { + return false; + } + a_ReturnedVal = static_cast(Val); + return true; + } + return false; + } // Include the auto-generated Push and GetStackValue() functions: #include "LuaState_Declaration.inc" @@ -218,10 +239,13 @@ public: /** Retrieves a list of values from the Lua stack, starting at the specified index. */ template - inline void GetStackValues(int a_StartStackPos, T & a_Ret, Args &&... args) + inline bool GetStackValues(int a_StartStackPos, T & a_Ret, Args &&... args) { - GetStackValue(a_StartStackPos, a_Ret); - GetStackValues(a_StartStackPos + 1, args...); + if (!GetStackValue(a_StartStackPos, a_Ret)) + { + return false; + } + return GetStackValues(a_StartStackPos + 1, args...); } /** Returns true if the specified parameters on the stack are of the specified usertable type; also logs warning if not. Used for static functions */ @@ -349,9 +373,9 @@ protected: /** Variadic template terminator: If there are no more values to get, bail out. This function is not available in the public API, because it's an error to request no values directly; only internal functions can do that. If you get a compile error saying this function is not accessible, check your calling code, you aren't reading any stack values. */ - void GetStackValues(int a_StartingStackPos) + bool GetStackValues(int a_StartingStackPos) { - // Do nothing + return true; } /** Pushes the function of the specified name onto the stack. -- cgit v1.2.3