lua-users home
lua-l archive

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


I strongly suggest you take a look at lqt. It does exactly what you
described, with some more candies around (inheritance, enums as
strings, and others I don't describe now).

Overloading is based on a dispatcher that checks which function fits
best the parameters on the stack, using default parameters if
avaliable.

Most of the common code is in a couple of headers which handle generic
udata based on their type name. Metatables are automatically filled.
Typedefs are transparent to everything. Any type can be made
transparent.

Seeing it in action is best way to understand how it works. Typical
generated files are in the editor (http://repo.or.cz/w/Leditor.git). I
don't use luaL_Reg, but push and settable each function separately. I
will change this, it is easy. (you could try to do it yourself :)

mauro

On 15/10/2007, Wesley Smith <wesley.hoke@gmail.com> wrote:
> 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
>