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@9221 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Mark Gossage 2006-07-21 08:51:40 +00:00
commit 1d24d79e1d
3 changed files with 64 additions and 8 deletions

View file

@ -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.

View file

@ -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;

View file

@ -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++; %}