// Copyright (C) 2017 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(LuaState& state); void PushGlobalTable(LuaState& state); 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; int PushClassInfo(LuaState& state); void SetupConstructor(LuaState& state, int classInfoRef); void SetupDefaultToString(LuaState& state, int classInfoRef); void SetupFinalizer(LuaState& state, int classInfoRef); void SetupGetter(LuaState& state, LuaCFunction proxy, int classInfoRef); void SetupGlobalTable(LuaState& state, int classInfoRef); void SetupMetatable(LuaState& state, int classInfoRef); void SetupMethod(LuaState& state, LuaCFunction proxy, const String& name, std::size_t methodIndex, int classInfoRef); void SetupSetter(LuaState& state, LuaCFunction proxy, int classInfoRef); 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* internalState); static int FinalizerProxy(lua_State* internalState); static int InfoDestructor(lua_State* internalState); static void Get(const std::shared_ptr& info, LuaState& state, T* instance); static int GetterProxy(lua_State* internalState); static int MethodProxy(lua_State* internalState); static int SetterProxy(lua_State* internalState); static int StaticGetterProxy(lua_State* internalState); static int StaticMethodProxy(lua_State* internalState); static int StaticSetterProxy(lua_State* internalState); static int ToStringProxy(lua_State* internalState); std::map m_methods; std::map m_staticMethods; std::shared_ptr m_info; }; } #include #endif // NAZARA_LUACLASS_HPP