This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <iostream> | |
#include <luabind/luabind.hpp> | |
#include <luabind/operator.hpp> | |
class testclass | |
{ | |
public: | |
testclass(int val) : m_int(val) {} | |
void print() | |
{ | |
std::cout << m_int << std::endl; | |
} | |
testclass operator+(int rhs) | |
{ | |
testclass tmp(m_int + rhs); | |
return tmp; | |
} | |
private: | |
int m_int; | |
}; | |
extern "C" int init(lua_State* L) | |
{ | |
using namespace luabind; | |
open(L); | |
module(L) | |
[ | |
class_<testclass>("testclass") | |
.def(constructor<int>()) | |
.def("print", &testclass::print) | |
.def(self + int()) | |
]; | |
return 0; | |
} |
When run:
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/operator$ cat test.lua
package.loadlib('./testclass.so', 'init')()
a = testclass(5)
a:print()
c = a + 6
c:print()
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/operator$ cat commands.bash
#!/bin/bash
g++ testclass.cpp -I/usr/include/lua5.2/ -c -fPIC
g++ -shared -Wl,--whole-archive -o testclass.so testclass.o -lluabind -Wl,--no-whole-archive
lua test.lua
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/operator$ ./commands.bash
5
11
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/operator$
No comments:
Post a Comment