Make lua binding for std::vector (#164)
* Make LuaImplQueryArg impl for std::vector * Fix shadowed argument * Make unit tests * Bugfix * Bugfix, for real this time * We didn't need these tests anyway * Revert "We didn't need these tests anyway" This reverts commit be88d4496a9cf62beb4d3ce1f30825589a4bacb2. * Add change to changelog * Update ChangeLog.md * Bugfix & use CallOnExit to pop stack
This commit is contained in:
parent
858d0da5f2
commit
da044bd21c
|
|
@ -107,6 +107,8 @@ Nazara Engine:
|
||||||
- Added AbstractViewer::Project and AbstractViewer::Unproject methods
|
- Added AbstractViewer::Project and AbstractViewer::Unproject methods
|
||||||
- Added AbstractViewer::ProjectDepth method
|
- Added AbstractViewer::ProjectDepth method
|
||||||
- Fixed SocketPoller not be able to recover from some errors (like invalid sockets and such)
|
- Fixed SocketPoller not be able to recover from some errors (like invalid sockets and such)
|
||||||
|
- Add LuaImplQuery implementation for std::vector
|
||||||
|
- Fixed LuaState::PushGlobal & LuaState::PushField to copy the object before moving it
|
||||||
- ⚠️ Replaced currentBitPos and currentByte fields by [read|write][BitPos][Byte] to handle properly bit reading/writing.
|
- ⚠️ Replaced currentBitPos and currentByte fields by [read|write][BitPos][Byte] to handle properly bit reading/writing.
|
||||||
- InstancedRenderable::SetMaterial methods are now public.
|
- InstancedRenderable::SetMaterial methods are now public.
|
||||||
- Fixed Model copy constructor not copying materials
|
- Fixed Model copy constructor not copying materials
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
#include <Nazara/Core/Algorithm.hpp>
|
#include <Nazara/Core/Algorithm.hpp>
|
||||||
|
#include <Nazara/Core/CallOnExit.hpp>
|
||||||
#include <Nazara/Core/Flags.hpp>
|
#include <Nazara/Core/Flags.hpp>
|
||||||
#include <Nazara/Core/MemoryHelper.hpp>
|
#include <Nazara/Core/MemoryHelper.hpp>
|
||||||
#include <Nazara/Core/StringStream.hpp>
|
#include <Nazara/Core/StringStream.hpp>
|
||||||
|
|
@ -159,6 +160,34 @@ namespace Nz
|
||||||
return LuaImplQueryArg(instance, index, arg, defValue, TypeTag<T>());
|
return LuaImplQueryArg(instance, index, arg, defValue, TypeTag<T>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
unsigned int LuaImplQueryArg(const LuaState& instance, int index, std::vector<T>* container, TypeTag<std::vector<T>>)
|
||||||
|
{
|
||||||
|
instance.CheckType(index, Nz::LuaType_Table);
|
||||||
|
std::size_t pos = 1;
|
||||||
|
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
Nz::CallOnExit popStack { [&instance]() { instance.Pop(); } };
|
||||||
|
instance.PushInteger(pos++);
|
||||||
|
|
||||||
|
if (instance.GetTable() == Nz::LuaType_Nil)
|
||||||
|
break;
|
||||||
|
|
||||||
|
T arg {};
|
||||||
|
|
||||||
|
if (LuaImplQueryArg(instance, -1, &arg, TypeTag<T>()) != 1)
|
||||||
|
{
|
||||||
|
instance.Error("Type needs more than one place to be initialized");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
container->push_back(arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
// Function returns
|
// Function returns
|
||||||
inline int LuaImplReplyVal(const LuaState& instance, bool val, TypeTag<bool>)
|
inline int LuaImplReplyVal(const LuaState& instance, bool val, TypeTag<bool>)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -183,5 +183,34 @@ SCENARIO("LuaState", "[LUA][LUASTATE]")
|
||||||
CHECK(luaInstance.ToNumber(1) == Approx(-4.09).margin(0.1));
|
CHECK(luaInstance.ToNumber(1) == Approx(-4.09).margin(0.1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WHEN("We push a std::vector locally")
|
||||||
|
{
|
||||||
|
std::vector<int> vec { 1, 5, -8, 6, -4 };
|
||||||
|
luaInstance.Push(std::vector<int> { vec });
|
||||||
|
|
||||||
|
THEN("We can retrieve it with correct values")
|
||||||
|
{
|
||||||
|
int index = 1;
|
||||||
|
std::vector<int> otherVec = luaInstance.Check<std::vector<int>>(&index);
|
||||||
|
|
||||||
|
for (std::size_t i {}; i < otherVec.size(); ++i)
|
||||||
|
CHECK(otherVec[i] == vec[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
WHEN("We push a std::vector globally")
|
||||||
|
{
|
||||||
|
std::vector<int> vec { 1, 5, -8, 6, -4 };
|
||||||
|
luaInstance.PushGlobal("vector", std::vector<int> { vec });
|
||||||
|
|
||||||
|
THEN("We can retrieve it with correct values")
|
||||||
|
{
|
||||||
|
std::vector<int> otherVec = luaInstance.CheckGlobal<std::vector<int>>("vector");
|
||||||
|
|
||||||
|
for (std::size_t i {}; i < otherVec.size(); ++i)
|
||||||
|
CHECK(otherVec[i] == vec[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue