/trunk/Lib/lua/std_string.i
Swig | 152 lines | 49 code | 14 blank | 89 comment | 0 complexity | ec0544ec09e7528257a25be6c67b22a4 MD5 | raw file
1/* ----------------------------------------------------------------------------- 2 * std_string.i 3 * 4 * std::string typemaps for LUA 5 * ----------------------------------------------------------------------------- */ 6 7%{ 8 #include <string> 9%} 10/* 11Only std::string and const std::string& are typemaped 12they are converted to the Lua strings automatically 13 14std::string& and std::string* are not 15they must be explicitly managed (see below) 16 17eg. 18 19std::string test_value(std::string x) { 20 return x; 21} 22 23can be used as 24 25s="hello world" 26s2=test_value(s) 27assert(s==s2) 28 29*/ 30 31%naturalvar std::string; 32 33/* 34Bug report #1526022 by neomantra 35Lua strings and std::string can contain embeded zero's 36Therefore a standard out typemap should not be: 37 lua_pushstring(L,$1.c_str()); 38but 39 lua_pushlstring(L,$1.data(),$1.size()); 40 41Similarly for getting the string 42 $1 = (char*)lua_tostring(L, $input); 43becomes 44 $1.assign(lua_tostring(L,$input),lua_strlen(L,$input)); 45 46Not using: lua_tolstring() as this is only found in Lua 5.1 & not 5.0.2 47*/ 48 49%typemap(in,checkfn="lua_isstring") std::string 50%{$1.assign(lua_tostring(L,$input),lua_strlen(L,$input));%} 51%typemap(out) std::string 52%{ lua_pushlstring(L,$1.data(),$1.size()); SWIG_arg++;%} 53 54%typemap(in,checkfn="lua_isstring") const std::string& (std::string temp) 55%{temp.assign(lua_tostring(L,$input),lua_strlen(L,$input)); $1=&temp;%} 56 57%typemap(out) const std::string& 58%{ lua_pushlstring(L,$1->data(),$1->size()); SWIG_arg++;%} 59 60// for throwing of any kind of string, string ref's and string pointers 61// we convert all to lua strings 62%typemap(throws) std::string,std::string&,const std::string& 63%{ lua_pushlstring(L,$1.data(),$1.size()); SWIG_fail;%} 64%typemap(throws) std::string*,const std::string* 65%{ lua_pushlstring(L,$1->data(),$1->size()); SWIG_fail;%} 66 67// and the typechecks 68%typecheck(SWIG_TYPECHECK_STRING) std::string,const std::string& { 69 $1 = lua_isstring(L,$input); 70} 71 72/* 73std::string& can be wrappered, but you must inform SWIG if it is in or out 74 75eg: 76void fn(std::string& str); 77Is this an in/out/inout value? 78 79Therefore you need the usual 80%apply (std::string& INOUT) {(std::string& str)}; 81or 82%apply std::string& INOUT {std::string& str}; 83typemaps to tell SWIG what to do. 84*/ 85 86%typemap(in) std::string &INPUT=const std::string &; 87%typemap(in, numinputs=0) std::string &OUTPUT (std::string temp) 88%{ $1 = &temp; %} 89%typemap(argout) std::string &OUTPUT 90%{ lua_pushlstring(L,$1->data(),$1->size()); SWIG_arg++;%} 91%typemap(in) std::string &INOUT =const std::string &; 92%typemap(argout) std::string &INOUT = std::string &OUTPUT; 93 94/* 95For const std::string* and std::string* is not clear 96is this a pointer or an array? 97 98Therefore just leaving it as is 99(there is some rough code below which could be used if needed 100 101// SWIG wraps const ref's as pointer 102// typemaps to deal with this and const ptrs 103%typemap(in,checkfn="lua_isstring") 104 const std::string& INPUT(std::string temp), 105 const std::string* INPUT(std::string temp) 106%{temp=(char*)lua_tostring(L, $input); $1=&temp;%} 107%typemap(out) const std::string&, const std::string* 108%{ lua_pushstring(L,$1->c_str()); SWIG_arg++;%} 109 110// the non-const pointer version 111%typemap(in) std::string *INPUT=const std::string *INPUT; 112%typemap(in, numinputs=0) std::string *OUTPUT (std::string temp) 113%{ $1 = &temp; %} 114%typemap(argout) std::string *OUTPUT 115%{ lua_pushstring(L,$1->c_str()); SWIG_arg++;%} 116%typemap(in) std::string *INOUT = std::string *INPUT; 117%typemap(argout) std::string *INOUT = std::string *OUTPUT; 118 119*/ 120 121/* 122A really cut down version of the string class 123 124This provides basic mapping of lua strings <-> std::string 125and little else 126(the std::string has a lot of unneeded functions anyway) 127 128note: no fn's taking the const string& 129as this is overloaded by the const char* version 130*/ 131namespace std { 132 133 class string { 134 public: 135 string(); 136 string(const char*); 137 //string(const string&); 138 unsigned int size() const; 139 unsigned int length() const; 140 bool empty() const; 141 // no support for operator[] 142 const char* c_str()const; 143 const char* data()const; 144 // assign does not return a copy of this object 145 // (no point in a scripting language) 146 void assign(const char*); 147 //void assign(const string&); 148 // no support for all the other features 149 // its probably better to do it in lua 150 }; 151} 152