Thursday, February 9, 2017

Luabind: properties demonstration

Consider the following code:

#include <string>
#include <iostream>
#include <luabind/luabind.hpp>
struct A
{
A(int val): a(val), b(5) {}
int a;
int b;
};
extern "C" int init(lua_State* L)
{
using namespace luabind;
open(L);
module(L)
[
class_<A>("A")
.def(constructor<int>())
.def_readwrite("a", &A::a)
.def_readonly("b", &A::b)
];
return 0;
}
view raw properties.cpp hosted with ❤ by GitHub
When run:

kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/properties$ 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/properties$ ./commands.bash 
7
89
5
lua: property 'b' is read only
stack traceback:
[C]: in function '__newindex'
test.lua:7: in main chunk
[C]: in ?
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/properties$ 

No comments:

Post a Comment