【原】luabind 导出C++类
首先建立一个export_class的DLL工程,然后建立一个TestClass测试类
TestClass.h
TestClass.cpp
export_class.cpp
脚本如下:
脚本运行结果:
现在还有一点问题,就是用Singleton的静态成员函数调用后再Print有问题,目前我也没有搞明白是怎么回事。太奇怪了。。。。
TestClass.h
#pragma once
#include <string>
#include <iostream>
using namespace std;
class TestClass
{
public:
TestClass(string s);
static TestClass& Singleton();
void Print();
private:
static TestClass m_Singleton;
string m_String;
};
#include <string>
#include <iostream>
using namespace std;
class TestClass
{
public:
TestClass(string s);
static TestClass& Singleton();
void Print();
private:
static TestClass m_Singleton;
string m_String;
};
TestClass.cpp
#include "TestClass.h"
TestClass TestClass::m_Singleton = TestClass("default string");
TestClass::TestClass(string s)
{
cout <<"TestClass::TestClass()"<<endl;
m_String = s;
}
TestClass& TestClass::Singleton()
{
cout <<"TestClass::Singleton()"<<endl;
return m_Singleton;
}
void TestClass::Print()
{
cout << m_String << endl;
}
TestClass TestClass::m_Singleton = TestClass("default string");
TestClass::TestClass(string s)
{
cout <<"TestClass::TestClass()"<<endl;
m_String = s;
}
TestClass& TestClass::Singleton()
{
cout <<"TestClass::Singleton()"<<endl;
return m_Singleton;
}
void TestClass::Print()
{
cout << m_String << endl;
}
export_class.cpp
// export_class.cpp : Defines the entry point for the DLL application.
#include <luabind/luabind.hpp>
extern "C"
{
#include <lua.h>
#include <lualib.h>
}
#include "TestClass.h"
using namespace luabind;
#ifdef WIN32
#define LUA_EXPORT __declspec(dllexport)
#endif
void print()
{
cout << "hello, Eric Xiang!" <<endl;
}
extern "C" int LUA_EXPORT luaopen_export_class(lua_State* L)
{
open(L);
module(L)
[
class_<TestClass>("TestClass")
.def(constructor<string>())
.def("Print", &TestClass::Print),
def("Singleton",&TestClass::Singleton),
def("g_print", &print)
];
return 0;
}
#include <luabind/luabind.hpp>
extern "C"
{
#include <lua.h>
#include <lualib.h>
}
#include "TestClass.h"
using namespace luabind;
#ifdef WIN32
#define LUA_EXPORT __declspec(dllexport)
#endif
void print()
{
cout << "hello, Eric Xiang!" <<endl;
}
extern "C" int LUA_EXPORT luaopen_export_class(lua_State* L)
{
open(L);
module(L)
[
class_<TestClass>("TestClass")
.def(constructor<string>())
.def("Print", &TestClass::Print),
def("Singleton",&TestClass::Singleton),
def("g_print", &print)
];
return 0;
}
脚本如下:
require "export_class"
g_print()
--testClass = Singleton()
testClass = TestClass("haha~~")
if testClass == nil then
print "testClass is a null value!"
else
testClass:Print()
end
g_print()
--testClass = Singleton()
testClass = TestClass("haha~~")
if testClass == nil then
print "testClass is a null value!"
else
testClass:Print()
end
脚本运行结果:
>lua -e "io.stdout:setvbuf 'no'" "main.lua" TestClass::TestClass() hello, Eric Xiang! TestClass::TestClass() haha~~ >Exit co |
现在还有一点问题,就是用Singleton的静态成员函数调用后再Print有问题,目前我也没有搞明白是怎么回事。太奇怪了。。。。
posted on 2009-07-29 17:54 Eric Xiang 阅读(1135) 评论(0) 编辑 收藏 举报