亲宝软件园·资讯

展开

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

金庆 人气:0
想了解C++利用LuaIntf调用Lua的方法示例的相关内容吗,金庆在本文为您仔细讲解C++利用LuaIntf调用Lua的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:c,调用lua,c,调用lua函数,lua,函数调用,下面大家一起来学习吧。

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完成打印

总结

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

加载全部内容

相关教程
猜你喜欢
用户评论