1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
/* -----------------------------------------------------------------------------
* See the LICENSE file for information on copyright, usage and redistribution
* of SWIG, and the README file for authors - http://www.swig.org/release.html.
*
* std_string.i
*
* std::string typemaps for LUA
* ----------------------------------------------------------------------------- */
%{
#include <string>
%}
/*
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) {
return x;
}
can be used as
s="hello world"
s2=test_value(s)
assert(s==s2)
*/
%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++;%}
// 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&
%{ lua_pushlstring(L,$1.data(),$1.size()); SWIG_fail;%}
%typemap(throws) std::string*,const std::string*
%{ lua_pushlstring(L,$1->data(),$1->size()); SWIG_fail;%}
// and the typechecks
%typecheck(SWIG_TYPECHECK_STRING) std::string,const std::string& {
$1 = lua_isstring(L,$input);
}
/*
std::string& can be wrappered, but you must inform SWIG if it is in or out
eg:
void fn(std::string& str);
Is this an in/out/inout value?
Therefore you need the usual
%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 (std::string temp)
%{ $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;
/*
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;
*/
/*
A really cut down version of the string class
This provides basic mapping of lua strings <-> std::string
and little else
(the std::string has a lot of unneeded functions anyway)
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
};
}
|