satoren / kaguya

C++ binding to Lua
Boost Software License 1.0
345 stars 70 forks source link

Is there any way to register overloaded method #24

Closed Sygmei closed 8 years ago

Sygmei commented 8 years ago

Can I do an "addMember" of an overloaded method in kaguya ? If not, is it possible to add this feature ?

satoren commented 8 years ago

Yes. can it.

please try addMemberFunction to same name

struct Foo {
    void func() { std::cout << 1 <<std::endl; }
    void func(int param) { std::cout << param <<std::endl; }
};
state["Foo"].setClass(kaguya::ClassMetatable<Foo>()
    .addMemberFunction("func", static_cast<void (Foo::*)()>(&Foo::func))
    .addMemberFunction("func", static_cast<void (Foo::*)(int)>(&Foo::func))
);
state["foo"] = Foo();
state("foo:func()");//1
state("foo:func(64)");//64
);

or addStaticField with kaguya::overload()

state["Foo"].setClass(kaguya::ClassMetatable<Foo>()
    .addStaticField("func", kaguya::overload(static_cast<void (Foo::*)()>(&Foo::func) , static_cast<void (Foo::*)(int)>(&Foo::func))
);
Sygmei commented 8 years ago

Thanks :)