Mercurial > mm7
comparison lib/swig/swigwin-2.0.11/Lib/lua/std_string.i @ 1899:b3009adc0e2f
Adding swig, gitignore, hgignore
author | Nomad |
---|---|
date | Mon, 21 Oct 2013 10:42:27 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1867:eb580660bbbb | 1899:b3009adc0e2f |
---|---|
1 /* ----------------------------------------------------------------------------- | |
2 * std_string.i | |
3 * | |
4 * std::string typemaps for LUA | |
5 * ----------------------------------------------------------------------------- */ | |
6 | |
7 %{ | |
8 #include <string> | |
9 %} | |
10 | |
11 /* | |
12 Only std::string and const std::string& are typemapped | |
13 they are converted to the Lua strings automatically | |
14 | |
15 std::string& and std::string* are not | |
16 they must be explicitly managed (see below) | |
17 | |
18 eg. | |
19 | |
20 std::string test_value(std::string x) { | |
21 return x; | |
22 } | |
23 | |
24 can be used as | |
25 | |
26 s="hello world" | |
27 s2=test_value(s) | |
28 assert(s==s2) | |
29 */ | |
30 | |
31 namespace std { | |
32 | |
33 %naturalvar string; | |
34 | |
35 /* | |
36 Bug report #1526022: | |
37 Lua strings and std::string can contain embedded zero bytes | |
38 Therefore a standard out typemap should not be: | |
39 lua_pushstring(L,$1.c_str()); | |
40 but | |
41 lua_pushlstring(L,$1.data(),$1.size()); | |
42 | |
43 Similarly for getting the string | |
44 $1 = (char*)lua_tostring(L, $input); | |
45 becomes | |
46 $1.assign(lua_tostring(L,$input),lua_rawlen(L,$input)); | |
47 | |
48 Not using: lua_tolstring() as this is only found in Lua 5.1 & not 5.0.2 | |
49 */ | |
50 | |
51 %typemap(in,checkfn="lua_isstring") string | |
52 %{$1.assign(lua_tostring(L,$input),lua_rawlen(L,$input));%} | |
53 | |
54 %typemap(out) string | |
55 %{ lua_pushlstring(L,$1.data(),$1.size()); SWIG_arg++;%} | |
56 | |
57 %typemap(in,checkfn="lua_isstring") const string& ($*1_ltype temp) | |
58 %{temp.assign(lua_tostring(L,$input),lua_rawlen(L,$input)); $1=&temp;%} | |
59 | |
60 %typemap(out) const string& | |
61 %{ lua_pushlstring(L,$1->data(),$1->size()); SWIG_arg++;%} | |
62 | |
63 // for throwing of any kind of string, string ref's and string pointers | |
64 // we convert all to lua strings | |
65 %typemap(throws) string, string&, const string& | |
66 %{ lua_pushlstring(L,$1.data(),$1.size()); SWIG_fail;%} | |
67 | |
68 %typemap(throws) string*, const string* | |
69 %{ lua_pushlstring(L,$1->data(),$1->size()); SWIG_fail;%} | |
70 | |
71 %typecheck(SWIG_TYPECHECK_STRING) string, const string& { | |
72 $1 = lua_isstring(L,$input); | |
73 } | |
74 | |
75 /* | |
76 std::string& can be wrapped, but you must inform SWIG if it is in or out | |
77 | |
78 eg: | |
79 void fn(std::string& str); | |
80 Is this an in/out/inout value? | |
81 | |
82 Therefore you need the usual | |
83 %apply (std::string& INOUT) {std::string& str}; | |
84 or | |
85 %apply std::string& INOUT {std::string& str}; | |
86 typemaps to tell SWIG what to do. | |
87 */ | |
88 | |
89 %typemap(in) string &INPUT=const string &; | |
90 %typemap(in, numinputs=0) string &OUTPUT ($*1_ltype temp) | |
91 %{ $1 = &temp; %} | |
92 %typemap(argout) string &OUTPUT | |
93 %{ lua_pushlstring(L,$1->data(),$1->size()); SWIG_arg++;%} | |
94 %typemap(in) string &INOUT =const string &; | |
95 %typemap(argout) string &INOUT = string &OUTPUT; | |
96 | |
97 /* | |
98 A really cut down version of the string class | |
99 | |
100 This provides basic mapping of lua strings <-> std::string | |
101 and little else | |
102 (the std::string has a lot of unneeded functions anyway) | |
103 | |
104 note: no fn's taking the const string& | |
105 as this is overloaded by the const char* version | |
106 */ | |
107 | |
108 class string { | |
109 public: | |
110 string(); | |
111 string(const char*); | |
112 //string(const string&); | |
113 unsigned int size() const; | |
114 unsigned int length() const; | |
115 bool empty() const; | |
116 // no support for operator[] | |
117 const char* c_str()const; | |
118 const char* data()const; | |
119 // assign does not return a copy of this object | |
120 // (no point in a scripting language) | |
121 void assign(const char*); | |
122 //void assign(const string&); | |
123 // no support for all the other features | |
124 // it's probably better to do it in lua | |
125 }; | |
126 } | |
127 |