类似 Luabind-syntax(索引运算符)

Luabind-like syntax (index operator)

本文关键字:运算符 索引 Luabind-syntax 类似      更新时间:2023-10-16

目前我正在试验Luabind-Library,我偶然发现了它的调用语法。它的行为和工作方式与预期一样,但不知何故,我不明白它为什么或如何做到。
有问题的代码如下:

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"
];

更具体地说,我不明白方括号中发生了什么。为什么会这样?我试图通过Luabind的源代码阅读,可惜没有成功。我也试图重建这种行为,但也没有成功。
那么,我错过了一些非常明显的东西吗?

提前感谢!

  • luabind::module是一个函数,它返回类型 luabind::module_ ,它有一个重载的 [] 运算符,采用类型 luabind::scope 的参数。
  • luabind::class_是一个类,它有一个接受类型 const char* 的构造函数和一个返回 class_& 的成员函数def,因此可以链接对def的调用。
  • luabind::class_是从一个名为 luabind::detail::class_base 的类派生的,该类派生自 luabind::scope,因此返回的最终class_可以转换为 scope 并作为参数传递给 luabind::module_::operator[]