0

I'm trying to export my vectors class

            .beginClass<Vector>("Vector")
            .addConstructor<void(*)()>()
            .addConstructor<void(*)(float, float, float)>()
            .addFunction("__eq", &Vector::operator==)
            .addFunction("__add", &Vector::operator+)
            .addData("x", &Vector::x)
            .addData("y", &Vector::y)
            .addData("z", &Vector::z)
            .addFunction("Length", &Vector::Length)
            .addFunction("Angle", &Vector::Angle)
            .addFunction("Distance", &Vector::DistTo)
        .endClass()

but when i try and do the other 3 operators, I have multiple overloads for them. How can I specify which one I want to use and is this even possible?

ar1a
  • 374
  • 3
  • 10

3 Answers3

0

If you have a function overloads int MyClass::Func(int) and MyClass* MyClass::Func(MyClass) then you can define the overload to use in the following manner. In this example I chose to use MyClass* MyClass::Func(MyClass) as overload.

.addFunction("Func", (MyClass*(MyClass::*)(MyClass)) &MyClass::Func)

So what happens here is that the function signature is provided with the pointer to the function.

Rochet2
  • 1,146
  • 1
  • 8
  • 13
  • I tried to do that with my operator- but without success `.addFunction("__sub", (Vector(Vector::*)(const Vector&)) &Vector::operator-)` `Vector Vector::operator-(const Vector& v) const { return Vector(x - v.x, y - v.y, z - v.z); }` – ar1a Dec 14 '15 at 07:03
  • @Snorflake Try adding the const too like this: `.addFunction("__sub", (Vector (Vector::* const)(const Vector&)) &Vector::operator-)` – Rochet2 Dec 14 '15 at 10:57
  • still nothing. It's okay, I posted my solution below anyways. – ar1a Dec 14 '15 at 17:31
0

So I just made an add/subtract/multiply/divide function and called that instead. Guess operators just don't want to comply.

ar1a
  • 374
  • 3
  • 10
  • I encountered exactly the same problem, but I managed to solve it: https://stackoverflow.com/questions/49490076/luabridge-binding-overloaded-operators/49498449#49498449. Hope that helps – Ian Young Mar 26 '18 at 18:42
0

infect luabridge can implement this in this way, if you define a class A

.addFunction("__eq", &A::equal )

'equal' should be declared like:

bool A::equal( const A & )

then :

if obj1 == obj2 then
end

'equal' will work!

but if you implement a sub class of A class B : public A

it's will takes you a lot of time!

first you have to Specialization the template class or the template method

luabridge::UserdataValue 

luabridge::UserdataPtr::push(lua_State* const L, T* const p)

the specify which class's (meta)table you need to regist the object or the pointer

you should read luabridge's sourcecode to accomplish this!

Then!

you should regist this function to B again!

.addFunction("__eq", &B::equal )

lua code :

local inst_a = app.new_class_A();
local inst_b = app.new_class_B();
-- this will call the '__eq' in B's metatable
if( inst_b == inst_a )then
end
-- this will call the '__eq' in A's metatable
if( inst_a == inst_b )then
end

while calling __eq, luabridge will not search the class's parent's metadata table, so you should regist again to A's subclass!

hope it will help you! sorry for my poor english!

bhlzlx
  • 1