From d6b6e26d317251f82a02d6f940837d3fa2be227d Mon Sep 17 00:00:00 2001 From: Lynix Date: Fri, 21 Oct 2016 17:24:45 +0200 Subject: [PATCH] Lua/LuaClass: Reference the destructor only if the class has one Allows to bind classes with deleted destructors --- include/Nazara/Lua/LuaClass.hpp | 5 ++++- include/Nazara/Lua/LuaClass.inl | 29 +++++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/include/Nazara/Lua/LuaClass.hpp b/include/Nazara/Lua/LuaClass.hpp index 64acc3763..45c3d930b 100644 --- a/include/Nazara/Lua/LuaClass.hpp +++ b/include/Nazara/Lua/LuaClass.hpp @@ -66,6 +66,9 @@ namespace Nz void SetStaticSetter(StaticIndexFunc getter); private: + template + friend struct LuaClassImplFinalizerSetupProxy; + void PushClassInfo(LuaInstance& lua); void SetupConstructor(LuaInstance& lua); void SetupDefaultToString(LuaInstance& lua); @@ -75,7 +78,7 @@ namespace Nz void SetupMetatable(LuaInstance& lua); void SetupMethod(LuaInstance& lua, LuaCFunction proxy, const String& name, std::size_t methodIndex); void SetupSetter(LuaInstance& lua, LuaCFunction proxy); - + using ParentFunc = std::function; using InstanceGetter = std::function; diff --git a/include/Nazara/Lua/LuaClass.inl b/include/Nazara/Lua/LuaClass.inl index 44cb4ee0c..6c609e731 100644 --- a/include/Nazara/Lua/LuaClass.inl +++ b/include/Nazara/Lua/LuaClass.inl @@ -2,6 +2,7 @@ // This file is part of the "Nazara Engine - Lua scripting module" // For conditions of distribution and use, see copyright notice in Config.hpp +#include #include #include #include @@ -251,9 +252,7 @@ namespace Nz template void LuaClass::SetupFinalizer(LuaInstance& lua) { - lua.PushValue(1); // ClassInfo - lua.PushCFunction(FinalizerProxy, 1); - lua.SetField("__gc"); + LuaClassImplFinalizerSetupProxy::value>::Setup(lua); } template @@ -353,6 +352,7 @@ namespace Nz lua.SetField("__newindex"); // Setter } + template int LuaClass::ConstructorProxy(lua_State* state) { @@ -566,7 +566,28 @@ namespace Nz lua.PushString(info->name); return 1; } + + template + struct LuaClassImplFinalizerSetupProxy; + + template + struct LuaClassImplFinalizerSetupProxy + { + static void Setup(LuaInstance& lua) + { + lua.PushValue(1); // ClassInfo + lua.PushCFunction(LuaClass::FinalizerProxy, 1); + lua.SetField("__gc"); + } + }; + + template + struct LuaClassImplFinalizerSetupProxy + { + static void Setup(LuaInstance&) + { + } + }; } #include -#include "LuaClass.hpp"