lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Well, I'm still in search of a way to do automatic Lua bindings that
suit my taste.  I frequently bind C++ classes and have a templated
udata worked out that takes care of everything I need.  A typical
binding function will look like:

static int remove(lua_State *L)
{
	Self *self = Self::to_udata(L, 1);
	Vertex_udata *vertex = Vertex_udata::to_udata(L, 2);
	if(self && vertex) {
		self->Base::remove(vertex);
	}
	else {
		//error
	}
	
	return 0;
}

where Self is the udata class passed in by the Lua syntax
object:method() and Base is the class I'm binding.  Any additional
arguments will be explicitly named as Vertex_udata is in this case.
For these kinds of functions, I just need type information to generate
the code.  More complicated ones with overloaded functions look like
this:

static int triangle(lua_State *L)
	{
		T_udata *self = T_udata::to_udata(L, 1);

		switch(lua_gettop(L))
		{
			case 3:
				{
					Cell_udata *cell = Cell_udata::to_udata(L, 2);
					if(self && cell && lua_isnumber(L, 3)) {
						int i = lua_tointeger(L, 3);
						Triangle_udata::udata_push(L, new Triangle_udata(
accessor(*self).DT::triangle(*cell, i) ));
					}
					else {
						//error
						lua_pushnil(L);
					}
				}
				break;
				
			case 2:
				{
					Facet_udata *facet = Facet_udata::to_udata(L, 2);
					if(self && facet) {
						Triangle_udata::udata_push(L, new Triangle_udata(
accessor(*self).DT::triangle(*facet) ));
					}
					else {
						//error
						lua_pushnil(L);
					}
				}
				break;
				
			default:
				//error
				lua_pushnil(L);
				break;
		};
		
		return 1;
	}

I'd like to automate as much as possible from automatic type
information meaning resolving nested typedefs like this:

typedef Delaunay_triangulation_3<Gt, Tds>		DT;
typedef typename DT::All_vertices_iterator		All_vertices_iterator;

that make come from several classes away.  I'd also like to automate
the generation of the luaL_Reg tables.  I'd also like to be able to
detect inheritance.  My userdata template takes an array from strings
refering to a name of parent classes and automatically sets up
metatables in the appropriate manner.  If I had this information as
well, I'd be on my way.

best,
wes