Optimize Lua binding
Optimize binding by removing a useless extra indirection and allowing to move replying variables Former-commit-id: 76728df1c3ab9a38a4304ae2b9f2fc6a000e0ebb
This commit is contained in:
@@ -28,7 +28,7 @@ namespace Nz
|
||||
public:
|
||||
using ClassFunc = std::function<int(LuaInstance& lua, T& instance)>;
|
||||
using ClassIndexFunc = std::function<bool(LuaInstance& lua, T& instance)>;
|
||||
using ConstructorFunc = std::function<T*(LuaInstance& lua)>;
|
||||
using ConstructorFunc = std::function<bool(LuaInstance& lua, T* instance)>;
|
||||
template<typename P> using ConvertToParent = std::function<P*(T*)>;
|
||||
using FinalizerFunc = std::function<bool(LuaInstance& lua, T& instance)>;
|
||||
using StaticIndexFunc = std::function<bool(LuaInstance& lua)>;
|
||||
@@ -36,6 +36,8 @@ namespace Nz
|
||||
|
||||
LuaClass(const String& name);
|
||||
|
||||
void BindDefaultConstructor();
|
||||
|
||||
void BindMethod(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> BindMethod(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> BindMethod(const String& name, R(P::*func)(Args...) const, DefArgs&&... defArgs);
|
||||
|
||||
@@ -16,6 +16,16 @@ namespace Nz
|
||||
m_info->name = name;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline void LuaClass<T>::BindDefaultConstructor()
|
||||
{
|
||||
SetConstructor([] (Nz::LuaInstance& lua, T* instance)
|
||||
{
|
||||
PlacementNew(instance);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
template<class T>
|
||||
template<class P>
|
||||
inline void LuaClass<T>::Inherit(LuaClass<P>& parent)
|
||||
@@ -36,7 +46,7 @@ namespace Nz
|
||||
|
||||
parentInfo->instanceGetters[m_info->name] = [info = m_info, convertFunc] (LuaInstance& lua) -> P*
|
||||
{
|
||||
return convertFunc(*static_cast<T**>(lua.CheckUserdata(1, info->name)));
|
||||
return convertFunc(static_cast<T*>(lua.CheckUserdata(1, info->name)));
|
||||
};
|
||||
|
||||
m_info->parentGetters.emplace_back([parentInfo, convertFunc] (LuaInstance& lua, T* instance)
|
||||
@@ -124,7 +134,7 @@ namespace Nz
|
||||
|
||||
m_info->instanceGetters[m_info->name] = [info = m_info] (LuaInstance& lua)
|
||||
{
|
||||
return *static_cast<T**>(lua.CheckUserdata(1, info->name));
|
||||
return static_cast<T*>(lua.CheckUserdata(1, info->name));
|
||||
};
|
||||
}
|
||||
lua.Pop(); // On pop la metatable
|
||||
@@ -322,14 +332,15 @@ namespace Nz
|
||||
|
||||
lua.Remove(1); // On enlève l'argument "table" du stack
|
||||
|
||||
T* instance = constructor(lua);
|
||||
if (!instance)
|
||||
T* instance = static_cast<T*>(lua.PushUserdata(sizeof(T)));
|
||||
|
||||
if (!constructor(lua, instance))
|
||||
{
|
||||
lua.Error("Constructor failed");
|
||||
return 0; // Normalement jamais exécuté (l'erreur provoquant une exception)
|
||||
}
|
||||
|
||||
lua.PushInstance(info->name.GetConstBuffer(), instance);
|
||||
lua.SetMetatable(info->name);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -341,11 +352,11 @@ namespace Nz
|
||||
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));
|
||||
T* instance = static_cast<T*>(lua.CheckUserdata(1, info->name));
|
||||
lua.Remove(1); //< Remove the instance from the Lua stack
|
||||
|
||||
if (!finalizer || finalizer(lua, *instance))
|
||||
delete instance;
|
||||
instance->~T();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -399,7 +410,7 @@ namespace Nz
|
||||
|
||||
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));
|
||||
T* instance = static_cast<T*>(lua.CheckUserdata(1, info->name));
|
||||
lua.Remove(1); //< Remove the instance from the Lua stack
|
||||
|
||||
Get(info, lua, instance);
|
||||
@@ -448,7 +459,7 @@ namespace Nz
|
||||
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)));
|
||||
T& instance = *static_cast<T*>(lua.CheckUserdata(1, info->name));
|
||||
lua.Remove(1); //< Remove the instance from the Lua stack
|
||||
|
||||
if (!setter(lua, instance))
|
||||
|
||||
@@ -131,7 +131,8 @@ namespace Nz
|
||||
template<typename R, typename... Args, typename... DefArgs> void PushFunction(R(*func)(Args...), DefArgs&&... defArgs) const;
|
||||
template<typename T> void PushGlobal(const char* name, T&& arg);
|
||||
template<typename T> void PushGlobal(const String& name, T&& arg);
|
||||
template<typename T> void PushInstance(const char* tname, T* instance) const;
|
||||
template<typename T> void PushInstance(const char* tname, const T& instance) const;
|
||||
template<typename T> void PushInstance(const char* tname, T&& instance) const;
|
||||
template<typename T, typename... Args> void PushInstance(const char* tname, Args&&... args) const;
|
||||
void PushInteger(long long value) const;
|
||||
void PushLightUserdata(void* value) const;
|
||||
|
||||
@@ -156,21 +156,21 @@ namespace Nz
|
||||
return LuaImplReplyVal(instance, val, TypeTag<T>());
|
||||
}
|
||||
|
||||
inline int LuaImplReplyVal(const LuaInstance& instance, std::string val, TypeTag<std::string>)
|
||||
inline int LuaImplReplyVal(const LuaInstance& instance, std::string&& val, TypeTag<std::string>)
|
||||
{
|
||||
instance.PushString(val.c_str(), val.size());
|
||||
return 1;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline int LuaImplReplyVal(const LuaInstance& instance, std::vector<T> valContainer, TypeTag<std::vector<T>>)
|
||||
inline int LuaImplReplyVal(const LuaInstance& instance, std::vector<T>&& valContainer, TypeTag<std::vector<T>>)
|
||||
{
|
||||
std::size_t index = 1;
|
||||
instance.PushTable(valContainer.size());
|
||||
for (const T& val : valContainer)
|
||||
for (T& val : valContainer)
|
||||
{
|
||||
instance.PushInteger(index++);
|
||||
if (LuaImplReplyVal(instance, val, TypeTag<T>()) != 1)
|
||||
if (LuaImplReplyVal(instance, std::move(val), TypeTag<T>()) != 1)
|
||||
{
|
||||
instance.Error("Couldn't create table: type need more than one place to store");
|
||||
return 0;
|
||||
@@ -181,20 +181,20 @@ namespace Nz
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline int LuaImplReplyVal(const LuaInstance& instance, ByteArray val, TypeTag<ByteArray>)
|
||||
inline int LuaImplReplyVal(const LuaInstance& instance, ByteArray&& val, TypeTag<ByteArray>)
|
||||
{
|
||||
instance.PushString(reinterpret_cast<const char*>(val.GetConstBuffer()), val.GetSize());
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline int LuaImplReplyVal(const LuaInstance& instance, String val, TypeTag<String>)
|
||||
inline int LuaImplReplyVal(const LuaInstance& instance, String&& val, TypeTag<String>)
|
||||
{
|
||||
instance.PushString(std::move(val));
|
||||
return 1;
|
||||
}
|
||||
|
||||
template<typename T1, typename T2>
|
||||
int LuaImplReplyVal(const LuaInstance& instance, std::pair<T1, T2> val, TypeTag<std::pair<T1, T2>>)
|
||||
int LuaImplReplyVal(const LuaInstance& instance, std::pair<T1, T2>&& val, TypeTag<std::pair<T1, T2>>)
|
||||
{
|
||||
int retVal = 0;
|
||||
|
||||
@@ -593,17 +593,30 @@ namespace Nz
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void LuaInstance::PushInstance(const char* tname, T* instance) const
|
||||
void LuaInstance::PushInstance(const char* tname, const T& instance) const
|
||||
{
|
||||
T** userdata = static_cast<T**>(PushUserdata(sizeof(T*)));
|
||||
*userdata = instance;
|
||||
T* userdata = static_cast<T*>(PushUserdata(sizeof(T*)));
|
||||
PlacementNew(userdata, instance);
|
||||
|
||||
SetMetatable(tname);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void LuaInstance::PushInstance(const char* tname, T&& instance) const
|
||||
{
|
||||
T* userdata = static_cast<T*>(PushUserdata(sizeof(T*)));
|
||||
PlacementNew(userdata, std::move(instance));
|
||||
|
||||
SetMetatable(tname);
|
||||
}
|
||||
|
||||
template<typename T, typename... Args>
|
||||
void LuaInstance::PushInstance(const char* tname, Args&&... args) const
|
||||
{
|
||||
PushInstance(tname, new T(std::forward<Args>(args)...));
|
||||
T* userdata = static_cast<T*>(PushUserdata(sizeof(T*)));
|
||||
PlacementNew(userdata, std::forward<Args>(args)...);
|
||||
|
||||
SetMetatable(tname);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
||||
Reference in New Issue
Block a user