std::vector and oder containers as userdata #176
Replies: 4 comments 4 replies
-
I can try to come up with some code later how to do it easily. |
Beta Was this translation helpful? Give feedback.
-
I can give it a try myself, could be fun. I just need a starting point. |
Beta Was this translation helpful? Give feedback.
-
An initial starting point: #include <vector>
template <class T>
void registerVector(luabridge::Namespace luaNamespace, const char* className)
{
using U = std::vector<T>;
luaNamespace
.beginClass<U>(className)
.addConstructor([](void* ptr) { return new(ptr) U(); })
.addFunction("push_back", luabridge::nonConstOverload<const float&>(&U::push_back))
.addFunction("at", luabridge::constOverload<std::size_t>(&U::at))
.addFunction("size", &U::size)
.endClass()
.endNamespace();
}
TEST_F(VectorTests, RegisterCustomTest)
{
auto localNamespace = luabridge::getGlobalNamespace(L).beginNamespace("test");
registerVector<float>(std::move(localNamespace), "FloatVector");
runLua("a = test.FloatVector(); a:push_back(1.0); result = a:size()");
EXPECT_EQ(1, result<int>());
} |
Beta Was this translation helpful? Give feedback.
-
There are some tests that show how to deal with __index and __newindex metamethods registration https://github.com/kunitoki/LuaBridge3/blob/master/Tests/Source/ClassTests.cpp#L2328 You'll probably have to register a lambda that matches the way lua table indexing work. As this is a request, i will look at providing these stl containers as userdata registration methods. |
Beta Was this translation helpful? Give feedback.
-
So I'd like to expose automatically std::vector and similar to luabridge, but not as lua table (like including Vector.h) (which incurs in a lot of copies and performance impacts for big vectors), but just a userdata with accessors.
Is there a way to do this generically without having to register every single instance manually?
Also, ideally I would also support the same syntax lua tables, I think it is easy to overload # and index metamethods, however, if I want to implement ipairs support I need to have multiple return values from luabridge.
Any idea about how best to do it?
Beta Was this translation helpful? Give feedback.
All reactions