Error

C++博客 首页 新随笔 联系 聚合 管理
  217 Posts :: 61 Stories :: 32 Comments :: 0 Trackbacks
#include <iostream>
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
#include <luabind/luabind.hpp>
using namespace luabind;
class TestClass
{
public:
TestClass(int a,int b);
static TestClass* Singleton();
int add();
virtual void ShowTest(std::string& strMsg);
private:
static TestClass* mSingleton;
int __a,__b;
};
void TestClass::ShowTest(std::string& strMsg)
{
std::cout << __FUNCTION__ << std::endl;
std::cout << strMsg.c_str() << std::endl;
strMsg += "000";
}
TestClass* TestClass::mSingleton = NULL;
TestClass::TestClass(int a,int b)
{
__a = a;
__b = b;
mSingleton = this;
}
TestClass* TestClass::Singleton()
{
if(TestClass::mSingleton == NULL)
{
return new TestClass(0,0);
}
else
{
return mSingleton;
}
}
int TestClass::add()
{
std::cout << __FUNCTION__ << std::endl;
return __a+__b;
}
int bindClass(lua_State* L)
{
open(L);
module(L)
[
class_<TestClass>("TestClass")
//.def(constructor<int,int>())
.def("add", &TestClass::add)
.def("ShowTest", &TestClass::ShowTest),
def("Singleton", &TestClass::Singleton)
];
return 0;
}
int LuaErrorCallBack(lua_State *L)
{
std::cout << __FUNCTION__ << std::endl;
return 0;
}
int main(int argc, char* argv[])
{
luabind::set_pcall_callback(&LuaErrorCallBack);
TestClass testClass(10,5);
lua_State* L = lua_open();  
luaL_openlibs(L);
std::cout << "init lua system" << std::endl;
bindClass(L);
std::cout << "do lua file" << std::endl;
try
{
std::string strFileName = "add.lua.ini";
int iRet = luaL_dofile(L, "add.lua.ini");
if (iRet != 0)
{
std::cout<<"loadfile error[file: " << strFileName.c_str() << "]: "<<lua_tostring(L, -1)<<std::endl;
}
//if (luaL_loadfile(L, strFileName.c_str()) != 0) {
// std::cout<<"loadfile error[file: " << strFileName.c_str() << "]"<<lua_tostring(L, -1)<<std::endl;
//}
//if (lua_pcall(L, 0, LUA_MULTRET, 0) != 0) {
// std::cout<<"pcall error[file: " << strFileName.c_str() << "]" <<lua_tostring(L, -1)<<std::endl;
//}
std::cout << "end" << std::endl;
//getchar();
}
catch(std::exception& ex)
{
std::cout << ex.what() << std::endl;
}
catch(luabind::error er)
{
std::cout << er.what() << std::endl;
}
lua_close(L);
return 0;
}


print("This is valid")
print(1234)
bad_function()
a = "meow"
b = 7
c = a + b
testClass = Singleton()
a = testClass:add()
print(a)
local valMsg = '中文'
testClass:ShowTest(valMsg)
print(valMsg)
posted on 2014-01-06 16:49 Enic 阅读(243) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理