lua-users home
lua-l archive

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


> Can you post some of the macros that you use? It's a pity I didn't think
> earlier about using macros to declare binding functions. Now as I try to
> create a macro that would create the Get/Set functions for the
> members of some C++ class, I find that a single macro cannot both
> declare the functions and register them (or include them in the table
> that is passed to luaL_openlib).

The macros I use are along the lines (with a bit more parameter checking):

#define GET_TYPE(Class, X, type)                        \
static void LuaGet##Class##X(lua_State *state)          \
{                                                       \
  Class *obj = (Class*)lua_getuserdata(lua_lua2C(1));   \
  lua_push##type(state, obj->Get##X());                 \
}

#define GET_NUMBER(Class,X) GET_TYPE(Class, X, number)
#define GET_OBJ_NUMBER(X) GET_NUMBER(Object,X)

and I declare them:

GET_OBJ_NUMBER(weight)
GET_OBJ_NUMBER(x_pos)
GET_OBJ_NUMBER(y_pos)

etc...

This code is written from memory, as I don't have my source here, but
you get the idea.
To register them, I have the standard array name/func_ptr to be passed
with lua_register.

-- 
Vincent Penquerc'h