lua-users home
lua-l archive

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

"Gedalia Pasternak" <> writes:
> Question 1: I have a class Apple and member function
> Apple:get_position(float *x,float *y,float *z);
> is there a easy way to do this?
>  Swig spits out a bunch of:
>      if(!SWIG_isptrtype(L,1)) SWIG_fail_arg("get_position",4,"float *");
> which go off when I can't provide it a float *. I noticed a couple of posts
> about luatypemaps.swg but I can't tell if it handles float *'s. Swig also
> declares SWIGTYPE_p_float but it doesn't seem to be an instatiatable object.

I think you need to mark those as "out" parameters; it's very simple to
do this -- something like adding OUT before the parameter in the method
decl -- but I forget the details, so you should check the swig docs.

> Question 2. I have an AppleManager class, which can look up an apple. When
> getting the apple, Swig will create a apple userdata and assocate the member
> functions of the apple into apple's metatable (I think in the ".fn" field
> via the registery).
>    I'm wondering if there is anyway to cleanly add some lua functions to the
> apple's table so that every time the registry looks it up, I can get both
> the c++ member functions and the new lua functions.

I do this by adding a "wrapper __index" -- i.e. I overwrite the original
swig __index entry in swig's userdata metatable to be my own __index
function, which will first try a lookup in my table of "new methods",
and then if nothing is found, call the saved original __index function
of the metatable.

You only need to do this once for each unique metatable (which are
shared by multiple objects), but the only way I know to do it is, by
wrapping the object constructor and doing something like "if the new
object's metatable __index isn't wrapped, wrap it."


Mayonnaise, n. One of the sauces that serve the French in place of a state