Clean up Lua std::string typemaps

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@13121 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2012-05-26 07:07:00 +00:00
commit e62dc28f16

View file

@ -5,8 +5,9 @@
* ----------------------------------------------------------------------------- */
%{
#include <string>
#include <string>
%}
/*
Only std::string and const std::string& are typemaped
they are converted to the Lua strings automatically
@ -25,13 +26,14 @@ can be used as
s="hello world"
s2=test_value(s)
assert(s==s2)
*/
%naturalvar std::string;
namespace std {
%naturalvar string;
/*
Bug report #1526022 by neomantra
Bug report #1526022:
Lua strings and std::string can contain embeded zero's
Therefore a standard out typemap should not be:
lua_pushstring(L,$1.c_str());
@ -46,26 +48,27 @@ becomes
Not using: lua_tolstring() as this is only found in Lua 5.1 & not 5.0.2
*/
%typemap(in,checkfn="lua_isstring") std::string
%typemap(in,checkfn="lua_isstring") string
%{$1.assign(lua_tostring(L,$input),lua_rawlen(L,$input));%}
%typemap(out) std::string
%typemap(out) string
%{ lua_pushlstring(L,$1.data(),$1.size()); SWIG_arg++;%}
%typemap(in,checkfn="lua_isstring") const std::string& ($*1_ltype temp)
%typemap(in,checkfn="lua_isstring") const string& ($*1_ltype temp)
%{temp.assign(lua_tostring(L,$input),lua_rawlen(L,$input)); $1=&temp;%}
%typemap(out) const std::string&
%typemap(out) const string&
%{ lua_pushlstring(L,$1->data(),$1->size()); SWIG_arg++;%}
// for throwing of any kind of string, string ref's and string pointers
// we convert all to lua strings
%typemap(throws) std::string,std::string&,const std::string&
%typemap(throws) string, string&, const string&
%{ lua_pushlstring(L,$1.data(),$1.size()); SWIG_fail;%}
%typemap(throws) std::string*,const std::string*
%typemap(throws) string*, const string*
%{ lua_pushlstring(L,$1->data(),$1->size()); SWIG_fail;%}
// and the typechecks
%typecheck(SWIG_TYPECHECK_STRING) std::string,const std::string& {
%typecheck(SWIG_TYPECHECK_STRING) string, const string& {
$1 = lua_isstring(L,$input);
}
@ -77,46 +80,19 @@ void fn(std::string& str);
Is this an in/out/inout value?
Therefore you need the usual
%apply (std::string& INOUT) {(std::string& str)};
%apply (std::string& INOUT) {std::string& str};
or
%apply std::string& INOUT {std::string& str};
typemaps to tell SWIG what to do.
*/
%typemap(in) std::string &INPUT=const std::string &;
%typemap(in, numinputs=0) std::string &OUTPUT ($*1_ltype temp)
%typemap(in) string &INPUT=const string &;
%typemap(in, numinputs=0) string &OUTPUT ($*1_ltype temp)
%{ $1 = &temp; %}
%typemap(argout) std::string &OUTPUT
%typemap(argout) 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;
/*
For const std::string* and std::string* is not clear
is this a pointer or an array?
Therefore just leaving it as is
(there is some rough code below which could be used if needed
// SWIG wraps const ref's as pointer
// typemaps to deal with this and const ptrs
%typemap(in,checkfn="lua_isstring")
const std::string& INPUT(std::string temp),
const std::string* INPUT(std::string temp)
%{temp=(char*)lua_tostring(L, $input); $1=&temp;%}
%typemap(out) const std::string&, const std::string*
%{ lua_pushstring(L,$1->c_str()); SWIG_arg++;%}
// the non-const pointer version
%typemap(in) std::string *INPUT=const std::string *INPUT;
%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++;%}
%typemap(in) std::string *INOUT = std::string *INPUT;
%typemap(argout) std::string *INOUT = std::string *OUTPUT;
*/
%typemap(in) string &INOUT =const string &;
%typemap(argout) string &INOUT = string &OUTPUT;
/*
A really cut down version of the string class
@ -128,25 +104,24 @@ and little else
note: no fn's taking the const string&
as this is overloaded by the const char* version
*/
namespace std {
class string {
public:
string();
string(const char*);
//string(const string&);
unsigned int size() const;
unsigned int length() const;
bool empty() const;
// no support for operator[]
const char* c_str()const;
const char* data()const;
// assign does not return a copy of this object
// (no point in a scripting language)
void assign(const char*);
//void assign(const string&);
// no support for all the other features
// its probably better to do it in lua
};
class string {
public:
string();
string(const char*);
//string(const string&);
unsigned int size() const;
unsigned int length() const;
bool empty() const;
// no support for operator[]
const char* c_str()const;
const char* data()const;
// assign does not return a copy of this object
// (no point in a scripting language)
void assign(const char*);
//void assign(const string&);
// no support for all the other features
// its probably better to do it in lua
};
}