Former-commit-id: ae44c15d5bd71c7669100951e7104ec6fb25fd87
This commit is contained in:
Lynix 2015-12-14 12:23:05 +01:00
commit e5d22a23cc
10 changed files with 305 additions and 94 deletions

View File

@ -13,64 +13,80 @@
#include <Nazara/Utility/Utility.hpp>
#include <NDK/Algorithm.hpp>
#include <NDK/BaseSystem.hpp>
#include <NDK/Components/CameraComponent.hpp>
#include <NDK/Components/CollisionComponent.hpp>
#include <NDK/Components/LightComponent.hpp>
#include <NDK/Components/ListenerComponent.hpp>
#include <NDK/Components/GraphicsComponent.hpp>
#include <NDK/Components/NodeComponent.hpp>
#include <NDK/Components/PhysicsComponent.hpp>
#include <NDK/Components/VelocityComponent.hpp>
#include <NDK/Systems/ListenerSystem.hpp>
#include <NDK/Systems/PhysicsSystem.hpp>
#include <NDK/Systems/RenderSystem.hpp>
#include <NDK/Systems/VelocitySystem.hpp>
#ifndef NDK_SERVER
#include <NDK/Components/CameraComponent.hpp>
#include <NDK/Components/LightComponent.hpp>
#include <NDK/Components/ListenerComponent.hpp>
#include <NDK/Components/GraphicsComponent.hpp>
#include <NDK/Systems/ListenerSystem.hpp>
#include <NDK/Systems/RenderSystem.hpp>
#endif
namespace Ndk
{
bool Sdk::Initialize()
{
if (s_referenceCounter++ > 0)
return true; // Déjà initialisé
return true; // Already initialized
try
{
Nz::ErrorFlags errFlags(Nz::ErrorFlag_ThrowException, true);
// Initialisation du moteur
// Initialize the engine first
// Modules clients
Nz::Audio::Initialize();
Nz::Graphics::Initialize();
// Modules serveurs
// Shared modules
Nz::Lua::Initialize();
Nz::Noise::Initialize();
Nz::Physics::Initialize();
Nz::Utility::Initialize();
// Initialisation du SDK
#ifndef NDK_SERVER
// Client modules
Nz::Audio::Initialize();
Nz::Graphics::Initialize();
#endif
// Initialisation des composants et systèmes
// SDK Initialization
// Components
BaseComponent::Initialize();
BaseSystem::Initialize();
// Composants
InitializeComponent<CameraComponent>("NdkCam");
// Shared components
InitializeComponent<CollisionComponent>("NdkColli");
InitializeComponent<LightComponent>("NdkLight");
InitializeComponent<ListenerComponent>("NdkList");
InitializeComponent<GraphicsComponent>("NdkGfx");
InitializeComponent<NodeComponent>("NdkNode");
InitializeComponent<PhysicsComponent>("NdkPhys");
InitializeComponent<VelocityComponent>("NdkVeloc");
// Systèmes
InitializeSystem<ListenerSystem>();
#ifndef NDK_SERVER
// Client components
InitializeComponent<CameraComponent>("NdkCam");
InitializeComponent<LightComponent>("NdkLight");
InitializeComponent<ListenerComponent>("NdkList");
InitializeComponent<GraphicsComponent>("NdkGfx");
#endif
// Systems
BaseSystem::Initialize();
// Shared systems
InitializeSystem<PhysicsSystem>();
InitializeSystem<RenderSystem>();
InitializeSystem<VelocitySystem>();
#ifndef NDK_SERVER
// Client systems
InitializeSystem<ListenerSystem>();
InitializeSystem<RenderSystem>();
#endif
NazaraNotice("Initialized: SDK");
return true;
}
@ -86,23 +102,25 @@ namespace Ndk
{
if (s_referenceCounter != 1)
{
// Le module est soit encore utilisé, soit pas initialisé
// Either the module is not initialized, either it was initialized multiple times
if (s_referenceCounter > 1)
s_referenceCounter--;
return;
}
// Libération du SDK
// Uninitialize the SDK
s_referenceCounter = 0;
// Libération du moteur
// Uninitialize the engine
// Modules clients
#ifndef NDK_SERVER
// Client modules
Nz::Audio::Uninitialize();
Nz::Graphics::Uninitialize();
#endif
// Modules serveurs
// Shared modules
Nz::Lua::Uninitialize();
Nz::Noise::Uninitialize();
Nz::Physics::Uninitialize();

View File

@ -4,11 +4,14 @@
#include <NDK/World.hpp>
#include <Nazara/Core/Error.hpp>
#include <NDK/Systems/ListenerSystem.hpp>
#include <NDK/Systems/PhysicsSystem.hpp>
#include <NDK/Systems/RenderSystem.hpp>
#include <NDK/Systems/VelocitySystem.hpp>
#ifndef NDK_SERVER
#include <NDK/Systems/ListenerSystem.hpp>
#include <NDK/Systems/RenderSystem.hpp>
#endif
namespace Ndk
{
World::~World()
@ -19,10 +22,13 @@ namespace Ndk
void World::AddDefaultSystems()
{
AddSystem<ListenerSystem>();
AddSystem<PhysicsSystem>();
AddSystem<RenderSystem>();
AddSystem<VelocitySystem>();
#ifndef NDK_SERVER
AddSystem<ListenerSystem>();
AddSystem<RenderSystem>();
#endif
}
const EntityHandle& World::CreateEntity()

View File

@ -0,0 +1,40 @@
TOOL.Name = "SDKServer"
TOOL.Directory = "../SDK/lib"
TOOL.Kind = "Library"
TOOL.Defines = {
"NDK_BUILD",
"NDK_SERVER"
}
TOOL.Includes = {
"../SDK/include"
}
TOOL.Files = {
"../SDK/include/NDK/**.hpp",
"../SDK/include/NDK/**.inl",
"../SDK/src/NDK/**.hpp",
"../SDK/src/NDK/**.inl",
"../SDK/src/NDK/**.cpp"
}
-- Exlude client-only files
TOOL.FilesExclusion = {
"../SDK/**/CameraComponent.*",
"../SDK/**/GraphicsComponent.*",
"../SDK/**/LightComponent.*",
"../SDK/**/ListenerComponent.*",
"../SDK/**/ListenerSystem.*",
"../SDK/**/RenderSystem.*",
"../SDK/**/LuaInterface_Audio.*"
}
TOOL.Libraries = {
"NazaraCore",
"NazaraLua",
"NazaraNoise",
"NazaraPhysics",
"NazaraUtility"
}

View File

@ -25,6 +25,12 @@ namespace Nz
template<typename T> ByteArray ComputeHash(AbstractHash* hash, const T& v);
template<typename T> void HashCombine(std::size_t& seed, const T& v);
template<typename T>
struct PointedType
{
using type = void; //< FIXME: I can't make SFINAE work
};
template<typename T>
struct TypeTag {};

View File

@ -79,6 +79,11 @@ namespace Nz
seed = static_cast<std::size_t>(b * kMul);
}
template<typename T> struct PointedType<T*> {typedef T type;};
template<typename T> struct PointedType<T* const> {typedef T type;};
template<typename T> struct PointedType<T* volatile> {typedef T type;};
template<typename T> struct PointedType<T* const volatile> {typedef T type;};
inline bool Serialize(SerializationContext& context, bool value)
{
if (context.currentBitPos == 8)

View File

@ -8,6 +8,7 @@
#define NAZARA_RESOURCEREF_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Core/RefCounted.hpp>
#include <type_traits>
@ -44,6 +45,9 @@ namespace Nz
private:
T* m_object;
};
template<typename T> struct PointedType<ObjectRef<T>> {typedef T type;};
template<typename T> struct PointedType<ObjectRef<T> const> {typedef T type;};
}
#include <Nazara/Core/ObjectRef.inl>

View File

@ -8,19 +8,22 @@
#define NAZARA_LUACLASS_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Core/String.hpp>
#include <Nazara/Lua/LuaInstance.hpp>
#include <functional>
#include <map>
#include <memory>
//#include <type_traits>
#include <unordered_map>
#include <vector>
namespace Nz
{
template<class T/*, class P = void*/>
template<class T>
class LuaClass
{
//static_assert(std::is_same<P, void>::value || std::is_base_of<P, T>::value, "P must be a base of T");
template<class U>
friend class LuaClass;
public:
using ClassFunc = std::function<int(LuaInstance& lua, T& instance)>;
@ -32,7 +35,8 @@ namespace Nz
LuaClass(const String& name);
//void Inherit(LuaClass<P>& parent);
template<class P>
void Inherit(LuaClass<P>& parent);
void Register(LuaInstance& lua);
@ -44,7 +48,8 @@ namespace Nz
void SetMethod(const String& name, ClassFunc method);
template<typename R, typename P, typename... Args, typename... DefArgs> std::enable_if_t<std::is_base_of<P, T>::value> SetMethod(const String& name, R(P::*func)(Args...), DefArgs... defArgs);
template<typename R, typename P, typename... Args, typename... DefArgs> std::enable_if_t<std::is_base_of<P, T>::value> SetMethod(const String& name, R(P::*func)(Args...) const, DefArgs... defArgs);
void SetBindMode(LuaBindMode mode);
template<typename R, typename P, typename... Args, typename... DefArgs> std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value> SetMethod(const String& name, R(P::*func)(Args...), DefArgs... defArgs);
template<typename R, typename P, typename... Args, typename... DefArgs> std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value> SetMethod(const String& name, R(P::*func)(Args...) const, DefArgs... defArgs);
void SetSetter(ClassIndexFunc setter);
void SetStaticGetter(StaticIndexFunc getter);
void SetStaticMethod(const String& name, StaticFunc func);
@ -52,9 +57,29 @@ namespace Nz
void SetStaticSetter(StaticIndexFunc getter);
private:
using ParentFunc = std::function<void(LuaInstance& lua, T& instance)>;
using InstanceGetter = std::function<T*(LuaInstance& lua)>;
struct ClassInfo
{
std::vector<ClassFunc> methods;
std::vector<ParentFunc> parentGetters;
std::vector<StaticFunc> staticMethods;
std::unordered_map<String, InstanceGetter> 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<ClassInfo>& info, LuaInstance& lua, T& instance);
static int GetterProxy(lua_State* state);
static int MethodProxy(lua_State* state);
static int SetterProxy(lua_State* state);
@ -62,20 +87,6 @@ namespace Nz
static int StaticMethodProxy(lua_State* state);
static int StaticSetterProxy(lua_State* state);
struct ClassInfo
{
std::vector<ClassFunc> methods;
std::vector<StaticFunc> staticMethods;
ClassIndexFunc getter = nullptr;
ClassIndexFunc setter = nullptr;
ConstructorFunc constructor = nullptr;
FinalizerFunc finalizer = nullptr;
StaticIndexFunc staticGetter = nullptr;
StaticIndexFunc staticSetter = nullptr;
String name;
int globalTableRef = -1;
};
std::map<String, ClassFunc> m_methods;
std::map<String, StaticFunc> m_staticMethods;
std::shared_ptr<ClassInfo> m_info;

View File

@ -4,6 +4,7 @@
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/MemoryHelper.hpp>
#include <type_traits>
#include <Nazara/Lua/Debug.hpp>
namespace Nz
@ -14,15 +15,26 @@ namespace Nz
{
m_info->name = name;
}
/*
template<class T>
void LuaClass<T>::Inherit(LuaClass<P>& parent)
{
static_assert(std::is_base_of<P, T>::value, "P must be a base of T");
m_info->parentInfo = parent.m_info;
template<class T>
template<class P>
inline void LuaClass<T>::Inherit(LuaClass<P>& parent)
{
static_assert(!std::is_same<P, T>::value || std::is_base_of<P, T>::value, "P must be a base of T");
std::shared_ptr<typename LuaClass<P>::ClassInfo>& parentInfo = parent.m_info;
parentInfo->instanceGetters[m_info->name] = [info = m_info](LuaInstance& lua) -> P*
{
return *static_cast<T**>(lua.CheckUserdata(1, info->name));
};
m_info->parentGetters.emplace_back([parentInfo] (LuaInstance& lua, T& instance)
{
LuaClass<P>::Get(parentInfo, lua, instance);
});
}
*/
template<class T>
void LuaClass<T>::Register(LuaInstance& lua)
{
@ -48,11 +60,16 @@ namespace Nz
if (!lua.NewMetatable(m_info->name))
NazaraWarning("Class \"" + m_info->name + "\" already registred in this instance");
{
lua.PushValue(1); // On associe l'UserData avec la fonction
lua.PushCFunction(FinalizerProxy, 1);
lua.SetField("__gc"); // Finalizer
// Set the type in a __type field
lua.PushString(m_info->name);
lua.SetField("__type");
if (m_info->getter)
// Define the Finalizer
lua.PushValue(1);
lua.PushCFunction(FinalizerProxy, 1);
lua.SetField("__gc");
if (m_info->getter || !m_info->parentGetters.empty())
{
lua.PushValue(1); // shared_ptr on UserData
lua.PushValue(-2); // Metatable
@ -76,14 +93,20 @@ namespace Nz
m_info->methods.reserve(m_methods.size());
for (auto& pair : m_methods)
{
std::size_t methodIndex = m_info->methods.size();
m_info->methods.push_back(pair.second);
lua.PushValue(1); // shared_ptr on UserData
lua.PushInteger(m_info->methods.size() - 1);
lua.PushInteger(methodIndex);
lua.PushCFunction(MethodProxy, 2);
lua.SetField(pair.first); // Method name
}
m_info->instanceGetters[m_info->name] = [info = m_info](LuaInstance& lua)
{
return *static_cast<T**>(lua.CheckUserdata(1, info->name));
};
}
lua.Pop(); // On pop la metatable
@ -126,10 +149,11 @@ namespace Nz
m_info->staticMethods.reserve(m_staticMethods.size());
for (auto& pair : m_staticMethods)
{
std::size_t methodIndex = m_info->staticMethods.size();
m_info->staticMethods.push_back(pair.second);
lua.PushValue(1); // shared_ptr on UserData
lua.PushInteger(m_info->staticMethods.size() - 1);
lua.PushInteger(methodIndex);
lua.PushCFunction(StaticMethodProxy, 2);
lua.SetField(pair.first); // ClassMeta.method = StaticMethodProxy
@ -181,7 +205,7 @@ namespace Nz
{
SetMethod(name, [func, defArgs...] (LuaInstance& instance, T& object) -> int
{
LuaImplMethodProxy<T, Args...>::Impl<DefArgs...> handler(instance, object, defArgs...);
typename LuaImplMethodProxy<T, Args...>::template Impl<DefArgs...> handler(instance, object, defArgs...);
handler.ProcessArgs();
return handler.Invoke(func);
@ -201,6 +225,32 @@ namespace Nz
});
}
template<class T>
template<typename R, typename P, typename... Args, typename... DefArgs>
std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value> LuaClass<T>::SetMethod(const String& name, R(P::*func)(Args...), DefArgs... defArgs)
{
SetMethod(name, [func, defArgs...](LuaInstance& instance, T& object) -> int
{
typename LuaImplMethodProxy<T, Args...>::template Impl<DefArgs...> handler(instance, object, defArgs...);
handler.ProcessArgs();
return handler.Invoke(func);
});
}
template<class T>
template<typename R, typename P, typename... Args, typename... DefArgs>
std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value> LuaClass<T>::SetMethod(const String& name, R(P::*func)(Args...) const, DefArgs... defArgs)
{
SetMethod(name, [func, defArgs...](LuaInstance& instance, T& object) -> int
{
typename LuaImplMethodProxy<T, Args...>::template Impl<DefArgs...> handler(instance, object, defArgs...);
handler.ProcessArgs();
return handler.Invoke(func);
});
}
template<class T>
void LuaClass<T>::SetSetter(ClassIndexFunc setter)
{
@ -243,7 +293,7 @@ namespace Nz
{
LuaInstance& lua = *LuaInstance::GetInstance(state);
ClassInfo* info = *static_cast<ClassInfo**>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
const ConstructorFunc& constructor = info->constructor;
lua.Remove(1); // On enlève l'argument "table" du stack
@ -255,10 +305,7 @@ namespace Nz
return 0; // Normalement jamais exécuté (l'erreur provoquant une exception)
}
T** ud = static_cast<T**>(lua.PushUserdata(sizeof(T*)));
*ud = instance;
lua.SetMetatable(info->name);
lua.PushInstance(info->name.GetConstBuffer(), instance);
return 1;
}
@ -267,7 +314,7 @@ namespace Nz
{
LuaInstance& lua = *LuaInstance::GetInstance(state);
ClassInfo* info = *static_cast<ClassInfo**>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
const FinalizerFunc& finalizer = info->finalizer;
T* instance = *static_cast<T**>(lua.CheckUserdata(1, info->name));
@ -284,34 +331,54 @@ namespace Nz
{
LuaInstance& lua = *LuaInstance::GetInstance(state);
std::shared_ptr<ClassInfo>& infoPtr = *static_cast<std::shared_ptr<ClassInfo>*>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
lua.DestroyReference(infoPtr->globalTableRef);
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
lua.DestroyReference(info->globalTableRef);
using namespace std; // Obligatoire pour le destructeur
infoPtr.~shared_ptr(); // Si vous voyez une autre façon de faire, je suis preneur
info.~shared_ptr(); // Si vous voyez une autre façon de faire, je suis preneur
return 0;
}
template<class T>
void LuaClass<T>::Get(const std::shared_ptr<ClassInfo>& info, LuaInstance& lua, T& instance)
{
const ClassIndexFunc& getter = info->getter;
if (!getter || !getter(lua, instance))
{
// Query from the metatable
lua.GetMetatable(info->name); //< Metatable
lua.PushValue(1); //< Field
lua.GetTable(); // Metatable[Field]
lua.Remove(-2); // Remove Metatable
if (!lua.IsValid(-1))
{
for (const ParentFunc& getter : info->parentGetters)
{
lua.Pop(); //< Pop the last nil value
getter(lua, instance);
if (lua.IsValid(-1))
return;
}
}
}
}
template<class T>
int LuaClass<T>::GetterProxy(lua_State* state)
{
LuaInstance& lua = *LuaInstance::GetInstance(state);
ClassInfo* info = *static_cast<ClassInfo**>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
const ClassIndexFunc& getter = info->getter;
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
T& instance = *(*static_cast<T**>(lua.CheckUserdata(1, info->name)));
lua.Remove(1); //< Remove the instance from the Lua stack
if (!getter(lua, instance))
{
// On accède alors à la table
lua.PushValue(lua.GetIndexOfUpValue(2));
lua.PushValue(-2);
lua.GetTable();
}
Get(info, lua, instance);
return 1;
}
@ -320,14 +387,26 @@ namespace Nz
{
LuaInstance& lua = *LuaInstance::GetInstance(state);
ClassInfo* info = *static_cast<ClassInfo**>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
unsigned int index = static_cast<unsigned int>(lua.ToInteger(lua.GetIndexOfUpValue(2)));
const ClassFunc& method = info->methods[index];
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
T* instance = nullptr;
if (lua.GetMetatable(1))
{
LuaType type = lua.GetField("__type");
if (type == LuaType_String)
{
String name = lua.ToString(-1);
auto it = info->instanceGetters.find(name);
if (it != info->instanceGetters.end())
instance = it->second(lua);
}
}
T& instance = *(*static_cast<T**>(lua.CheckUserdata(1, info->name)));
lua.Remove(1); //< Remove the instance from the Lua stack
return method(lua, instance);
unsigned int index = static_cast<unsigned int>(lua.ToInteger(lua.GetIndexOfUpValue(2)));
const ClassFunc& method = info->methods[index];
return method(lua, *instance);
}
template<class T>
@ -335,7 +414,7 @@ namespace Nz
{
LuaInstance& lua = *LuaInstance::GetInstance(state);
ClassInfo* info = *static_cast<ClassInfo**>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
const ClassIndexFunc& setter = info->setter;
T& instance = *(*static_cast<T**>(lua.CheckUserdata(1, info->name)));
@ -357,7 +436,7 @@ namespace Nz
{
LuaInstance& lua = *LuaInstance::GetInstance(state);
ClassInfo* info = *static_cast<ClassInfo**>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
const StaticIndexFunc& getter = info->staticGetter;
if (!getter(lua))
@ -376,7 +455,7 @@ namespace Nz
{
LuaInstance& lua = *LuaInstance::GetInstance(state);
ClassInfo* info = *static_cast<ClassInfo**>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
unsigned int index = static_cast<unsigned int>(lua.ToInteger(lua.GetIndexOfUpValue(2)));
const StaticFunc& method = info->staticMethods[index];
@ -388,7 +467,7 @@ namespace Nz
{
LuaInstance& lua = *LuaInstance::GetInstance(state);
ClassInfo* info = *static_cast<ClassInfo**>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
const StaticIndexFunc& setter = info->staticSetter;
if (!setter(lua))

View File

@ -117,6 +117,8 @@ namespace Nz
void PushCFunction(LuaCFunction func, unsigned int upvalueCount = 0);
void PushFunction(LuaFunction func);
template<typename R, typename... Args, typename... DefArgs> void PushFunction(R(*func)(Args...), DefArgs... defArgs);
template<typename T> void PushInstance(const char* tname, T* instance);
template<typename T, typename... Args> void PushInstance(const char* tname, Args&&... args);
void PushInteger(long long value);
void PushLightUserdata(void* value);
void PushMetatable(const char* str);

View File

@ -344,6 +344,32 @@ namespace Nz
return LuaImplReplyVal(m_instance, std::move(Apply(m_object, func, m_args)), TypeTag<decltype(Apply(m_object, func, m_args))>());
}
template<typename P>
std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value, int> Invoke(void(P::*func)(Args...))
{
Apply(*m_object, func, m_args);
return 0;
}
template<typename P, typename Ret>
std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value, int> Invoke(Ret(P::*func)(Args...))
{
return LuaImplReplyVal(m_instance, std::move(Apply(*m_object, func, m_args)), TypeTag<decltype(Apply(*m_object, func, m_args))>());
}
template<typename P>
std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value, int> Invoke(void(P::*func)(Args...) const)
{
Apply(*m_object, func, m_args);
return 0;
}
template<typename P, typename Ret>
std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value, int> Invoke(Ret(P::*func)(Args...) const)
{
return LuaImplReplyVal(m_instance, std::move(Apply(*m_object, func, m_args)), TypeTag<decltype(Apply(*m_object, func, m_args))>());
}
private:
using ArgContainer = std::tuple<std::remove_cv_t<std::remove_reference_t<Args>>...>;
using DefArgContainer = std::tuple<std::remove_cv_t<std::remove_reference_t<DefArgs>>...>;
@ -404,4 +430,18 @@ namespace Nz
return handler.Invoke(func);
});
}
template<typename T>
void LuaInstance::PushInstance(const char* tname, T* instance)
{
T** userdata = static_cast<T**>(PushUserdata(sizeof(T*)));
*userdata = instance;
SetMetatable(tname);
}
template<typename T, typename... Args>
void LuaInstance::PushInstance(const char* tname, Args&&... args)
{
PushInstance(tname, new T(std::forward<Args>(args)...));
}
}