c++lualuabind

C++11 luabind integration, function failing


I'm experimenting with integrating LUA into my program with luabind, but I've hit a big stumbling block.

I'm very unfamiliar with LUA's calling conventions and I feel like I'm missing something simple.

Here is my C++ code:

struct app_t
{
    //...
    void exit();
    void reset();

    resource_mgr_t resources;
    //...
};

struct resource_mgr_t
{
    //...
    void prune();
    void purge();
    //...
};

extern app_t app;

And my luabind modules:

luabind::module(state)
    [
        luabind::class_<resource_mgr_t>("resource_mgr")
        .def("prune", &resource_mgr_t::prune)
        .def("purge", &resource_mgr_t::purge)
    ];

luabind::module(state)
    [
        luabind::class_<app_t>("app")
        .def("exit", &app_t::exit)
        .def("reset", &app_t::reset)
        .def_readonly("resources", &app_t::resources)
    ];

luabind::globals(state)["app"] = &app;

I can execute the following lua commands just fine:

app:exit()
app:reset()

However, the following call fails:

app.resources:purge()

With the following error:

[string "app.resources:purge()"]:1: attempt to index field 'resources' (a function value)

Any help is very much appreciated!


Solution

  • When binding members that are a non-primitive type, the auto generated getter function will return a reference to it.

    And, just as in app:reset(), resources is an instance member field.

    So, use it like this:

    app:resources():purge()