Search code examples
c++c++11lualuabind

Luabind-like syntax (index operator)


Currently im experimenting with the Luabind-Library and I stumbled upon its calling syntax. It behaves and works like expected, but somehow I can not understand why or how it does.
The code in question is following:

Class Animation
{
    std::vector frames;
public:
    Animation(){}
    ~Animation(){}
    addFrame(const Texture2D *in_image);
};

//Somewhere else
luabind::module(LuaState)
[
 luabind::class_("Animation")    // < "Animation" how we want to name the Class in the Lua runtime
 .def(luabind::constructor<>())             // < Binds the empty constructor
 .def("addFrame", &Animation::addFrame)     // < Binds the &Animation::addFrame method to lua with the name "addFrame"
];

To be more specific, I don't understand what's happening in the square brackets. Why does this work? I tried to read trough the source code of Luabind, sadly without success. I also tried to reconstruct this behaviour, which was also unsuccessful.
So, am I missing something very obvious?

Thanks in advance!


Solution

    • luabind::module is a function, and it returns type luabind::module_, which has an overloaded [] operator taking an argument of type luabind::scope.
    • luabind::class_ is a class and it has a constructor that takes type const char* and a member function def which returns class_& so calls to def can be chained.
    • luabind::class_ is derived from a class called luabind::detail::class_base, which is derived from luabind::scope, so the final class_ returned can be converted to scope and passed as an argument to luabind::module_::operator[].