C++利用LuaIntf调用Lua的方法示例

所属分类: 软件编程 / C 语言 阅读数: 86
收藏 0 赞 0 分享

C++利用LuaIntf调用Lua

本文主要介绍了C++利用LuaIntf调用Lua的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

void LuaTest::OnResponse(uint32_t uLuaRpcId,
 const std::string& sRespContent) const
{
 using LuaIntf::LuaRef;
 LuaRef require(m_pLuaState, "require");
 try {
  LuaRef handler = require.call<LuaRef>("client_rpc_response_handler");
  handler.dispatchStatic("handle", uLuaRpcId, sRespContent);
 }
 catch (const LuaIntf::LuaException& e) {
  std::cerr << "Failed to call lua client_rpc_response_handler.handle(), "
   << e.what() << std::endl;
 }
}

这是测试客户端代码,可以写Lua代码测试服务器.

Lua代码中发出一个Rpc请求时, 会在Lua中保存一个回调, 待收到应答时触发回调. 通过uLuaRpcId来索引该回调.

sRespContent 是收到的应答包, 将在lua中解包.

OnResponse() 就是调用了 Lua 代码:

require("client_rpc_response_handler").handle(uLuaRpcId, sRespContent)

利用lua-intf来调用C++函数

这里主要是在windows利用VS2015完成,首先是配置lua环境,包含lua的头文件,连接器里面链接lua的静态库,然后就是包含lua-intf的代码,具体如下表


需要注意的是:lua-intf_d6f17a是一个包含lua-intf的目录。

lua-intf的代码在github上可以下载:https://github.com/SteveKChiu/lua-intf

如何使用看他的README.md

这里我们主要是测试绑定C++类中的函数,工程目录结构如下

main.cpp代码如下

#include <iostream> 
#include <lua.hpp> 
#include <LuaIntf/LuaIntf.h> 
#include <string> 
 
const char SCRIPTS_DIR[] = "../scripts"; 
 
using namespace std; 
 
struct lua_State; 
 
class TestLog 
{ 
public: 
 static TestLog *getInstance() 
 { 
  static TestLog instance; 
  return &instance; 
 } 
 
 ~TestLog(); 
 void Log(const string &str); 
 void BindLua(lua_State *l); 
 
private: 
 TestLog(); 
  
}; 
 
TestLog::TestLog() 
{ 
} 
 
TestLog::~TestLog() 
{ 
} 
 
void TestLog::Log(const string &str) 
{ 
 cout << str << endl; 
} 
 
namespace 
{ 
 using LuaRef = LuaIntf::LuaRef; 
 
 void LuaLog(const string &str) 
 { 
  TestLog::getInstance()->Log(str); 
 } 
 
 namespace LuaTestLog 
 { 
  void Bind(lua_State* L) 
  { 
   assert(L); 
   LuaIntf::LuaBinding(L).beginModule("c_testlog") 
    .addFunction("log", &LuaLog) 
    .endModule(); 
  } 
 }; 
}; 
 
void TestLog::BindLua(lua_State *l) 
{ 
 LuaTestLog::Bind(l); 
} 
 
int main() 
{ 
 lua_State *l = luaL_newstate(); 
 luaL_openlibs(l); 
 TestLog::getInstance()->BindLua(l); 
 cout << "mmmmmmmmmmm" << endl; 
 luaL_dofile(l, "test.lua"); 
 system("pause"); 
 return 0; 
} 

log.lua代码如下:

local Log = {} 
 
local log = c_testlog.log 
 
function Log:new(log_name) 
 assert("table" == type(self)) 
 assert(not log_name or "string" == type(log_name)) 
 local log = {} 
 log.log_name = log_name or "Log" 
 setmetatable(log, self) 
 self.__index = self 
 return log 
end 
 
function Log:set_log_name(log_name) 
 self.log_name = log_name 
end 
 
function Log:info(pattern, ...) 
 log(string.format(pattern, ...)) 
end 
 
function Log:debug() 
 print("ssssssssssssssssssssssssss") 
end 
 
return Log 

test.lua代码如下:

local function main() 
 
 print("dddddddddddddddddddddddd") 
 local p = "../testLuaIntf" 
 package.path = package.path ..";" .. p .. "/" .. "testLuaIntf" .. "/?.lua" 
 --package.path = package.path .. "E:\VSProject\?.luac" 
 
 print("bbbbbbbbbbbbbbbbbbbbbbbbbbbbb") 
 
 local log = require("log"):new("svn_log") 
 log:info("%d...",1) 
 
 
 --:new("svc_log") 
 print("ssssssssssssssssssssssss") 
 
 
end 
 
 
xpcall(main,function(...) 
local msg = {...}; 
for k ,v in pairs(msg) do 
print("k=" .. tostring( k) .. " v=" .. tostring(v)) 
 
end 
print(tostring() .. " 123") 
end) 

这里流程通过C++调lua的接口luaL_dofile(l, "test.lua");来执行test.lua,test.lua中require("log"),然后lua再调用C++的函数Log完成打印

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

更多精彩内容其他人还在看

C++中四种对象生存期和作用域以及static的用法总结分析

以下是对C++中四种对象生存期和作用域以及static的用法进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++嵌套类与局部类详细解析

从作用域的角度看,嵌套类被隐藏在外围类之中,该类名只能在外围类中使用。如果在外围类之外的作用域使用该类名时,需要加名字限定
收藏 0 赞 0 分享

C++空类详解

以下是对C++中的空类进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++之友元:友元函数和友元类详解

友元是一种允许非类成员函数访问类的非公有成员的一种机制。可以把一个函数指定为类的友元,也可以把整个类指定为另一个类的友元
收藏 0 赞 0 分享

C++中返回指向函数的指针示例

int (*ff(int)) (int *,int);表示:ff(int)是一个函数,带有一个int型的形参,该函数返回int (*) (int *,int),它是一个指向函数的指针,所指向的函数返回int型并带有两个分别是Int*和int型的形参
收藏 0 赞 0 分享

C数据结构之单链表详细示例分析

以下是对C语言中的单链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C数据结构之双链表详细示例分析

以下是对c语言中的双链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅析如何在c语言中调用Linux脚本

如何在c语言中调用Linux脚本呢?下面小编就为大家详细的介绍一下吧!需要的朋友可以过来参考下
收藏 0 赞 0 分享

深入解析unsigned int 和 int

以下是对unsigned int和int进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅谈C++中的string 类型占几个字节

本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节
收藏 0 赞 0 分享
查看更多