luabridge不是一个陌生的名字,GIT上已经有3-4年多没有更新。将lua和C++相互调用封装的很方便,比如以下示例代码:

//////////////////////////////////////////////////////////////////////////
// test code for luabridge class A
{
public:
A()
{ } ~A()
{ } public:
std::string get_title() const
{
return title;
} void set_title( const std::string& s )
{
title = s;
} private:
std::string title;
}; class B : public A
{
public:
B() : A()
{ } ~B()
{ }
}; void trace( const std::string& strOutput)
{
OutputDebugStringA(strOutput.c_str());
OutputDebugStringA("\n");
} class lua_test
{
public:
lua_test()
: L_()
, B_()
{
L_ = luaL_newstate();
luaL_openlibs(L_);
luaopen_string(L_); luabridge::getGlobalNamespace( L_ )
.addFunction( "trace", trace )
.beginNamespace( "test" )
.beginClass< A >( "A" )
.addConstructor <void (*) (void)> ()
.addProperty( "title", &A::get_title, &A::set_title )
.endClass() .deriveClass< B, A >( "B" )
.addConstructor <void (*) (void)> ()
.endClass()
.endNamespace()
;
} ~lua_test()
{
lua_close( L_ );
} bool run( )
{
luabridge::setglobal<A*>( L_, (A*)&this->B_, "classb" );
B_.set_title( "B.title "); std::string lua_string;
FILE* f = fopen( "D:/test.lua", "r" );
if( f ) {
char buf[] = {};
int r = fread( buf, , sizeof( buf ), f );
if( r > )
lua_string = std::string( buf, r );
fclose( f );
f = ;
} try
{
//2.加载Lua文件
int bRet = luaL_loadstring( L_, lua_string.c_str() );
if(bRet) {
OutputDebugStringA(lua_tostring( L_, - ) );
return false;
} //3.运行Lua文件 CHttpCall::~CHttpCall
bRet = lua_pcall( L_, , , );
if(bRet)
{
OutputDebugStringA(lua_tostring( L_, - ) );
return false;
}
} catch (...) {
OutputDebugStringA( lua_tostring( L_, - ) );
}
return true;
} private:
lua_State* L_;
B B_;
}; // 运行代码

lua_test t;
  t.run();

lua_test 打开D:/test.lua文件并执行test_func方法,该方法创建了一个B的实例并打印实例的title属性以及全局对象classb的title属性

test.lua:

function test_func()
local a = test.B();
a.title = "abcdefg";
trace( a.title )
trace( classb.title );
end test_func();

在此记录一下。

最新文章

  1. php二维数组按照键值排序的方法
  2. 通过j-interop访问WMI实例代码
  3. CSS线性渐变
  4. 读书笔记:&lt;我是一只IT小小鸟&gt;
  5. bzoj3211,bzoj3038
  6. android--HttpURLConnection(转载)
  7. bluetooth记录
  8. Android Spinner使用简介
  9. Basys3在线调试视频指南及代码
  10. 如何理解java是一个面向对象的语言?(转自Hollis的直面java)
  11. Solr 10 - SolrCloud集群模式简介 + 组成结构的说明
  12. kubernetes-整体概述和架构
  13. Java静态数据的初始化
  14. windowns下excel2013快速生成月报表
  15. mysql ibdata1
  16. 受限玻尔兹曼机(Restricted Boltzmann Machine, RBM) 简介
  17. 四则运算 C 语言
  18. ACM札记
  19. 1118 Birds in Forest (25 分)
  20. shell 中的操作符

热门文章

  1. NotePad++ for PHP
  2. 解决Win7下一个VC++6.0您不能直接打开多个project问题
  3. TML5安全:CORS(跨域资源共享)简介
  4. jq toggle1.9版本后不支持解决方案
  5. 【hoj】2651 pie 二分查找
  6. Javascript--dataTransfer
  7. PHP系列目录
  8. microsoft NLayerApp项目中的层次结构图
  9. jQuery百叶窗图片滑块
  10. android Fragment 用法小结