lua-users home
lua-l archive

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


> Ok, I have successfully merged my application, Lua, and SWIG!  I am  
> able to access my variables in Lua and read the modified values.   
> Thanks to all who helped!
> 
> I noticed one bug, however.  The wrapped code is not checking the  
> number of parameters passed into the routines.  I can send in any  
> number of parameters, even if the parameter takes only one.
> 
> Also, is there a way to make the error messages more specific to the  
> function that is failing?  Right now, it says something like  
> "argument 2 is not a number", but I'd like it to say "argument 2
> to call GetXi is not a number".
> 
> I have one last question, which might not be Lua specific:
> 
> I have a function :
> 
> void ParseTime (double, int &year, int &day, int &hour, int
> &minute, int &second, long &nano);
> 
> for which I would like to return multiple values, or return the  
> results in a Lua table.  If I try to wrap it as is, it doesn't really  
> work when I call it from Lua code.  What is the best way to make this  
> a routine like "time = ParseTime (doubleT)" and have it return either
> multiple values or in a table?
> 
> Thanks,
> Joey
> 
Hello Joey,
I wrote the lua-swig bindings, so I guess I should answer this.

1: Not checking for extra values passed in. Yes it doesn't bother to check. we could add this to the code.
2: Not warning on what function was called. Good suggestion, will add it.
3: void ParseTime (double, int &year, int &day, int &hour, int &minute, int &second, long &nano);
This one is easy to do in SWIG. All you have to do is select the appropriate typemap. You can find details in the SWIG manual on typemaps. Here is the summary.

%include <typemaps.i> // this is all the typemaps for this
%apply int& OUTPUT{ int &year};	// if SWIG sees int& year, assume its an OUTPUT parameter
%apply int& OUTPUT{ int &day};	// ditto int &year
%apply int& OUTPUT{ int &hour};
%apply int& OUTPUT{ int &minute};
%apply int& OUTPUT{ int &second};
%apply long& OUTPUT{ long &nano};

// then it parses this file it treat the parameters accordingly.
%include "myheaderfile.h"

This will allow you to do:
y,d,h,m,s,n=ParseTime(some_double)
or
t={ParseTime(some_double)}

There is also a INPUT, and INOUT parameter, which can be used accordingly.

Hope this helps.
Regards,
Mark Gossage