diff options
author | Mattes D <github@xoft.cz> | 2014-10-15 18:50:32 +0200 |
---|---|---|
committer | Mattes D <github@xoft.cz> | 2014-10-15 19:18:23 +0200 |
commit | f58d44ea503c470558dbbaaf6c4e9a5aaac5718d (patch) | |
tree | fdc0f177938257b41bc871c65b8241936799d2f9 | |
parent | Functions in cPluginManager get references instead of pointers. (diff) | |
download | cuberite-f58d44ea503c470558dbbaaf6c4e9a5aaac5718d.tar cuberite-f58d44ea503c470558dbbaaf6c4e9a5aaac5718d.tar.gz cuberite-f58d44ea503c470558dbbaaf6c4e9a5aaac5718d.tar.bz2 cuberite-f58d44ea503c470558dbbaaf6c4e9a5aaac5718d.tar.lz cuberite-f58d44ea503c470558dbbaaf6c4e9a5aaac5718d.tar.xz cuberite-f58d44ea503c470558dbbaaf6c4e9a5aaac5718d.tar.zst cuberite-f58d44ea503c470558dbbaaf6c4e9a5aaac5718d.zip |
-rw-r--r-- | src/Bindings/LuaState.h | 78 |
1 files changed, 75 insertions, 3 deletions
diff --git a/src/Bindings/LuaState.h b/src/Bindings/LuaState.h index ef87c3efc..b703959e0 100644 --- a/src/Bindings/LuaState.h +++ b/src/Bindings/LuaState.h @@ -240,10 +240,24 @@ public: /** Retrieve value at a_StackPos, if it is a valid cWorld class. If not, a_Value is unchanged */ void GetStackValue(int a_StackPos, pWorld & a_Value); + /** Call the specified Lua function. + Returns true if call succeeded, false if there was an error. + A special param of cRet & signifies the end of param list and the start of return values. + Example call: Call(Fn, Param1, Param2, Param3, cLuaState::Return, Ret1, Ret2) */ + template <typename FnT, typename... Args> + bool Call(const FnT & a_Function, Args &... args) + { + PushFunction(a_Function); + return PushCallPop(args...); + } - // Include the cLuaState::Call() overload implementation that is generated by the gen_LuaState_Call.lua script: - #include "LuaState_Call.inc" - + /** Retrieves a list of values from the Lua stack, starting at the specified index. */ + template <typename T, typename... Args> + inline void GetStackValues(int a_StartStackPos, T & a_Ret, Args &... args) + { + GetStackValue(a_StartStackPos, a_Ret); + 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 */ bool CheckParamUserTable(int a_StartParam, const char * a_UserTable, int a_EndParam = -1); @@ -310,6 +324,7 @@ public: static void LogStack(lua_State * a_LuaState, const char * a_Header = NULL); protected: + lua_State * m_LuaState; /** If true, the state is owned by this object and will be auto-Closed. False => attached state */ @@ -327,6 +342,63 @@ protected: int m_NumCurrentFunctionArgs; + /** Variadic template terminator: Counting zero args returns zero. */ + int CountArgs(void) + { + return 0; + } + + /** Variadic template: Counting args means add one to the count of the rest. */ + template <typename T, typename... Args> + int CountArgs(T, Args... args) + { + return 1 + CountArgs(args...); + } + + /** Variadic template terminator: If there's nothing more to push / pop, just call the function. + Note that there are no return values either, because those are prefixed by a cRet value, so the arg list is never empty. */ + bool PushCallPop(void) + { + return CallFunction(0); + } + + /** Variadic template recursor: More params to push. Push them and recurse. */ + template<typename T, typename... Args> + inline bool PushCallPop(T a_Param, Args &... args) + { + Push(a_Param); + return PushCallPop(args...); + } + + /** Variadic template terminator: If there's nothing more to push, but return values to collect, call the function and collect the returns. */ + template <typename... Args> + bool PushCallPop(cLuaState::cRet, Args &... args) + { + // Calculate the number of return values (number of args left): + int NumReturns = CountArgs(args...); + + // Call the function: + if (!CallFunction(NumReturns)) + { + return false; + } + + // Collect the return values: + GetStackValues(-NumReturns, args...); + lua_pop(m_LuaState, NumReturns); + + // All successful: + return true; + } + + /** 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) + { + // Do nothing + } + /** Pushes the function of the specified name onto the stack. Returns true if successful. Logs a warning on failure (incl. m_SubsystemName) */ |