Advice on wrapping a full C++ class hierarchy?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Advice on wrapping a full C++ class hierarchy?

Sean Barton
I'm running into some issues wrapping up a C++ class hierarchy for luabind.

Let's say I have two C++ classes:

struct Foo
{
    Foo (const std::string& name) : mName(name) {}
    virtual ~Foo (void) {}

    const std::string& GetName (void)
    {
        return mName;
    }
};

struct Bar : Foo
{
    Bar (const std::string& name) : Foo(name) {}
    virtual ~Bar (void) {}
};


And I wrap them up for usage by Luabind like so ...


struct FooWrapper : public Foo, public luabind::wrap_base
{
    FooWrapper (const std::string& name) : Foo(name) {}
};

struct BarWrapper : public Bar, public luabind::wrap_base
{
    BarWrapper (const std::string& name) : Bar(name) {}
};


And I have a C++ function that "manipulates" a Foo object like so ...


void PrintName (Foo* foo)
{
    printf("Name = %s\n", foo->GetName().c_str());
}


And for completion, my registration in C++ ...

module(luaState)
[
    class_<Foo, FooWrapper>("Foo")
        .def(constructor<const std::string&>()),

    class_<Bar, BarWrapper>("Bar")
        .def(constructor<const std::string&>()),

    def("PrintName", &PrintName)

];


Now I attempt to use these classes and PrintName function in Lua like so ...

    foo = Foo('myFoo')
    bar = Bar('myBar')   
   
    PrintName(foo)
    PrintName(bar)   


And I get the following error:

"no match for function call 'PrintName' with the parameters (Bar)
candidates are:
PrintName(Foo*)"


I was hoping this would work seeing how Bar inherits from Foo.

Incidently, I can get this to work by registering the PrintName function twice like so ...

    def("PrintName",        (void (*) (Foo*)) &PrintName),
    def("PrintName",        (void (*) (Bar*)) &PrintName)

... but I'd have to add that for every class in my hierarchy, and it won't support other classes that I may construct in Lua that also inherit from Foo.

Any suggestions?  Is there a rule of thumb when you wrap a whole hierarchy?

Thanks.




_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Advice on wrapping a full C++ class hierarchy?

Josh Green-3
----Original Message Follows----
From: "Sean Barton" <[hidden email]>

Your registration looks like this...

module(luaState)
[
    class_<Foo, FooWrapper>("Foo")
        .def(constructor<const std::string&>()),

    class_<Bar, BarWrapper>("Bar")
        .def(constructor<const std::string&>()),

    def("PrintName", &PrintName)

];


As far as I remember, in order to get luabind to recognise that the classes
are in a parent child relationship, you must specify the parent type inside
the class_ part...(just as you have put wrapper there).. But you should also
be able to put in the class holder type, so I dunno how luabind is working
at the moment... but I suspect if you do this:

module(luaState)
[
    class_<Foo, FooWrapper>("Foo")
        .def(constructor<const std::string&>()),

    class_<Bar, Foo, BarWrapper>("Bar")  //Added in the parent specification
        .def(constructor<const std::string&>()),

    def("PrintName", &PrintName)

];

Then it should work.. Sorry, Havn't been coding lua in the last few weeks so
i might be wrong.

Hope I've helped
--Josh




_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Advice on wrapping a full C++ class hierarchy?

Sean Barton
Thank-you very much, that worked.

I actually tried something similar ...

class_<Bar, bases<Foo, BarWrapper> >("Bar")

.. but that didn't work.  I guess in my example I was explicitly saying that Bar inherited from both Foo and BarWrapper which I see now is wrong.  :)

Thanks again!



On 6/7/06, Josh Green <[hidden email]> wrote:
----Original Message Follows----
From: "Sean Barton" <[hidden email]>

Your registration looks like this...

module(luaState)
[
    class_<Foo, FooWrapper>("Foo")
        .def(constructor<const std::string&>()),

    class_<Bar, BarWrapper>("Bar")
        .def(constructor<const std::string&>()),

    def("PrintName", &PrintName)

];


As far as I remember, in order to get luabind to recognise that the classes
are in a parent child relationship, you must specify the parent type inside
the class_ part...(just as you have put wrapper there).. But you should also
be able to put in the class holder type, so I dunno how luabind is working
at the moment... but I suspect if you do this:

module(luaState)
[
    class_<Foo, FooWrapper>("Foo")
        .def(constructor<const std::string&>()),

    class_<Bar, Foo, BarWrapper>("Bar")  //Added in the parent specification
        .def(constructor<const std::string&>()),

    def("PrintName", &PrintName)

];

Then it should work.. Sorry, Havn't been coding lua in the last few weeks so
i might be wrong.

Hope I've helped
--Josh




_______________________________________________
luabind-user mailing list
[hidden email]
<a href="https://lists.sourceforge.net/lists/listinfo/luabind-user" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)"> https://lists.sourceforge.net/lists/listinfo/luabind-user



_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user