annotate lib/swig/swigwin-2.0.11/Lib/lua/std_string.i @ 2044:28cb79ae2f6f

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