diff options
author | Mark Gossage <mark@gossage.cjb.net> | 2006-07-21 08:51:40 +0000 |
---|---|---|
committer | Mark Gossage <mark@gossage.cjb.net> | 2006-07-21 08:51:40 +0000 |
commit | b2363bff02ee72f3fd79e9cb075a789e88dae1dc (patch) | |
tree | b689f51341e0e165d07728900151469e0b42a65e | |
parent | f2ff50e60eaac3fca8f5fe382c7bd57d5b945201 (diff) | |
download | swig-b2363bff02ee72f3fd79e9cb075a789e88dae1dc.tar.gz |
upfdated to the typemaps to support std:strings with '\0' in them, and to add a typemap for SWIGTYPE** OUTPUT
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@9221 626c5289-ae23-0410-ae9c-e8d60b6d4f22
-rw-r--r-- | CHANGES.current | 4 | ||||
-rw-r--r-- | Lib/lua/std_string.i | 34 | ||||
-rw-r--r-- | Lib/lua/typemaps.i | 34 |
3 files changed, 64 insertions, 8 deletions
diff --git a/CHANGES.current b/CHANGES.current index 4df931d4d..f48e11ed7 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -1,5 +1,9 @@ Version 1.3.30 (in progress) ============================ +07/21/2006: mgossage + Bugfix #1526022 pdated std::string to support strings with '\0' inside them + updated typemaps.i to add support for pointer to pointers + 07/19/2006: mutandiz [allegrocl] - Add std_string.i support. diff --git a/Lib/lua/std_string.i b/Lib/lua/std_string.i index 900024cda..06a731ecb 100644 --- a/Lib/lua/std_string.i +++ b/Lib/lua/std_string.i @@ -14,6 +14,9 @@ Only std::string and const std::string& are typemaped they are converted to the Lua strings automatically +std::string& and std::string* are not +they must be explicitly managed (see below) + eg. std::string test_value(std::string x) { @@ -30,20 +33,35 @@ assert(s==s2) %naturalvar std::string; -%typemap(in,checkfn="lua_isstring") std::string -%{$1 = (char*)lua_tostring(L, $input);%} +/* +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_pushstring(L,$1.c_str()); SWIG_arg++;%} +%{ lua_pushlstring(L,$1.data(),$1.size()); SWIG_arg++;%} %typemap(in,checkfn="lua_isstring") const std::string& (std::string temp) -%{temp=(char*)lua_tostring(L, $input); $1=&temp;%} +%{temp.assign(lua_tostring(L,$input),lua_strlen(L,$input)); $1=&temp;%} + %typemap(out) const std::string& -%{ lua_pushstring(L,$1->c_str()); SWIG_arg++;%} +%{ lua_pushlstring(L,$1->data(),$1->size()); SWIG_arg++;%} %typemap(throws) std::string,const std::string& -%{lua_pushstring(L,$1.c_str()); -SWIG_fail; %} +%{ lua_pushlstring(L,$1.data(),$1.size()); SWIG_fail;%} // and the typechecks %typecheck(SWIG_TYPECHECK_STRING) std::string,const std::string& { @@ -68,7 +86,7 @@ typemaps to tell SWIG what to do. %typemap(in, numinputs=0) std::string &OUTPUT (std::string temp) %{ $1 = &temp; %} %typemap(argout) std::string &OUTPUT -%{ lua_pushstring(L,$1.c_str()); SWIG_arg++;%} +%{ 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; diff --git a/Lib/lua/typemaps.i b/Lib/lua/typemaps.i index 99abdff81..42047b136 100644 --- a/Lib/lua/typemaps.i +++ b/Lib/lua/typemaps.i @@ -491,3 +491,37 @@ void SWIG_write_ptr_array(lua_State* L,void **array,int size,swig_type_info *typ %{ SWIG_write_ptr_array(L,(void**)$1,$2,$*1_descriptor,0); SWIG_arg++; %} %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++; %} + |