From 326fb930af75e0e0f4429e2c0ea06398c1fe94fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Leclercq?= Date: Thu, 27 Jul 2017 11:41:13 +0200 Subject: [PATCH] Sdk/LuaBinding: Add support for component binding without default constructor (will throw a lua error) --- SDK/include/NDK/Lua/LuaBinding.inl | 46 +++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/SDK/include/NDK/Lua/LuaBinding.inl b/SDK/include/NDK/Lua/LuaBinding.inl index 502f179ae..cb80305d7 100644 --- a/SDK/include/NDK/Lua/LuaBinding.inl +++ b/SDK/include/NDK/Lua/LuaBinding.inl @@ -6,6 +6,35 @@ namespace Ndk { + namespace Detail + { + template + struct AddComponentIf; + + template<> + struct AddComponentIf + { + template + static int AddComponent(Nz::LuaState& lua, EntityHandle& handle) + { + T& component = handle->AddComponent(); + lua.Push(component.CreateHandle()); + return 1; + } + }; + + template<> + struct AddComponentIf + { + template + static int AddComponent(Nz::LuaState& lua, EntityHandle& /*handle*/) + { + lua.Error("Component has no default constructor and cannot be created from Lua yet"); + return 0; + } + }; + } + /*! * \brief Binds a component to a name * @@ -13,14 +42,15 @@ namespace Ndk * * \remark Produces a NazaraAssert if name is empty */ - template void LuaBinding::BindComponent(const Nz::String& name) { NazaraAssert(!name.IsEmpty(), "Component name cannot be empty"); + static_assert(std::is_base_of::value, "ComponentType must inherit BaseComponent"); + ComponentBinding binding; - binding.adder = &AddComponentOfType; + binding.adder = &Detail::AddComponentIf::value>::AddComponent; binding.getter = &PushComponentOfType; binding.index = T::componentIndex; binding.name = name; @@ -32,21 +62,9 @@ namespace Ndk m_componentBindingByName[name] = T::componentIndex; } - template - int LuaBinding::AddComponentOfType(Nz::LuaState& lua, EntityHandle& handle) - { - static_assert(std::is_base_of::value, "ComponentType must inherit BaseComponent"); - - T& component = handle->AddComponent(); - lua.Push(component.CreateHandle()); - return 1; - } - template int LuaBinding::PushComponentOfType(Nz::LuaState& lua, BaseComponent& component) { - static_assert(std::is_base_of::value, "ComponentType must inherit BaseComponent"); - T& rightComponent = static_cast(component); lua.Push(rightComponent.CreateHandle()); return 1;