• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

radiotail/eluna: simple library to bind C/C++ and Lua(support lua 5.3/5.2/5.1)

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

radiotail/eluna

开源软件地址(OpenSource Url):

https://github.com/radiotail/eluna

开源编程语言(OpenSource Language):

C++ 99.0%

开源软件介绍(OpenSource Introduction):

ELuna - Extended Luna

ELuna is a simple library to bind C/C++ and Lua, which just depends on Lua library. It is clear and lightweight. You can include ELuna and Lua in your project to use. My original intention is to do some extension for Luna, so its main idea bases on luna. Users don't have to focus on the details of binding C++ and Lua, but use some simple API provided by ELuna to bind cpp class, method, function or to bind lua function, table.

Before version 1.1, ELuna copied class's all methods to object's table when you create an object, that is the same as Luna. It needn't find method from metatable when you call cpp method, but object creation is very slow. Obviously, this way may be a good chiose when you call its method many times but create object rarely. In order to improve its efficiency, I do some adjustments in later versions. I use a new way to bind method which doesn't copy class's all methods to table, but register a method to class's metatable. When you call this method, ELuna will find this method from metatable by using "index" metamethod. In this way, object creation is very fast while calling method is a little slower than before. Finally, how to choose the version depends on your requirement.

After many versions, ELuna has been stable at present. However, maybe there are still some bugs that I didn't find. Please contact me when you find any problems. Of course, ELuna also can't support all the features, such as binding member data and so on. The main reason is that they were either used rarely or implemented easily by other ways during my past project. For example, we can register a "get" and "set" method to take the place of binding the member data. If you really need these features, it is simple to add by yourself.

SUPPORTED PLATFORMS

Windows, Unix, OS X.

SAMPLE

There is a simple sample to show you how to use ELuna. In this sample, I will show you how to register a class(using ELuna::registerClass), a method (using ELuna::registerMethod) or a function(using ELuna::registerFunction) to lua, and how to register a lua function(using ELuna::LuaFunction) or table(using ELuna::LuaTable) to cpp.

//sample.cpp 
#include <stdio.h> 
#include <string.h>
#include "ELuna.h"

//define a class
class CPPClass {
public:
    CPPClass(const char* name): m_name(name) {
            printf("%s Constructor!\n", name);
    }
    ~CPPClass(){
            printf("%s Destructor!\n", m_name);
    }

    //define method
    void cppPrint(const char* word) {
            printf("%s: %s\n", m_name, word);
    }

    int cppSum(int a, int b){
            return a + b;
    }
private:
    const char* m_name;
};

//define function
void cppPrint(char* str) {
    printf("cppPrint: %s\n", str);
}

int cppSum(int a, int b){
    return a + b;
}

void testCPP(lua_State* L) {
    //register a class and it's constructor. indicate all constructor's param type
    ELuna::registerClass<CPPClass>(L, "CPPClass", ELuna::constructor<CPPClass, const char* >);
    //register a method
    ELuna::registerMethod<CPPClass>(L, "cppPrint", &CPPClass::cppPrint);
    ELuna::registerMethod<CPPClass>(L, "cppSum", &CPPClass::cppSum);

    //register a function
    ELuna::registerFunction(L, "cppPrint", &cppPrint);
    ELuna::registerFunction(L, "cppSum", &cppSum);
}

void testLua(lua_State* L) {
    //new a LuaFunction object to bind lua function. indicate return value type
    ELuna::LuaFunction<void> luaPrint(L, "luaPrint");
    ELuna::LuaFunction<int> luaSum(L, "luaSum");

    //run Luafunction's () to call lua function
    luaPrint("hello world");
    printf("luaSum: %d\n", luaSum(1,2));

    //register a lua table
    ELuna::LuaTable luaTable(L, "luaTable");
    //set table's key and value
    luaTable.set(2, "world");
    printf("LuaTable: %s %s\n", luaTable.get<char*>(1), luaTable.get<char*>(2));
}

const char *fileName = "sample.lua";

int main() {
    lua_State *L = ELuna::openLua();

    testCPP(L);

    ELuna::doFile(L, fileName);

    testLua(L);
    ELuna::closeLua(L);

    system("pause");
    return 0;
}
/////////////////////////////////////////////////////////////////////////
//sample.lua
--new a class
local cppClass = CPPClass("cppClass")
--call cpp method
cppClass:cppPrint("Hello world!")
print("CPPClass's cppSum:", cppClass:cppSum(1,2))

--call cpp function
cppPrint("Hello world!")
print("cppSum:", cppSum(1,2))

--define lua function
function luaPrint(str)
    print("luaPrint:", str)
end

function luaSum(a, b)
    return a + b
end

--define table
luaTable = {"hello"}

ATTENTIONS

It is the same as passing refrence or object to LuaFunction(Eluna will create a temporary object to pass). But you can pass pointer instead of refrence.

BUILDS

  1. Install lua.
  2. You need open premake4.lua and change "links {"lua51"}" to your lua lib's path.
  3. Use premake4 to generate project files.
$ cd premake4
$ premake4 action

You can read this page:Premake Quick Start.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap