luwra, Lua的最小开销 C++ 包装器

分享于 

5分钟阅读

GitHub

  繁體 雙語
Minimal-overhead Lua wrapper for C++.
  • 源代码名称:luwra
  • 源代码网址:http://www.github.com/vapourismo/luwra
  • luwra源代码文档
  • luwra源代码下载
  • Git URL:
    git://www.github.com/vapourismo/luwra.git
    Git Clone代码到本地:
    git clone http://www.github.com/vapourismo/luwra
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/vapourismo/luwra
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    

    Build Status

    Luwra

    只提供了最少开销的头 C++ 库,它提供了。

    用法

    请参阅文档。 为了使用这个库,你必须克隆这个库并包含 lib/luwra.hpp

    有问题给我发电子邮件或者打开问题。

    示例

    在下面的示例中,lua 引用了 lua_State*的一个实例。

    轻松将值推送到堆栈上:

    // Push an integerluwra::push(lua, 1338);// Push a numberluwra::push(lua, 13.37);// Push a booleanluwra::push(lua, false);// Push a stringluwra::push(lua, "Hello World");

    或者检索它们:

    // Your functionintmy_fun(int a, int b) {
     return a + b;
    }// Prepare stackluwra::push(lua, 13);luwra::push(lua, 37);// Read manuallyint a = luwra::read<int>(lua, 1);// Read with deduced typeint b = luwra::read(lua, -1);// Apply your functionint result = luwra::apply(lua, my_fun);// which is equivalent toint result = luwra::apply(lua, 1, my_fun);// and equivalent toint result = luwra::apply(lua, -2, my_fun);// All of this is essentially syntactic sugar forint result = my_fun(luwra::read(lua, 1), luwra::read(lua, 2));// You can also return the result to the stackluwra::map(lua, 1, my_fun);int result = luwra::read(state, -1);

    生成一个可以由Lua使用的C 函数:

    // Assuming your function looks something like thisintmy_function(constchar* a, int b) {
     //.. .}// Convert to lua_CFunctionlua_CFunction cfun = LUWRA_WRAP(my_function);// Do something with it, for example set it as a Lua global functionluwra::setGlobal(lua, "my_function", cfun);
    -- Invoke the registered functionlocal my_result =my_function("Hello World", 1337)print(my_result)

    或者 register 你自己的类:

    structPoint {
     double x, y;
     Point(double x, double y):
     x(x), y(y)
     {
     std::cout <<"Construct Point(" <<x <<", " <<y <<")" <<std::endl;
     }
     ~Point() {
     std::cout <<"Destruct Point(" <<x <<", " <<y <<")" <<std::endl;
     }
     voidscale(double f) {
     x *= f;
     y *= f;
     }
     std::string __tostring() {
     return"<Point(" + std::to_string(x) + ", " + std::to_string(y) + ")>";
     }
    };// Register the metatable and constructorluwra::registerUserType<Point(double, double)>(
     lua,
     // Constructor name"Point",
     // Methods need to be declared here {
     LUWRA_MEMBER(Point, scale),
     LUWRA_MEMBER(Point, x),
     LUWRA_MEMBER(Point, y)
     },
     // Meta methods may be registered aswell {
     LUWRA_MEMBER(Point, __tostring)
     }
    );
    -- Instantiate 'Point'local point =Point(13, 37)-- Invoke 'scale' methodpoint:scale(1.5)-- Convert to string via '__tostring' meta methodprint(point)-- Read properties 'x' and 'y'print(point:x(), point:y())-- Set property 'x'point:x(4.2)

    要求

    你需要一个C++11-compliant编译器,至少需要Lua才能使这个库正常工作。 我建议使用 Lua 5.3或者更高版本,以避免混乱的lua_Integer 情况。 LuaJIT 2.0似乎也工作。

    测试

    Makefile 允许你分别使用 make examplesmake test 运行示例和测试。 你可能需要调整 LUA_* 变量,因此Luwra会找到Lua头和库。

    假设所有头都位于 /usr/include/lua5.3 中并且共享对象名是 liblua5.3.so,则需要调用以下方法:

    
    make LUA_INCDIR=/usr/include/lua5.3 LUA_LIBNAME=lua5.3 test
    
    
    
    

    WRAP  换行  Minim  minima  LUA  Overhead  
    相关文章