[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Tricky binding question
- From: "Wesley Smith" <wesley.hoke@...>
- Date: Sun, 7 Oct 2007 03:41:03 -0700
Hi Lua list,
I'm writing some Lua bindings for a C++ library and I'd like the
binding to follow as much as possible the way the library is coded
from C++. I'm adding higer-level bindings as well, but the base,
low-level bindings I want to closely parallel what a C++ program would
look like. Here's what I'm binding:
class Voronoi
{
public:
const Delaunay & dual()
{
return dt;
}
//everything else left out for simplicity
protected:
Delaunay dt;
};
The way this is typically used goes as follows
Voronoi v;
v.dual().is_infinite(point);
I'd like to use it like this in Lua
v = Voronoi()
v:dual(): is_infinite(point)
There are a few issues to address here. First, I'm returning a const
reference in C++ and I'd like to enforce that constness in the Lua
interface. Second, I don't want to make a new userdata from the
dual() function because I have the Delaunay class already fully bound
and this reference to a Delaunay class isn't quite the same because of
its constness. The ideal situation would be for the call to insert to
actually call a function in the Voronoi class. In general, I bind C++
classes as userdata that are also C++ classes as so:
Voronoi_udata : public Voronoi;
What I envision is have a function in the Voronoi_udata class:
static int delaunay_ is_infinite(lua_State *L)
{
//get voronoi instance from lua state
Voronoi_udata *v = to_udata(L, 1);
v->dual().is_infinite(...);
}
So my question is how do I bind the "dual" field in the Voronoi
userdata metatable to get this behavior? One thing I've thought of
are to have dual() return a table with all the valid functions of the
Delaunay class. Can anyone think of another potentially better
solution?
thanks,
wes