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:
Lynix 2016-04-24 19:54:46 +02:00
parent 6d66063b2c
commit 24a8fcee01
11 changed files with 190 additions and 107 deletions

View File

@ -271,25 +271,25 @@ namespace Nz
inline int LuaImplReplyVal(const LuaInstance& instance, EulerAnglesd val, TypeTag<EulerAnglesd>) inline int LuaImplReplyVal(const LuaInstance& instance, EulerAnglesd&& val, TypeTag<EulerAnglesd>)
{ {
instance.PushInstance<EulerAnglesd>("EulerAngles", val); instance.PushInstance<EulerAnglesd>("EulerAngles", val);
return 1; return 1;
} }
inline int LuaImplReplyVal(const LuaInstance& instance, EulerAnglesf val, TypeTag<EulerAnglesf>) inline int LuaImplReplyVal(const LuaInstance& instance, EulerAnglesf&& val, TypeTag<EulerAnglesf>)
{ {
instance.PushInstance<EulerAnglesd>("EulerAngles", val); instance.PushInstance<EulerAnglesd>("EulerAngles", val);
return 1; return 1;
} }
inline int LuaImplReplyVal(const LuaInstance& instance, FontRef val, TypeTag<FontRef>) inline int LuaImplReplyVal(const LuaInstance& instance, FontRef&& val, TypeTag<FontRef>)
{ {
instance.PushInstance<FontRef>("Font", val); instance.PushInstance<FontRef>("Font", val);
return 1; return 1;
} }
inline int LuaImplReplyVal(const LuaInstance& instance, Font::SizeInfo val, TypeTag<Font::SizeInfo>) inline int LuaImplReplyVal(const LuaInstance& instance, Font::SizeInfo&& val, TypeTag<Font::SizeInfo>)
{ {
instance.PushTable(); instance.PushTable();
instance.PushField("LineHeight", val.lineHeight); instance.PushField("LineHeight", val.lineHeight);
@ -300,55 +300,55 @@ namespace Nz
return 1; return 1;
} }
inline int LuaImplReplyVal(const LuaInstance& instance, Quaterniond val, TypeTag<Quaterniond>) inline int LuaImplReplyVal(const LuaInstance& instance, Quaterniond&& val, TypeTag<Quaterniond>)
{ {
instance.PushInstance<Quaterniond>("Quaternion", val); instance.PushInstance<Quaterniond>("Quaternion", val);
return 1; return 1;
} }
inline int LuaImplReplyVal(const LuaInstance& instance, Quaternionf val, TypeTag<Quaternionf>) inline int LuaImplReplyVal(const LuaInstance& instance, Quaternionf&& val, TypeTag<Quaternionf>)
{ {
instance.PushInstance<Quaterniond>("Quaternion", val); instance.PushInstance<Quaterniond>("Quaternion", val);
return 1; return 1;
} }
inline int LuaImplReplyVal(const LuaInstance& instance, IpAddress val, TypeTag<IpAddress>) inline int LuaImplReplyVal(const LuaInstance& instance, IpAddress&& val, TypeTag<IpAddress>)
{ {
instance.PushInstance<IpAddress>("IpAddress", val); instance.PushInstance<IpAddress>("IpAddress", val);
return 1; return 1;
} }
inline int LuaImplReplyVal(const LuaInstance& instance, Vector2d val, TypeTag<Vector2d>) inline int LuaImplReplyVal(const LuaInstance& instance, Vector2d&& val, TypeTag<Vector2d>)
{ {
instance.PushInstance<Vector2d>("Vector2", val); instance.PushInstance<Vector2d>("Vector2", val);
return 1; return 1;
} }
inline int LuaImplReplyVal(const LuaInstance& instance, Vector2f val, TypeTag<Vector2f>) inline int LuaImplReplyVal(const LuaInstance& instance, Vector2f&& val, TypeTag<Vector2f>)
{ {
instance.PushInstance<Vector2d>("Vector2", val); instance.PushInstance<Vector2d>("Vector2", val);
return 1; return 1;
} }
inline int LuaImplReplyVal(const LuaInstance& instance, Vector2ui val, TypeTag<Vector2ui>) inline int LuaImplReplyVal(const LuaInstance& instance, Vector2ui&& val, TypeTag<Vector2ui>)
{ {
instance.PushInstance<Vector2d>("Vector2", val); instance.PushInstance<Vector2d>("Vector2", val);
return 1; return 1;
} }
inline int LuaImplReplyVal(const LuaInstance& instance, Vector3d val, TypeTag<Vector3d>) inline int LuaImplReplyVal(const LuaInstance& instance, Vector3d&& val, TypeTag<Vector3d>)
{ {
instance.PushInstance<Vector3d>("Vector3", val); instance.PushInstance<Vector3d>("Vector3", val);
return 1; return 1;
} }
inline int LuaImplReplyVal(const LuaInstance& instance, Vector3f val, TypeTag<Vector3f>) inline int LuaImplReplyVal(const LuaInstance& instance, Vector3f&& val, TypeTag<Vector3f>)
{ {
instance.PushInstance<Vector3d>("Vector3", val); instance.PushInstance<Vector3d>("Vector3", val);
return 1; return 1;
} }
inline int LuaImplReplyVal(const LuaInstance& instance, Vector3ui val, TypeTag<Vector3ui>) inline int LuaImplReplyVal(const LuaInstance& instance, Vector3ui&& val, TypeTag<Vector3ui>)
{ {
instance.PushInstance<Vector3d>("Vector3", val); instance.PushInstance<Vector3d>("Vector3", val);
return 1; return 1;
@ -366,19 +366,19 @@ namespace Nz
return 1; return 1;
} }
inline int LuaImplReplyVal(const LuaInstance& instance, Ndk::EntityHandle handle, TypeTag<Ndk::EntityHandle>) inline int LuaImplReplyVal(const LuaInstance& instance, Ndk::EntityHandle&& handle, TypeTag<Ndk::EntityHandle>)
{ {
instance.PushInstance<Ndk::EntityHandle>("Entity", handle); instance.PushInstance<Ndk::EntityHandle>("Entity", handle);
return 1; return 1;
} }
inline int LuaImplReplyVal(const LuaInstance& instance, Ndk::NodeComponentHandle handle, TypeTag<Ndk::NodeComponentHandle>) inline int LuaImplReplyVal(const LuaInstance& instance, Ndk::NodeComponentHandle&& handle, TypeTag<Ndk::NodeComponentHandle>)
{ {
instance.PushInstance<Ndk::NodeComponentHandle>("NodeComponent", handle); instance.PushInstance<Ndk::NodeComponentHandle>("NodeComponent", handle);
return 1; return 1;
} }
inline int LuaImplReplyVal(const LuaInstance& instance, Ndk::VelocityComponentHandle handle, TypeTag<Ndk::VelocityComponentHandle>) inline int LuaImplReplyVal(const LuaInstance& instance, Ndk::VelocityComponentHandle&& handle, TypeTag<Ndk::VelocityComponentHandle>)
{ {
instance.PushInstance<Ndk::VelocityComponentHandle>("VelocityComponent", handle); instance.PushInstance<Ndk::VelocityComponentHandle>("VelocityComponent", handle);
return 1; return 1;
@ -390,20 +390,20 @@ namespace Nz
return 1; return 1;
} }
inline int LuaImplReplyVal(const LuaInstance& instance, Ndk::WorldHandle handle, TypeTag<Ndk::WorldHandle>) inline int LuaImplReplyVal(const LuaInstance& instance, Ndk::WorldHandle&& handle, TypeTag<Ndk::WorldHandle>)
{ {
instance.PushInstance<Ndk::WorldHandle>("World", handle); instance.PushInstance<Ndk::WorldHandle>("World", handle);
return 1; return 1;
} }
#ifndef NDK_SERVER #ifndef NDK_SERVER
inline int LuaImplReplyVal(const LuaInstance& instance, Ndk::ConsoleHandle handle, TypeTag<Ndk::ConsoleHandle>) inline int LuaImplReplyVal(const LuaInstance& instance, Ndk::ConsoleHandle&& handle, TypeTag<Ndk::ConsoleHandle>)
{ {
instance.PushInstance<Ndk::ConsoleHandle>("Console", handle); instance.PushInstance<Ndk::ConsoleHandle>("Console", handle);
return 1; return 1;
} }
inline int LuaImplReplyVal(const LuaInstance& instance, Ndk::GraphicsComponentHandle handle, TypeTag<Ndk::GraphicsComponentHandle>) inline int LuaImplReplyVal(const LuaInstance& instance, Ndk::GraphicsComponentHandle&& handle, TypeTag<Ndk::GraphicsComponentHandle>)
{ {
instance.PushInstance<Ndk::GraphicsComponentHandle>("GraphicsComponent", handle); instance.PushInstance<Ndk::GraphicsComponentHandle>("GraphicsComponent", handle);
return 1; return 1;

View File

@ -1,6 +1,7 @@
// This file was automatically generated on 26 May 2014 at 01:05:31 // This file was automatically generated on 26 May 2014 at 01:05:31
#include <NDK/LuaBinding.hpp> #include <NDK/LuaBinding.hpp>
#include <Nazara/Core/MemoryHelper.hpp>
#include <NDK/LuaAPI.hpp> #include <NDK/LuaAPI.hpp>
namespace Ndk namespace Ndk
@ -10,10 +11,7 @@ namespace Ndk
/*********************************** Nz::Music **********************************/ /*********************************** Nz::Music **********************************/
musicClass.Inherit(soundEmitter); musicClass.Inherit(soundEmitter);
musicClass.SetConstructor([] (Nz::LuaInstance& lua) -> Nz::Music* musicClass.BindDefaultConstructor();
{
return new Nz::Music;
});
//musicClass.SetMethod("Create", &Nz::Music::Create); //musicClass.SetMethod("Create", &Nz::Music::Create);
//musicClass.SetMethod("Destroy", &Nz::Music::Destroy); //musicClass.SetMethod("Destroy", &Nz::Music::Destroy);
@ -51,10 +49,7 @@ namespace Ndk
/*********************************** Nz::Sound **********************************/ /*********************************** Nz::Sound **********************************/
soundClass.Inherit(soundEmitter); soundClass.Inherit(soundEmitter);
soundClass.SetConstructor([] (Nz::LuaInstance& lua) -> Nz::Sound* soundClass.BindDefaultConstructor();
{
return new Nz::Sound;
});
soundClass.BindMethod("GetBuffer", &Nz::Sound::GetBuffer); soundClass.BindMethod("GetBuffer", &Nz::Sound::GetBuffer);
@ -79,9 +74,10 @@ namespace Ndk
}); });
/*********************************** Nz::SoundBuffer **********************************/ /*********************************** Nz::SoundBuffer **********************************/
soundBuffer.SetConstructor([] (Nz::LuaInstance& lua) -> Nz::SoundBufferRef* soundBuffer.SetConstructor([] (Nz::LuaInstance& lua, Nz::SoundBufferRef* instance)
{ {
return new Nz::SoundBufferRef(new Nz::SoundBuffer); Nz::PlacementNew(instance, Nz::SoundBuffer::New());
return true;
}); });
soundBuffer.BindMethod("Destroy", &Nz::SoundBuffer::Destroy); soundBuffer.BindMethod("Destroy", &Nz::SoundBuffer::Destroy);

View File

@ -1,6 +1,7 @@
// This file was automatically generated on 26 May 2014 at 01:05:31 // This file was automatically generated on 26 May 2014 at 01:05:31
#include <NDK/LuaBinding.hpp> #include <NDK/LuaBinding.hpp>
#include <Nazara/Core/MemoryHelper.hpp>
#include <NDK/LuaAPI.hpp> #include <NDK/LuaAPI.hpp>
namespace Ndk namespace Ndk
@ -8,10 +9,14 @@ namespace Ndk
void LuaBinding::BindCore() void LuaBinding::BindCore()
{ {
/*********************************** Nz::Clock **********************************/ /*********************************** Nz::Clock **********************************/
clockClass.SetConstructor([](Nz::LuaInstance& lua) -> Nz::Clock* clockClass.SetConstructor([](Nz::LuaInstance& lua, Nz::Clock* clock)
{ {
int argIndex = 1; int argIndex = 1;
return new Nz::Clock(lua.Check<Nz::Int64>(&argIndex, 0), lua.Check<bool>(&argIndex, false)); Nz::Int64 startingValue = lua.Check<Nz::Int64>(&argIndex, 0);
bool paused = lua.Check<bool>(&argIndex, false);
Nz::PlacementNew(clock, startingValue, paused);
return true;
}); });
clockClass.BindMethod("GetMicroseconds", &Nz::Clock::GetMicroseconds); clockClass.BindMethod("GetMicroseconds", &Nz::Clock::GetMicroseconds);
@ -35,7 +40,7 @@ namespace Ndk
}); });
/********************************* Nz::Directory ********************************/ /********************************* Nz::Directory ********************************/
directoryClass.SetConstructor([](Nz::LuaInstance& lua) -> Nz::Directory* directoryClass.SetConstructor([](Nz::LuaInstance& lua, Nz::Directory* directory)
{ {
unsigned int argCount = std::min(lua.GetStackTop(), 1U); unsigned int argCount = std::min(lua.GetStackTop(), 1U);
@ -43,13 +48,15 @@ namespace Ndk
switch (argCount) switch (argCount)
{ {
case 0: case 0:
return new Nz::Directory; Nz::PlacementNew(directory);
return true;
case 1: case 1:
return new Nz::Directory(lua.Check<Nz::String>(&argIndex)); Nz::PlacementNew(directory, lua.Check<Nz::String>(&argIndex));
return true;
} }
return nullptr; return false;
}); });
directoryClass.BindMethod("Close", &Nz::Directory::Close); directoryClass.BindMethod("Close", &Nz::Directory::Close);
@ -127,7 +134,7 @@ namespace Ndk
/*********************************** Nz::File ***********************************/ /*********************************** Nz::File ***********************************/
fileClass.Inherit(streamClass); fileClass.Inherit(streamClass);
fileClass.SetConstructor([](Nz::LuaInstance& lua) -> Nz::File* fileClass.SetConstructor([](Nz::LuaInstance& lua, Nz::File* file)
{ {
unsigned int argCount = std::min(lua.GetStackTop(), 2U); unsigned int argCount = std::min(lua.GetStackTop(), 2U);
@ -135,20 +142,29 @@ namespace Ndk
switch (argCount) switch (argCount)
{ {
case 0: case 0:
return new Nz::File; Nz::PlacementNew(file);
return true;
case 1: case 1:
return new Nz::File(lua.Check<Nz::String>(&argIndex)); {
Nz::String filePath = lua.Check<Nz::String>(&argIndex);
Nz::PlacementNew(file, filePath);
return true;
}
case 2: case 2:
{ {
Nz::String filePath = lua.Check<Nz::String>(&argIndex); Nz::String filePath = lua.Check<Nz::String>(&argIndex);
Nz::UInt32 openMode = lua.Check<Nz::UInt32>(&argIndex); Nz::UInt32 openMode = lua.Check<Nz::UInt32>(&argIndex);
return new Nz::File(filePath, openMode);
Nz::PlacementNew(file, filePath, openMode);
return true;
} }
} }
return nullptr; lua.Error("No matching overload for File constructor");
return false;
}); });
fileClass.BindMethod("Close", &Nz::File::Close); fileClass.BindMethod("Close", &Nz::File::Close);

View File

@ -16,9 +16,10 @@ namespace Ndk
return reinterpret_cast<Nz::InstancedRenderableRef*>(model); //TODO: Make a ObjectRefCast return reinterpret_cast<Nz::InstancedRenderableRef*>(model); //TODO: Make a ObjectRefCast
}); });
modelClass.SetConstructor([] (Nz::LuaInstance& lua) -> Nz::ModelRef* modelClass.SetConstructor([] (Nz::LuaInstance& lua, Nz::ModelRef* model)
{ {
return new Nz::ModelRef(new Nz::Model); Nz::PlacementNew(model, Nz::Model::New());
return true;
}); });
//modelClass.SetMethod("GetMaterial", &Nz::Model::GetMaterial); //modelClass.SetMethod("GetMaterial", &Nz::Model::GetMaterial);

View File

@ -1,6 +1,7 @@
// This file was automatically generated on 26 May 2014 at 01:05:31 // This file was automatically generated on 26 May 2014 at 01:05:31
#include <NDK/LuaBinding.hpp> #include <NDK/LuaBinding.hpp>
#include <Nazara/Core/MemoryHelper.hpp>
#include <NDK/LuaAPI.hpp> #include <NDK/LuaAPI.hpp>
#include <cstring> #include <cstring>
@ -9,23 +10,26 @@ namespace Ndk
void LuaBinding::BindMath() void LuaBinding::BindMath()
{ {
/*********************************** Nz::EulerAngles **********************************/ /*********************************** Nz::EulerAngles **********************************/
eulerAnglesClass.SetConstructor([] (Nz::LuaInstance& lua) -> Nz::EulerAnglesd* eulerAnglesClass.SetConstructor([] (Nz::LuaInstance& lua, Nz::EulerAnglesd* angles)
{ {
unsigned int argCount = std::min(lua.GetStackTop(), 3U); unsigned int argCount = std::min(lua.GetStackTop(), 3U);
switch (argCount) switch (argCount)
{ {
case 0: case 0:
return new Nz::EulerAnglesd(0.0, 0.0, 0.0); Nz::PlacementNew(angles, Nz::EulerAnglesd::Zero());
return true;
case 1: case 1:
return new Nz::EulerAnglesd(*(*static_cast<Nz::EulerAnglesd**>(lua.CheckUserdata(1, "EulerAngles")))); Nz::PlacementNew(angles, *static_cast<Nz::EulerAnglesd*>(lua.CheckUserdata(1, "EulerAngles")));
return true;
case 3: case 3:
return new Nz::EulerAnglesd(lua.CheckNumber(1), lua.CheckNumber(2), lua.CheckNumber(3)); Nz::PlacementNew(angles, lua.CheckNumber(1), lua.CheckNumber(2), lua.CheckNumber(3));
return true;
} }
lua.Error("No matching overload for EulerAngles constructor"); lua.Error("No matching overload for EulerAngles constructor");
return nullptr; return false;
}); });
eulerAnglesClass.BindMethod("__tostring", &Nz::EulerAnglesd::ToString); eulerAnglesClass.BindMethod("__tostring", &Nz::EulerAnglesd::ToString);
@ -146,31 +150,38 @@ namespace Ndk
}); });
/*********************************** Nz::Quaternion **********************************/ /*********************************** Nz::Quaternion **********************************/
quaternionClass.SetConstructor([] (Nz::LuaInstance& lua) -> Nz::Quaterniond* quaternionClass.SetConstructor([] (Nz::LuaInstance& lua, Nz::Quaterniond* quaternion)
{ {
unsigned int argCount = std::min(lua.GetStackTop(), 4U); unsigned int argCount = std::min(lua.GetStackTop(), 4U);
switch (argCount) switch (argCount)
{ {
case 0: case 0:
return new Nz::Quaterniond(1.0, 0.0, 0.0, 0.0); Nz::PlacementNew(quaternion, Nz::Quaterniond::Zero());
return true;
case 1: case 1:
{ {
if (lua.IsOfType(1, "EulerAngles")) if (lua.IsOfType(1, "EulerAngles"))
return new Nz::Quaterniond(*(*static_cast<Nz::EulerAnglesd**>(lua.ToUserdata(1)))); Nz::PlacementNew(quaternion, *static_cast<Nz::EulerAnglesd*>(lua.ToUserdata(1)));
else if (lua.IsOfType(1, "Quaternion")) else if (lua.IsOfType(1, "Quaternion"))
return new Nz::Quaterniond(*(*static_cast<Nz::Quaterniond**>(lua.ToUserdata(1)))); Nz::PlacementNew(quaternion, *static_cast<Nz::Quaterniond*>(lua.ToUserdata(1)));
else
break;
return true;
} }
case 2: case 2:
return new Nz::Quaterniond(lua.CheckNumber(1), *(*static_cast<Nz::Vector3d**>(lua.CheckUserdata(2, "Vector3")))); Nz::PlacementNew(quaternion, lua.CheckNumber(1), *(*static_cast<Nz::Vector3d**>(lua.CheckUserdata(2, "Vector3"))));
return true;
case 4: case 4:
return new Nz::Quaterniond(lua.CheckNumber(1), lua.CheckNumber(2), lua.CheckNumber(3), lua.CheckNumber(4)); Nz::PlacementNew(quaternion, lua.CheckNumber(1), lua.CheckNumber(2), lua.CheckNumber(3), lua.CheckNumber(4));
return true;
} }
lua.Error("No matching overload for Quaternion constructor"); lua.Error("No matching overload for Quaternion constructor");
return nullptr; return false;
}); });
quaternionClass.BindMethod("__tostring", &Nz::Quaterniond::ToString); quaternionClass.BindMethod("__tostring", &Nz::Quaterniond::ToString);
@ -238,28 +249,31 @@ namespace Ndk
}); });
/*********************************** Nz::Vector2 **********************************/ /*********************************** Nz::Vector2 **********************************/
vector2dClass.SetConstructor([](Nz::LuaInstance& lua) -> Nz::Vector2d* vector2dClass.SetConstructor([](Nz::LuaInstance& lua, Nz::Vector2d* vector)
{ {
unsigned int argCount = std::min(lua.GetStackTop(), 2U); unsigned int argCount = std::min(lua.GetStackTop(), 2U);
switch (argCount) switch (argCount)
{ {
case 0: case 0:
case 2: case 2:
return new Nz::Vector2d(lua.CheckNumber(1, 0.0), lua.CheckNumber(2, 0.0)); Nz::PlacementNew(vector, lua.CheckNumber(1, 0.0), lua.CheckNumber(2, 0.0));
return true;
case 1: case 1:
{ {
if (lua.IsOfType(1, Nz::LuaType_Number)) if (lua.IsOfType(1, Nz::LuaType_Number))
return new Nz::Vector2d(lua.CheckNumber(1)); Nz::PlacementNew(vector, lua.CheckNumber(1));
else if (lua.IsOfType(1, "Vector2")) else if (lua.IsOfType(1, "Vector2"))
return new Nz::Vector2d(*(*static_cast<Nz::Vector2d**>(lua.ToUserdata(1)))); Nz::PlacementNew(vector, *static_cast<Nz::Vector2d*>(lua.ToUserdata(1)));
else
break;
break; return true;
} }
} }
lua.Error("No matching overload for Vector2 constructor"); lua.Error("No matching overload for Vector2 constructor");
return nullptr; return false;
}); });
vector2dClass.BindMethod("__tostring", &Nz::Vector2d::ToString); vector2dClass.BindMethod("__tostring", &Nz::Vector2d::ToString);
@ -345,40 +359,44 @@ namespace Ndk
}); });
/*********************************** Nz::Vector3 **********************************/ /*********************************** Nz::Vector3 **********************************/
vector3dClass.SetConstructor([] (Nz::LuaInstance& lua) -> Nz::Vector3d* vector3dClass.SetConstructor([] (Nz::LuaInstance& lua, Nz::Vector3d* vector)
{ {
unsigned int argCount = std::min(lua.GetStackTop(), 3U); unsigned int argCount = std::min(lua.GetStackTop(), 3U);
switch (argCount) switch (argCount)
{ {
case 0: case 0:
case 3: case 3:
return new Nz::Vector3d(lua.CheckNumber(1, 0.0), lua.CheckNumber(2, 0.0), lua.CheckNumber(3, 0.0)); Nz::PlacementNew(vector, lua.CheckNumber(1, 0.0), lua.CheckNumber(2, 0.0), lua.CheckNumber(3, 0.0));
case 1: case 1:
{ {
if (lua.IsOfType(1, Nz::LuaType_Number)) if (lua.IsOfType(1, Nz::LuaType_Number))
return new Nz::Vector3d(lua.CheckNumber(1), *(*static_cast<Nz::Vector2d**>(lua.ToUserdata(1)))); Nz::PlacementNew(vector, lua.CheckNumber(1), *static_cast<Nz::Vector2d*>(lua.ToUserdata(1)));
else if (lua.IsOfType(1, "Vector2")) else if (lua.IsOfType(1, "Vector2"))
return new Nz::Vector3d(*(*static_cast<Nz::Vector2d**>(lua.ToUserdata(1)))); Nz::PlacementNew(vector, *static_cast<Nz::Vector2d*>(lua.ToUserdata(1)));
else if (lua.IsOfType(1, "Vector3")) else if (lua.IsOfType(1, "Vector3"))
return new Nz::Vector3d(*(*static_cast<Nz::Vector3d**>(lua.ToUserdata(1)))); Nz::PlacementNew(vector, *static_cast<Nz::Vector3d*>(lua.ToUserdata(1)));
else
break;
break; return true;
} }
case 2: case 2:
{ {
if (lua.IsOfType(1, Nz::LuaType_Number)) if (lua.IsOfType(1, Nz::LuaType_Number))
return new Nz::Vector3d(lua.CheckNumber(1), *(*static_cast<Nz::Vector2d**>(lua.CheckUserdata(1, "Vector2")))); Nz::PlacementNew(vector, lua.CheckNumber(1), *static_cast<Nz::Vector2d*>(lua.CheckUserdata(1, "Vector2")));
else if (lua.IsOfType(1, "Vector2")) else if (lua.IsOfType(1, "Vector2"))
return new Nz::Vector3d(*(*static_cast<Nz::Vector2d**>(lua.ToUserdata(1))), lua.CheckNumber(2)); Nz::PlacementNew(vector, *static_cast<Nz::Vector2d*>(lua.ToUserdata(1)), lua.CheckNumber(2));
else
break;
break; return true;
} }
} }
lua.Error("No matching overload for constructor"); lua.Error("No matching overload for constructor");
return nullptr; return false;
}); });
vector3dClass.BindMethod("__tostring", &Nz::Vector3d::ToString); vector3dClass.BindMethod("__tostring", &Nz::Vector3d::ToString);

View File

@ -17,7 +17,7 @@ namespace Ndk
abstractSocketClass.BindMethod("QueryAvailableBytes", &Nz::AbstractSocket::QueryAvailableBytes); abstractSocketClass.BindMethod("QueryAvailableBytes", &Nz::AbstractSocket::QueryAvailableBytes);
/*********************************** Nz::IpAddress **********************************/ /*********************************** Nz::IpAddress **********************************/
ipAddressClass.SetConstructor([] (Nz::LuaInstance& lua) -> Nz::IpAddress* ipAddressClass.SetConstructor([] (Nz::LuaInstance& lua, Nz::IpAddress* address)
{ {
unsigned int argCount = std::min(lua.GetStackTop(), 9U); unsigned int argCount = std::min(lua.GetStackTop(), 9U);
@ -25,30 +25,54 @@ namespace Ndk
switch (argCount) switch (argCount)
{ {
case 0: case 0:
return new Nz::IpAddress; Nz::PlacementNew(address);
return true;
case 1: case 1:
return new Nz::IpAddress(lua.CheckString(argIndex)); Nz::PlacementNew(address, lua.CheckString(argIndex));
return true;
case 4: case 4:
case 5: case 5:
return new Nz::IpAddress(lua.Check<Nz::UInt8>(&argIndex), lua.Check<Nz::UInt8>(&argIndex), lua.Check<Nz::UInt8>(&argIndex), lua.Check<Nz::UInt8>(&argIndex), lua.Check<Nz::UInt16>(&argIndex, 0)); {
Nz::UInt8 a = lua.Check<Nz::UInt8>(&argIndex);
Nz::UInt8 b = lua.Check<Nz::UInt8>(&argIndex);
Nz::UInt8 c = lua.Check<Nz::UInt8>(&argIndex);
Nz::UInt8 d = lua.Check<Nz::UInt8>(&argIndex);
Nz::UInt16 port = lua.Check<Nz::UInt16>(&argIndex, 0);
Nz::PlacementNew(address, a, b, c, d, port);
return true;
}
case 8: case 8:
case 9: case 9:
return new Nz::IpAddress(lua.Check<Nz::UInt16>(&argIndex), lua.Check<Nz::UInt16>(&argIndex), lua.Check<Nz::UInt16>(&argIndex), lua.Check<Nz::UInt16>(&argIndex), {
lua.Check<Nz::UInt16>(&argIndex), lua.Check<Nz::UInt16>(&argIndex), lua.Check<Nz::UInt16>(&argIndex), lua.Check<Nz::UInt16>(&argIndex), lua.Check<Nz::UInt16>(&argIndex, 0)); Nz::UInt16 a = lua.Check<Nz::UInt16>(&argIndex);
Nz::UInt16 b = lua.Check<Nz::UInt16>(&argIndex);
Nz::UInt16 c = lua.Check<Nz::UInt16>(&argIndex);
Nz::UInt16 d = lua.Check<Nz::UInt16>(&argIndex);
Nz::UInt16 e = lua.Check<Nz::UInt16>(&argIndex);
Nz::UInt16 f = lua.Check<Nz::UInt16>(&argIndex);
Nz::UInt16 g = lua.Check<Nz::UInt16>(&argIndex);
Nz::UInt16 h = lua.Check<Nz::UInt16>(&argIndex);
Nz::UInt16 port = lua.Check<Nz::UInt16>(&argIndex, 0);
Nz::PlacementNew(address, a, b, c, d, e, f, g, h, port);
return true;
}
} }
return nullptr; lua.Error("No matching overload for constructor");
return false;
}); });
ipAddressClass.BindMethod("GetPort", &Nz::IpAddress::GetPort); ipAddressClass.BindMethod("GetPort", &Nz::IpAddress::GetPort);
ipAddressClass.BindMethod("GetProtocol", &Nz::IpAddress::GetProtocol); ipAddressClass.BindMethod("GetProtocol", &Nz::IpAddress::GetProtocol);
ipAddressClass.BindMethod("IsLoopback", &Nz::IpAddress::IsLoopback); ipAddressClass.BindMethod("IsLoopback", &Nz::IpAddress::IsLoopback);
ipAddressClass.BindMethod("IsValid", &Nz::IpAddress::IsValid); ipAddressClass.BindMethod("IsValid", &Nz::IpAddress::IsValid);
ipAddressClass.BindMethod("ToUInt32", &Nz::IpAddress::ToUInt32); ipAddressClass.BindMethod("ToUInt32", &Nz::IpAddress::ToUInt32);
ipAddressClass.BindMethod("__tostring", &Nz::IpAddress::ToString); ipAddressClass.BindMethod("__tostring", &Nz::IpAddress::ToString);
ipAddressClass.BindStaticMethod("ResolveAddress", [] (Nz::LuaInstance& instance) -> int ipAddressClass.BindStaticMethod("ResolveAddress", [] (Nz::LuaInstance& instance) -> int
{ {
@ -79,7 +103,7 @@ namespace Ndk
int argIndex = 1; int argIndex = 1;
Nz::NetProtocol protocol = instance.Check<Nz::NetProtocol>(&argIndex); Nz::NetProtocol protocol = instance.Check<Nz::NetProtocol>(&argIndex);
Nz::String hostname = instance.Check<Nz::String>(&argIndex); Nz::String hostname = instance.Check<Nz::String>(&argIndex);
Nz::String service = instance.Check<Nz::String>(&argIndex, "http"); Nz::String service = instance.Check<Nz::String>(&argIndex, "http");
std::vector<Nz::HostnameInfo> addresses = Nz::IpAddress::ResolveHostname(protocol, hostname, service, &error); std::vector<Nz::HostnameInfo> addresses = Nz::IpAddress::ResolveHostname(protocol, hostname, service, &error);
if (error == Nz::ResolveError_NoError) if (error == Nz::ResolveError_NoError)

View File

@ -89,9 +89,10 @@ namespace Ndk
}); });
/*********************************** Nz::Font **********************************/ /*********************************** Nz::Font **********************************/
fontClass.SetConstructor([] (Nz::LuaInstance& lua) -> Nz::FontRef* fontClass.SetConstructor([] (Nz::LuaInstance& lua, Nz::FontRef* font)
{ {
return new Nz::FontRef(new Nz::Font); Nz::PlacementNew(font, Nz::Font::New());
return true;
}); });
fontClass.BindMethod("ClearGlyphCache", &Nz::Font::ClearGlyphCache); fontClass.BindMethod("ClearGlyphCache", &Nz::Font::ClearGlyphCache);

View File

@ -28,7 +28,7 @@ namespace Nz
public: public:
using ClassFunc = std::function<int(LuaInstance& lua, T& instance)>; using ClassFunc = std::function<int(LuaInstance& lua, T& instance)>;
using ClassIndexFunc = std::function<bool(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*)>; template<typename P> using ConvertToParent = std::function<P*(T*)>;
using FinalizerFunc = std::function<bool(LuaInstance& lua, T& instance)>; using FinalizerFunc = std::function<bool(LuaInstance& lua, T& instance)>;
using StaticIndexFunc = std::function<bool(LuaInstance& lua)>; using StaticIndexFunc = std::function<bool(LuaInstance& lua)>;
@ -36,6 +36,8 @@ namespace Nz
LuaClass(const String& name); LuaClass(const String& name);
void BindDefaultConstructor();
void BindMethod(const String& name, ClassFunc method); 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...), 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); 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);

View File

@ -16,6 +16,16 @@ namespace Nz
m_info->name = name; 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 T>
template<class P> template<class P>
inline void LuaClass<T>::Inherit(LuaClass<P>& parent) 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* 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) 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) 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 lua.Pop(); // On pop la metatable
@ -322,14 +332,15 @@ namespace Nz
lua.Remove(1); // On enlève l'argument "table" du stack lua.Remove(1); // On enlève l'argument "table" du stack
T* instance = constructor(lua); T* instance = static_cast<T*>(lua.PushUserdata(sizeof(T)));
if (!instance)
if (!constructor(lua, instance))
{ {
lua.Error("Constructor failed"); lua.Error("Constructor failed");
return 0; // Normalement jamais exécuté (l'erreur provoquant une exception) return 0; // Normalement jamais exécuté (l'erreur provoquant une exception)
} }
lua.PushInstance(info->name.GetConstBuffer(), instance); lua.SetMetatable(info->name);
return 1; return 1;
} }
@ -341,11 +352,11 @@ namespace Nz
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<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; 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 lua.Remove(1); //< Remove the instance from the Lua stack
if (!finalizer || finalizer(lua, *instance)) if (!finalizer || finalizer(lua, *instance))
delete instance; instance->~T();
return 0; return 0;
} }
@ -399,7 +410,7 @@ namespace Nz
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(lua.ToUserdata(lua.GetIndexOfUpValue(1))); 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 lua.Remove(1); //< Remove the instance from the Lua stack
Get(info, lua, instance); 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))); std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
const ClassIndexFunc& setter = info->setter; 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 lua.Remove(1); //< Remove the instance from the Lua stack
if (!setter(lua, instance)) if (!setter(lua, instance))

View File

@ -131,7 +131,8 @@ namespace Nz
template<typename R, typename... Args, typename... DefArgs> void PushFunction(R(*func)(Args...), DefArgs&&... defArgs) const; 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 char* name, T&& arg);
template<typename T> void PushGlobal(const String& 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; template<typename T, typename... Args> void PushInstance(const char* tname, Args&&... args) const;
void PushInteger(long long value) const; void PushInteger(long long value) const;
void PushLightUserdata(void* value) const; void PushLightUserdata(void* value) const;

View File

@ -156,21 +156,21 @@ namespace Nz
return LuaImplReplyVal(instance, val, TypeTag<T>()); 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()); instance.PushString(val.c_str(), val.size());
return 1; return 1;
} }
template<typename T> 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; std::size_t index = 1;
instance.PushTable(valContainer.size()); instance.PushTable(valContainer.size());
for (const T& val : valContainer) for (T& val : valContainer)
{ {
instance.PushInteger(index++); 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"); instance.Error("Couldn't create table: type need more than one place to store");
return 0; return 0;
@ -181,20 +181,20 @@ namespace Nz
return 1; 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()); instance.PushString(reinterpret_cast<const char*>(val.GetConstBuffer()), val.GetSize());
return 1; 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)); instance.PushString(std::move(val));
return 1; return 1;
} }
template<typename T1, typename T2> 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; int retVal = 0;
@ -593,17 +593,30 @@ namespace Nz
} }
template<typename T> 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*))); T* userdata = static_cast<T*>(PushUserdata(sizeof(T*)));
*userdata = instance; 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); SetMetatable(tname);
} }
template<typename T, typename... Args> template<typename T, typename... Args>
void LuaInstance::PushInstance(const char* tname, Args&&... args) const 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> template<typename T>