// Copyright (C) 2015 Jérôme Leclercq // This file is part of the "Nazara Engine - Lua scripting module" // For conditions of distribution and use, see copyright notice in Config.hpp #pragma once #ifndef NAZARA_LUACLASS_HPP #define NAZARA_LUACLASS_HPP #include #include #include #include #include #include #include #include #include namespace Nz { template class LuaClass { template friend class LuaClass; public: using ClassFunc = std::function; using ClassIndexFunc = std::function; using ConstructorFunc = std::function; template using ConvertToParent = std::function; using FinalizerFunc = std::function; using StaticIndexFunc = std::function; using StaticFunc = std::function; LuaClass() = default; LuaClass(const String& name); void BindDefaultConstructor(); void BindMethod(const String& name, ClassFunc method); template std::enable_if_t::value> BindMethod(const String& name, R(P::*func)(Args...), DefArgs&&... defArgs); template std::enable_if_t::value> BindMethod(const String& name, R(P::*func)(Args...) const, DefArgs&&... defArgs); template std::enable_if_t::type>::value> BindMethod(const String& name, R(P::*func)(Args...), DefArgs&&... defArgs); template std::enable_if_t::type>::value> BindMethod(const String& name, R(P::*func)(Args...) const, DefArgs&&... defArgs); void BindStaticMethod(const String& name, StaticFunc func); template void BindStaticMethod(const String& name, R(*func)(Args...), DefArgs&&... defArgs); template void Inherit(LuaClass

& parent); template void Inherit(LuaClass

& parent, ConvertToParent

convertFunc); void Reset(); void Reset(const String& name); void Register(LuaInstance& lua); void PushGlobalTable(LuaInstance& lua); void SetConstructor(ConstructorFunc constructor); void SetFinalizer(FinalizerFunc finalizer); void SetGetter(ClassIndexFunc getter); void SetSetter(ClassIndexFunc setter); void SetStaticGetter(StaticIndexFunc getter); void SetStaticSetter(StaticIndexFunc getter); private: template friend struct LuaClassImplFinalizerSetupProxy; void PushClassInfo(LuaInstance& lua); void SetupConstructor(LuaInstance& lua); void SetupDefaultToString(LuaInstance& lua); void SetupFinalizer(LuaInstance& lua); void SetupGetter(LuaInstance& lua, LuaCFunction proxy); void SetupGlobalTable(LuaInstance& lua); 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; struct ClassInfo { std::vector methods; std::vector parentGetters; std::vector staticMethods; std::unordered_map instanceGetters; ClassIndexFunc getter; ClassIndexFunc setter; ConstructorFunc constructor; FinalizerFunc finalizer; StaticIndexFunc staticGetter; StaticIndexFunc staticSetter; String name; int globalTableRef = -1; }; static int ConstructorProxy(lua_State* state); static int FinalizerProxy(lua_State* state); static int InfoDestructor(lua_State* state); static void Get(const std::shared_ptr& info, LuaInstance& lua, T* instance); static int GetterProxy(lua_State* state); static int MethodProxy(lua_State* state); static int SetterProxy(lua_State* state); static int StaticGetterProxy(lua_State* state); static int StaticMethodProxy(lua_State* state); static int StaticSetterProxy(lua_State* state); static int ToStringProxy(lua_State* state); std::map m_methods; std::map m_staticMethods; std::shared_ptr m_info; }; } #include #endif // NAZARA_LUACLASS_HPP