From: Mark G. <mgo...@us...> - 2006-07-21 09:42:46
|
Update of /cvsroot/swig/SWIG/Lib/lua In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv32100/SWIG/Lib/lua Modified Files: std_string.i typemaps.i Log Message: upfdated to the typemaps to support std:strings with '\0' in them, and to add a typemap for SWIGTYPE** OUTPUT Index: typemaps.i =================================================================== RCS file: /cvsroot/swig/SWIG/Lib/lua/typemaps.i,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** typemaps.i 7 Mar 2006 22:02:23 -0000 1.5 --- typemaps.i 21 Jul 2006 08:51:40 -0000 1.6 *************** *** 492,493 **** --- 492,527 ---- %typemap(freearg) (SWIGTYPE**INOUT,int)=(SWIGTYPE**INPUT,int); + /* ----------------------------------------------------------------------------- + * Pointer-Pointer typemaps + * ----------------------------------------------------------------------------- */ + /* + This code is to deal with the issue for pointer-pointer's + In particular for factory methods. + + for example take the following code segment: + + struct iMath; // some structure + int Create_Math(iMath** pptr); // its factory (assume it mallocs) + + to use it you might have the following C code: + + iMath* ptr; + int ok; + ok=Create_Math(&ptr); + // do things with ptr + //... + free(ptr); + + With the following SWIG code + %apply SWIGTYPE** OUTPUT{iMath **pptr }; + + You can get natural wrappering in Lua as follows: + ok,ptr=Create_Math() -- ptr is a iMath* which is returned with the int + ptr=nil -- the iMath* will be GC'ed as normal + */ + + %typemap(in,numinputs=0) SWIGTYPE** OUTPUT ($*ltype temp) + %{ $1 = &temp; %} + %typemap(argout) SWIGTYPE** OUTPUT + %{SWIG_NewPointerObj(L,*$1,$*descriptor,1); SWIG_arg++; %} + Index: std_string.i =================================================================== RCS file: /cvsroot/swig/SWIG/Lib/lua/std_string.i,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** std_string.i 7 Mar 2006 00:14:07 -0000 1.4 --- std_string.i 21 Jul 2006 08:51:40 -0000 1.5 *************** *** 15,18 **** --- 15,21 ---- they are converted to the Lua strings automatically + std::string& and std::string* are not + they must be explicitly managed (see below) + eg. *************** *** 31,48 **** %naturalvar std::string; ! %typemap(in,checkfn="lua_isstring") std::string ! %{$1 = (char*)lua_tostring(L, $input);%} %typemap(out) std::string ! %{ lua_pushstring(L,$1.c_str()); SWIG_arg++;%} %typemap(in,checkfn="lua_isstring") const std::string& (std::string temp) ! %{temp=(char*)lua_tostring(L, $input); $1=&temp;%} %typemap(out) const std::string& ! %{ lua_pushstring(L,$1->c_str()); SWIG_arg++;%} %typemap(throws) std::string,const std::string& ! %{lua_pushstring(L,$1.c_str()); ! SWIG_fail; %} // and the typechecks --- 34,66 ---- %naturalvar std::string; ! /* ! Bug report #1526022 by neomantra ! Lua strings and std::string can contain embeded zero's ! Therefore a standard out typemap should not be: ! lua_pushstring(L,$1.c_str()); ! but ! lua_pushlstring(L,$1.data(),$1.size()); ! ! Similarly for getting the string ! $1 = (char*)lua_tostring(L, $input); ! becomes ! $1.assign(lua_tostring(L,$input),lua_strlen(L,$input)); ! ! Not using: lua_tolstring() as this is only found in Lua 5.1 & not 5.0.2 ! */ + %typemap(in,checkfn="lua_isstring") std::string + %{$1.assign(lua_tostring(L,$input),lua_strlen(L,$input));%} %typemap(out) std::string ! %{ lua_pushlstring(L,$1.data(),$1.size()); SWIG_arg++;%} %typemap(in,checkfn="lua_isstring") const std::string& (std::string temp) ! %{temp.assign(lua_tostring(L,$input),lua_strlen(L,$input)); $1=&temp;%} ! %typemap(out) const std::string& ! %{ lua_pushlstring(L,$1->data(),$1->size()); SWIG_arg++;%} %typemap(throws) std::string,const std::string& ! %{ lua_pushlstring(L,$1.data(),$1.size()); SWIG_fail;%} // and the typechecks *************** *** 69,73 **** %{ $1 = &temp; %} %typemap(argout) std::string &OUTPUT ! %{ lua_pushstring(L,$1.c_str()); SWIG_arg++;%} %typemap(in) std::string &INOUT =const std::string &; %typemap(argout) std::string &INOUT = std::string &OUTPUT; --- 87,91 ---- %{ $1 = &temp; %} %typemap(argout) std::string &OUTPUT ! %{ lua_pushlstring(L,$1->data(),$1->size()); SWIG_arg++;%} %typemap(in) std::string &INOUT =const std::string &; %typemap(argout) std::string &INOUT = std::string &OUTPUT; |