summaryrefslogtreecommitdiffstats
path: root/src/Bindings/LuaState.h
diff options
context:
space:
mode:
authorMattes D <github@xoft.cz>2014-10-16 18:47:11 +0200
committerMattes D <github@xoft.cz>2014-10-16 18:47:11 +0200
commit5b9ca4a7b4de6e5a03de23bc629a6e1cf2a390a6 (patch)
treefb9cb1f999714970ecac76c8b0ad3f260dad53a8 /src/Bindings/LuaState.h
parentMerge pull request #1539 from mc-server/DungeonLoot (diff)
parentUse universal references (diff)
downloadcuberite-5b9ca4a7b4de6e5a03de23bc629a6e1cf2a390a6.tar
cuberite-5b9ca4a7b4de6e5a03de23bc629a6e1cf2a390a6.tar.gz
cuberite-5b9ca4a7b4de6e5a03de23bc629a6e1cf2a390a6.tar.bz2
cuberite-5b9ca4a7b4de6e5a03de23bc629a6e1cf2a390a6.tar.lz
cuberite-5b9ca4a7b4de6e5a03de23bc629a6e1cf2a390a6.tar.xz
cuberite-5b9ca4a7b4de6e5a03de23bc629a6e1cf2a390a6.tar.zst
cuberite-5b9ca4a7b4de6e5a03de23bc629a6e1cf2a390a6.zip
Diffstat (limited to 'src/Bindings/LuaState.h')
-rw-r--r--src/Bindings/LuaState.h78
1 files changed, 75 insertions, 3 deletions
diff --git a/src/Bindings/LuaState.h b/src/Bindings/LuaState.h
index ef87c3efc..779760017 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)
*/