diff --git a/include/Nazara/Lua/LuaInstance.hpp b/include/Nazara/Lua/LuaInstance.hpp index 9dbb72edf..f9133ec73 100644 --- a/include/Nazara/Lua/LuaInstance.hpp +++ b/include/Nazara/Lua/LuaInstance.hpp @@ -47,8 +47,16 @@ namespace Nz void CheckAny(int index) const; bool CheckBoolean(int index) const; bool CheckBoolean(int index, bool defValue) const; + template T CheckField(const char* fieldName, int tableIndex = -1); + template T CheckField(const String& fieldName, int tableIndex = -1); + template T CheckField(const char* fieldName, T defValue, int tableIndex = -1); + template T CheckField(const String& fieldName, T defValue, int tableIndex = -1); long long CheckInteger(int index) const; long long CheckInteger(int index, long long defValue) const; + template T CheckGlobal(const char* fieldName) const; + template T CheckGlobal(const String& fieldName) const; + template T CheckGlobal(const char* fieldName, T defValue) const; + template T CheckGlobal(const String& fieldName, T defValue) const; double CheckNumber(int index) const; double CheckNumber(int index, double defValue) const; void CheckStack(int space, const char* error = nullptr) const; @@ -78,8 +86,8 @@ namespace Nz bool ExecuteFromStream(Stream& stream); int GetAbsIndex(int index) const; - LuaType GetField(const char* fieldName, int index = -1) const; - LuaType GetField(const String& fieldName, int index = -1) const; + LuaType GetField(const char* fieldName, int tableIndex = -1) const; + LuaType GetField(const String& fieldName, int tableIndex = -1) const; LuaType GetGlobal(const char* name) const; LuaType GetGlobal(const String& name) const; lua_State* GetInternalState() const; @@ -136,8 +144,12 @@ namespace Nz void Remove(int index); void Replace(int index); + template void SetField(const char* name, T&& arg, int tableIndex = -2); + template void SetField(const String& name, T&& arg, int tableIndex = -2); void SetField(const char* name, int tableIndex = -2); void SetField(const String& name, int tableIndex = -2); + template void SetGlobal(const char* name, T&& arg); + template void SetGlobal(const String& name, T&& arg); void SetGlobal(const char* name); void SetGlobal(const String& name); void SetMetatable(const char* tname); diff --git a/include/Nazara/Lua/LuaInstance.inl b/include/Nazara/Lua/LuaInstance.inl index 9034e2cc8..573cb462e 100644 --- a/include/Nazara/Lua/LuaInstance.inl +++ b/include/Nazara/Lua/LuaInstance.inl @@ -78,7 +78,7 @@ namespace Nz std::enable_if_t::value, unsigned int> LuaImplQueryArg(LuaInstance& instance, int index, T* arg, T defValue, TypeTag) { using SignedT = std::make_signed_t; - + return LuaImplQueryArg(instance, index, reinterpret_cast(arg), static_cast(defValue), TypeTag()); } @@ -242,7 +242,7 @@ namespace Nz ProcessArgs<0, Args...>(instance); } - int Invoke(LuaInstance& instance, void (*func)(Args...)) const + int Invoke(LuaInstance& instance, void(*func)(Args...)) const { NazaraUnused(instance); @@ -251,7 +251,7 @@ namespace Nz } template - int Invoke(LuaInstance& instance, Ret (*func)(Args...)) const + int Invoke(LuaInstance& instance, Ret(*func)(Args...)) const { return LuaImplReplyVal(instance, std::move(Apply(func, m_args)), TypeTag()); } @@ -425,7 +425,79 @@ namespace Nz return object; } - + + template + T LuaInstance::CheckField(const char* fieldName, int tableIndex) + { + T object; + + GetField(fieldName, tableIndex); + tableIndex += LuaImplQueryArg(*this, -1, &object, TypeTag()); + Pop(); + + return object; + } + + template + T LuaInstance::CheckField(const String& fieldName, int tableIndex) + { + return CheckField(fieldName.GetConstBuffer(), tableIndex); + } + + template + T LuaInstance::CheckField(const char* fieldName, T defValue, int tableIndex) + { + T object; + + GetField(fieldName, tableIndex); + tableIndex += LuaImplQueryArg(*this, -1, &object, defValue, TypeTag()); + Pop(); + + return object; + } + + template + T LuaInstance::CheckField(const String& fieldName, T defValue, int tableIndex) + { + return CheckField(fieldName.GetConstBuffer(), defValue, tableIndex); + } + + template + T LuaInstance::CheckGlobal(const char* fieldName) + { + T object; + + GetGlobal(fieldName); + tableIndex += LuaImplQueryArg(*this, -1, &object, TypeTag()); + Pop(); + + return object; + } + + template + T LuaInstance::CheckGlobal(const String& fieldName) + { + return CheckGlobal(fieldName.GetConstBuffer()); + } + + template + T LuaInstance::CheckGlobal(const char* fieldName, T defValue) + { + T object; + + GetGlobal(fieldName); + tableIndex += LuaImplQueryArg(*this, -1, &object, defValue, TypeTag()); + Pop(); + + return object; + } + + template + T LuaInstance::CheckGlobal(const String& fieldName, T defValue) + { + return CheckGlobal(fieldName.GetConstBuffer(), defValue); + } + template int LuaInstance::Push(T arg) { @@ -433,11 +505,11 @@ namespace Nz } template - void LuaInstance::PushFunction(R (*func)(Args...), DefArgs&&... defArgs) + void LuaInstance::PushFunction(R(*func)(Args...), DefArgs&&... defArgs) { typename LuaImplFunctionProxy::template Impl handler(std::forward(defArgs)...); - PushFunction([func, handler](LuaInstance& lua) -> int + PushFunction([func, handler] (LuaInstance& lua) -> int { handler.ProcessArgs(lua); @@ -458,4 +530,30 @@ namespace Nz { PushInstance(tname, new T(std::forward(args)...)); } + + template + void LuaInstance::SetField(const char* name, T&& arg, int tableIndex) + { + Push(std::forward(arg)); + SetField(name, tableIndex); + } + + template + void LuaInstance::SetField(const String& name, T&& arg, int tableIndex) + { + SetField(name.GetConstBuffer(), std::forward(arg), tableIndex); + } + + template + void LuaInstance::SetGlobal(const char* name, T&& arg) + { + Push(std::forward(arg)); + SetGlobal(name); + } + + template + void LuaInstance::SetGlobal(const String& name, T&& arg) + { + SetGlobal(name.GetConstBuffer(), std::forward(arg)); + } } diff --git a/src/Nazara/Lua/LuaInstance.cpp b/src/Nazara/Lua/LuaInstance.cpp index dcaad2e26..09bb47bb4 100644 --- a/src/Nazara/Lua/LuaInstance.cpp +++ b/src/Nazara/Lua/LuaInstance.cpp @@ -443,14 +443,14 @@ namespace Nz return lua_absindex(m_state, index); } - LuaType LuaInstance::GetField(const char* fieldName, int index) const + LuaType LuaInstance::GetField(const char* fieldName, int tableIndex) const { - return FromLuaType(lua_getfield(m_state, index, fieldName)); + return FromLuaType(lua_getfield(m_state, tableIndex, fieldName)); } - LuaType LuaInstance::GetField(const String& fieldName, int index) const + LuaType LuaInstance::GetField(const String& fieldName, int tableIndex) const { - return FromLuaType(lua_getfield(m_state, index, fieldName.GetConstBuffer())); + return FromLuaType(lua_getfield(m_state, tableIndex, fieldName.GetConstBuffer())); } LuaType LuaInstance::GetGlobal(const char* name) const