From e225b7f8262df48ad4d7094bc295add3007b0649 Mon Sep 17 00:00:00 2001 From: peterbell10 Date: Mon, 11 Sep 2017 22:20:49 +0100 Subject: Replace ItemCallbacks with lambdas (#3993) --- src/Bindings/ManualBindings.h | 235 ++++++++++-------------------------------- 1 file changed, 52 insertions(+), 183 deletions(-) (limited to 'src/Bindings/ManualBindings.h') diff --git a/src/Bindings/ManualBindings.h b/src/Bindings/ManualBindings.h index dc9d9462f..0272e993b 100644 --- a/src/Bindings/ManualBindings.h +++ b/src/Bindings/ManualBindings.h @@ -1,4 +1,4 @@ - + // ManualBindings.h // Declares the cManualBindings class used as a namespace for functions exported to the Lua API manually @@ -57,7 +57,7 @@ public: template < class Ty1, class Ty2, - bool (Ty1::*DoWithFn)(const AString &, cItemCallback &) + bool (Ty1::*DoWithFn)(const AString &, cFunctionRef) > static int DoWith(lua_State * tolua_S) { @@ -89,28 +89,14 @@ public: return lua_do_error(tolua_S, "Error in function call '#funcname#': Expected a valid callback function for parameter #2"); } - class cLuaCallback : public cItemCallback - { - public: - cLuaCallback(cLuaState & a_LuaState, cLuaState::cRef & a_FnRef): - m_LuaState(a_LuaState), - m_FnRef(a_FnRef) - { - } - - private: - virtual bool Item(Ty2 * a_Item) override + // Call the DoWith function: + bool res = (Self->*DoWithFn)(ItemName, [&](Ty2 & a_Item) { bool ret = false; - m_LuaState.Call(m_FnRef, a_Item, cLuaState::Return, ret); + L.Call(FnRef, &a_Item, cLuaState::Return, ret); return ret; } - cLuaState & m_LuaState; - cLuaState::cRef & m_FnRef; - } Callback(L, FnRef); - - // Call the DoWith function: - bool res = (Self->*DoWithFn)(ItemName, Callback); + ); // Push the result as the return value: L.Push(res); @@ -125,7 +111,7 @@ public: template < class Ty1, class Ty2, - bool (Ty1::*DoWithFn)(const AString &, cItemCallback &) + bool (Ty1::*DoWithFn)(const AString &, cFunctionRef) > static int StaticDoWith(lua_State * tolua_S) { @@ -152,28 +138,14 @@ public: return lua_do_error(tolua_S, "Error in function call '#funcname#': Expected a valid callback function for parameter #2"); } - class cLuaCallback : public cItemCallback - { - public: - cLuaCallback(cLuaState & a_LuaState, cLuaState::cRef & a_FnRef): - m_LuaState(a_LuaState), - m_FnRef(a_FnRef) - { - } - - private: - virtual bool Item(Ty2 * a_Item) override + // Call the DoWith function: + bool res = (Ty1::Get()->*DoWithFn)(ItemName, [&](Ty2 & a_Item) { bool ret = false; - m_LuaState.Call(m_FnRef, a_Item, cLuaState::Return, ret); + L.Call(FnRef, &a_Item, cLuaState::Return, ret); return ret; } - cLuaState & m_LuaState; - cLuaState::cRef & m_FnRef; - } Callback(L, FnRef); - - // Call the DoWith function: - bool res = (Ty1::Get()->*DoWithFn)(ItemName, Callback); + ); // Push the result as the return value: L.Push(res); @@ -187,7 +159,7 @@ public: template < class Ty1, class Ty2, - bool (Ty1::*DoWithFn)(UInt32, cItemCallback &) + bool (Ty1::*DoWithFn)(UInt32, cFunctionRef) > static int DoWithID(lua_State * tolua_S) { @@ -215,28 +187,14 @@ public: return lua_do_error(tolua_S, "Error in function call '#funcname#': Expected a valid callback function for parameter #2"); } - class cLuaCallback : public cItemCallback - { - public: - cLuaCallback(cLuaState & a_LuaState, cLuaState::cRef & a_FnRef): - m_LuaState(a_LuaState), - m_FnRef(a_FnRef) - { - } - - private: - virtual bool Item(Ty2 * a_Item) override + // Call the DoWith function: + bool res = (Self->*DoWithFn)(ItemID, [&](Ty2 & a_Item) { bool ret = false; - m_LuaState.Call(m_FnRef, a_Item, cLuaState::Return, ret); + L.Call(FnRef, &a_Item, cLuaState::Return, ret); return ret; } - cLuaState & m_LuaState; - cLuaState::cRef & m_FnRef; - } Callback(L, FnRef); - - // Call the DoWith function: - bool res = (Self->*DoWithFn)(ItemID, Callback); + ); // Push the result as the return value: L.Push(res); @@ -251,7 +209,7 @@ public: template < class SELF, class ITEM, - bool (SELF::*DoWithFn)(int, int, int, cItemCallback &) + bool (SELF::*DoWithFn)(int, int, int, cFunctionRef) > static int DoWithXYZ(lua_State * tolua_S) { @@ -282,28 +240,14 @@ public: return lua_do_error(tolua_S, "Error in function call '#funcname#': Expected a valid callback function for parameter #5"); } - class cLuaCallback : public cItemCallback - { - public: - cLuaCallback(cLuaState & a_LuaState, cLuaState::cRef & a_FnRef): - m_LuaState(a_LuaState), - m_FnRef(a_FnRef) - { - } - - private: - virtual bool Item(ITEM * a_Item) override + // Call the DoWith function: + bool res = (Self->*DoWithFn)(BlockX, BlockY, BlockZ, [&](ITEM & a_Item) { bool ret = false; - m_LuaState.Call(m_FnRef, a_Item, cLuaState::Return, ret); + L.Call(FnRef, &a_Item, cLuaState::Return, ret); return ret; } - cLuaState & m_LuaState; - cLuaState::cRef & m_FnRef; - } Callback(L, FnRef); - - // Call the DoWith function: - bool res = (Self->*DoWithFn)(BlockX, BlockY, BlockZ, Callback); + ); // Push the result as the return value: L.Push(res); @@ -318,7 +262,7 @@ public: template < class SELF, class ITEM, - bool (SELF::*DoWithFn)(int, int, int, cItemCallback &), + bool (SELF::*DoWithFn)(int, int, int, cFunctionRef), bool (SELF::*CoordCheckFn)(int, int, int) const > static int DoWithXYZ(lua_State * tolua_S) @@ -356,28 +300,14 @@ public: ).c_str()); } - class cLuaCallback : public cItemCallback - { - public: - cLuaCallback(cLuaState & a_LuaState, cLuaState::cRef & a_FnRef): - m_LuaState(a_LuaState), - m_FnRef(a_FnRef) - { - } - - private: - virtual bool Item(ITEM * a_Item) override + // Call the DoWith function: + bool res = (Self->*DoWithFn)(BlockX, BlockY, BlockZ, [&](ITEM & a_Item) { bool ret = false; - m_LuaState.Call(m_FnRef, a_Item, cLuaState::Return, ret); + L.Call(FnRef, &a_Item, cLuaState::Return, ret); return ret; } - cLuaState & m_LuaState; - cLuaState::cRef & m_FnRef; - } Callback(L, FnRef); - - // Call the DoWith function: - bool res = (Self->*DoWithFn)(BlockX, BlockY, BlockZ, Callback); + ); // Push the result as the return value: L.Push(res); @@ -391,7 +321,7 @@ public: template < class Ty1, class Ty2, - bool (Ty1::*ForEachFn)(int, int, cItemCallback &) + bool (Ty1::*ForEachFn)(int, int, cFunctionRef) > static int ForEachInChunk(lua_State * tolua_S) { @@ -421,28 +351,14 @@ public: return lua_do_error(tolua_S, "Error in function call '#funcname#': Expected a valid callback function for parameter #4"); } - class cLuaCallback : public cItemCallback - { - public: - cLuaCallback(cLuaState & a_LuaState, cLuaState::cRef & a_FnRef): - m_LuaState(a_LuaState), - m_FnRef(a_FnRef) - { - } - - private: - virtual bool Item(Ty2 * a_Item) override + // Call the DoWith function: + bool res = (Self->*ForEachFn)(ChunkX, ChunkZ, [&](Ty2 & a_Item) { bool ret = false; - m_LuaState.Call(m_FnRef, a_Item, cLuaState::Return, ret); + L.Call(FnRef, &a_Item, cLuaState::Return, ret); return ret; } - cLuaState & m_LuaState; - cLuaState::cRef & m_FnRef; - } Callback(L, FnRef); - - // Call the DoWith function: - bool res = (Self->*ForEachFn)(ChunkX, ChunkZ, Callback); + ); // Push the result as the return value: L.Push(res); @@ -456,7 +372,7 @@ public: template < class Ty1, class Ty2, - bool (Ty1::*ForEachFn)(const cBoundingBox &, cItemCallback &) + bool (Ty1::*ForEachFn)(const cBoundingBox &, cFunctionRef) > static int ForEachInBox(lua_State * tolua_S) { @@ -488,36 +404,19 @@ public: return lua_do_error(tolua_S, "Error in function call '#funcname#': Expected a valid callback function for parameter #2"); } - // Callback wrapper for the Lua function: - class cLuaCallback : public cItemCallback - { - public: - cLuaCallback(cLuaState & a_LuaState, cLuaState::cRef & a_FuncRef) : - m_LuaState(a_LuaState), - m_FnRef(a_FuncRef) + bool res = (Self->*ForEachFn)(*Box, [&](Ty2 & a_Item) { - } - - private: - cLuaState & m_LuaState; - cLuaState::cRef & m_FnRef; - - // cItemCallback overrides: - virtual bool Item(Ty2 * a_Item) override - { - bool res = false; - if (!m_LuaState.Call(m_FnRef, a_Item, cLuaState::Return, res)) + bool ret = false; + if (!L.Call(FnRef, &a_Item, cLuaState::Return, ret)) { LOGWARNING("Failed to call Lua callback"); - m_LuaState.LogStackTrace(); + L.LogStackTrace(); return true; // Abort enumeration } - return res; + return ret; } - } Callback(L, FnRef); - - bool res = (Self->*ForEachFn)(*Box, Callback); + ); // Push the result as the return value: L.Push(res); @@ -531,7 +430,7 @@ public: template < class Ty1, class Ty2, - bool (Ty1::*ForEachFn)(cItemCallback &) + bool (Ty1::*ForEachFn)(cFunctionRef) > static int ForEach(lua_State * tolua_S) { @@ -558,29 +457,14 @@ public: return lua_do_error(tolua_S, "Error in function call '#funcname#': Expected a valid callback function for parameter #1"); } - class cLuaCallback : public cItemCallback - { - public: - cLuaCallback(cLuaState & a_LuaState, cLuaState::cRef & a_FnRef): - m_LuaState(a_LuaState), - m_FnRef(a_FnRef) - { - } - - private: - cLuaState & m_LuaState; - cLuaState::cRef & m_FnRef; - - virtual bool Item(Ty2 * a_Item) override + // Call the enumeration: + bool res = (Self->*ForEachFn)([&](Ty2 & a_Item) { - bool res = false; // By default continue the enumeration - m_LuaState.Call(m_FnRef, a_Item, cLuaState::Return, res); - return res; + bool ret = false; // By default continue the enumeration + L.Call(FnRef, &a_Item, cLuaState::Return, ret); + return ret; } - } Callback(L, FnRef); - - // Call the enumeration: - bool res = (Self->*ForEachFn)(Callback); + ); // Push the return value: L.Push(res); @@ -595,7 +479,7 @@ public: template < class Ty1, class Ty2, - bool (Ty1::*ForEachFn)(cItemCallback &) + bool (Ty1::*ForEachFn)(cFunctionRef) > static int StaticForEach(lua_State * tolua_S) { @@ -616,29 +500,14 @@ public: return lua_do_error(tolua_S, "Error in function call '#funcname#': Expected a valid callback function for parameter #1"); } - class cLuaCallback : public cItemCallback - { - public: - cLuaCallback(cLuaState & a_LuaState, cLuaState::cRef & a_FnRef): - m_LuaState(a_LuaState), - m_FnRef(a_FnRef) - { - } - - private: - cLuaState & m_LuaState; - cLuaState::cRef & m_FnRef; - - virtual bool Item(Ty2 * a_Item) override + // Call the enumeration: + bool res = (Ty1::Get()->*ForEachFn)([&](Ty2 & a_Item) { - bool res = false; // By default continue the enumeration - m_LuaState.Call(m_FnRef, a_Item, cLuaState::Return, res); - return res; + bool ret = false; // By default continue the enumeration + L.Call(FnRef, &a_Item, cLuaState::Return, ret); + return ret; } - } Callback(L, FnRef); - - // Call the enumeration: - bool res = (Ty1::Get()->*ForEachFn)(Callback); + ); // Push the return value: L.Push(res); -- cgit v1.2.3