Lua/LuaClass: Reference the destructor only if the class has one

Allows to bind classes with deleted destructors
This commit is contained in:
Lynix 2016-10-21 17:24:45 +02:00
parent 3f7f12b625
commit d6b6e26d31
2 changed files with 29 additions and 5 deletions

View File

@ -66,6 +66,9 @@ namespace Nz
void SetStaticSetter(StaticIndexFunc getter); void SetStaticSetter(StaticIndexFunc getter);
private: private:
template<typename T, bool HasDestructor>
friend struct LuaClassImplFinalizerSetupProxy;
void PushClassInfo(LuaInstance& lua); void PushClassInfo(LuaInstance& lua);
void SetupConstructor(LuaInstance& lua); void SetupConstructor(LuaInstance& lua);
void SetupDefaultToString(LuaInstance& lua); void SetupDefaultToString(LuaInstance& lua);

View File

@ -2,6 +2,7 @@
// This file is part of the "Nazara Engine - Lua scripting module" // This file is part of the "Nazara Engine - Lua scripting module"
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Lua/LuaClass.hpp>
#include <Nazara/Core/Error.hpp> #include <Nazara/Core/Error.hpp>
#include <Nazara/Core/MemoryHelper.hpp> #include <Nazara/Core/MemoryHelper.hpp>
#include <type_traits> #include <type_traits>
@ -251,9 +252,7 @@ namespace Nz
template<class T> template<class T>
void LuaClass<T>::SetupFinalizer(LuaInstance& lua) void LuaClass<T>::SetupFinalizer(LuaInstance& lua)
{ {
lua.PushValue(1); // ClassInfo LuaClassImplFinalizerSetupProxy<T, std::is_destructible<T>::value>::Setup(lua);
lua.PushCFunction(FinalizerProxy, 1);
lua.SetField("__gc");
} }
template<class T> template<class T>
@ -353,6 +352,7 @@ namespace Nz
lua.SetField("__newindex"); // Setter lua.SetField("__newindex"); // Setter
} }
template<class T> template<class T>
int LuaClass<T>::ConstructorProxy(lua_State* state) int LuaClass<T>::ConstructorProxy(lua_State* state)
{ {
@ -566,7 +566,28 @@ namespace Nz
lua.PushString(info->name); lua.PushString(info->name);
return 1; return 1;
} }
template<typename T, bool HasDestructor>
struct LuaClassImplFinalizerSetupProxy;
template<typename T>
struct LuaClassImplFinalizerSetupProxy<T, true>
{
static void Setup(LuaInstance& lua)
{
lua.PushValue(1); // ClassInfo
lua.PushCFunction(LuaClass<T>::FinalizerProxy, 1);
lua.SetField("__gc");
}
};
template<typename T>
struct LuaClassImplFinalizerSetupProxy<T, false>
{
static void Setup(LuaInstance&)
{
}
};
} }
#include <Nazara/Lua/DebugOff.hpp> #include <Nazara/Lua/DebugOff.hpp>
#include "LuaClass.hpp"