1 /* ----------------------------------------------------------------------------- 2 * std_string.i 3 * 4 * SWIG typemaps for std::string 5 * ----------------------------------------------------------------------------- */ 6 7 // ------------------------------------------------------------------------ 8 // std::string is typemapped by value 9 // This can prevent exporting methods which return a string 10 // in order for the user to modify it. 11 // However, I think I'll wait until someone asks for it... 12 // ------------------------------------------------------------------------ 13 14 // %include <exception.i> 15 %warnfilter(404) std::string; 16 %warnfilter(404) std::wstring; 17 18 %{ 19 #include <string> 20 %} 21 22 // %include <std_vector.i> 23 24 // %naturalvar std::string; 25 // %naturalvar std::wstring; 26 27 namespace std { 28 typedef unsigned long size_t; 29 typedef signed long ptrdiff_t; 30 31 template <class charT> class basic_string { 32 public: 33 typedef charT *pointer; 34 typedef charT &reference; 35 typedef const charT &const_reference; 36 typedef size_t size_type; 37 typedef ptrdiff_t difference_type; 38 basic_string(); 39 basic_string( charT *str ); 40 size_type size(); 41 charT operator []( int pos ) const; 42 charT *c_str() const; 43 basic_string<charT> &operator = ( const basic_string &ws ); 44 basic_string<charT> &operator = ( const charT *str ); 45 basic_string<charT> &append( const basic_string<charT> &other ); 46 basic_string<charT> &append( const charT *str ); 47 void push_back( charT c ); 48 void clear(); 49 void reserve( size_type t ); 50 void resize( size_type n, charT c = charT() ); 51 int compare( const basic_string<charT> &other ) const; 52 int compare( const charT *str ) const; 53 basic_string<charT> &insert( size_type pos, 54 const basic_string<charT> &str ); 55 size_type find( const basic_string<charT> &other, int pos = 0 ) const; 56 size_type find( charT c, int pos = 0 ) const; 57 %extend { 58 bool operator == ( const basic_string<charT> &other ) const { 59 return self->compare( other ) == 0; 60 } 61 bool operator != ( const basic_string<charT> &other ) const { 62 return self->compare( other ) != 0; 63 } 64 bool operator < ( const basic_string<charT> &other ) const { 65 return self->compare( other ) == -1; 66 } 67 bool operator > ( const basic_string<charT> &other ) const { 68 return self->compare( other ) == 1; 69 } 70 bool operator <= ( const basic_string<charT> &other ) const { 71 return self->compare( other ) != 1; 72 } 73 bool operator >= ( const basic_string<charT> &other ) const { 74 return self->compare( other ) != -1; 75 } 76 77 } 78 }; 79 80 %template(string) basic_string<char>; 81 %template(wstring) basic_string<wchar_t>; 82 83 %apply char * { string }; 84 %apply wchar_t * { wstring }; 85 86 typedef basic_string<char> string; 87 typedef basic_string<wchar_t> wstring; 88 89 // automatically convert constant std::strings to cl:strings 90 %typemap(ctype) string "char *"; 91 %typemap(in) string "$1.assign($input);"; 92 %typemap(out) string "$result = (char *)(&$1)->c_str();"; 93 %typemap(lisptype) string "cl:string"; 94 %typemap(lout) string "(cl::setq ACL_ffresult $body)"; 95 96 %typemap(ctype) const string *"char *"; 97 %typemap(in) const string * "$1.assign($input);"; 98 %typemap(out) const string * "$result = (char *)($1)->c_str();"; 99 %typemap(lisptype) const string * "cl:string"; 100 %typemap(lout) const string * "(cl::setq ACL_ffresult $body)"; 101 102 %typemap(ctype) wstring "wchar_t *"; 103 %typemap(in) wstring "$1.assign($input);"; 104 %typemap(out) wstring "$result = (wchar_t *)(&$1)->c_str();"; 105 %typemap(lisptype) wstring "cl:string"; 106 %typemap(lout) wstring "(cl::setq ACL_ffresult (excl:native-to-string $body 107 :external-format #+little-endian :fat-le #-little-endian :fat))"; 108 109 %typemap(ctype) const wstring *"char *"; 110 %typemap(in) const wstring * "$1.assign($input);"; 111 %typemap(out) const wstring * "$result = (char *)($1)->c_str();"; 112 %typemap(lisptype) const wstring * "cl:string"; 113 %typemap(lout) const wstring * "(cl::setq ACL_ffresult $body)"; 114 115 /* Overloading check */ 116 // %typemap(in) string { 117 // if (caml_ptr_check($input)) 118 // $1.assign((char *)caml_ptr_val($input,0), 119 // caml_string_len($input)); 120 // else 121 // SWIG_exception(SWIG_TypeError, "string expected"); 122 // } 123 124 // %typemap(in) const string & ($*1_ltype temp) { 125 // if (caml_ptr_check($input)) { 126 // temp.assign((char *)caml_ptr_val($input,0), 127 // caml_string_len($input)); 128 // $1 = &temp; 129 // } else { 130 // SWIG_exception(SWIG_TypeError, "string expected"); 131 // } 132 // } 133 134 // %typemap(in) string & ($*1_ltype temp) { 135 // if (caml_ptr_check($input)) { 136 // temp.assign((char *)caml_ptr_val($input,0), 137 // caml_string_len($input)); 138 // $1 = &temp; 139 // } else { 140 // SWIG_exception(SWIG_TypeError, "string expected"); 141 // } 142 // } 143 144 // %typemap(in) string * ($*1_ltype *temp) { 145 // if (caml_ptr_check($input)) { 146 // temp = new $*1_ltype((char *)caml_ptr_val($input,0), 147 // caml_string_len($input)); 148 // $1 = temp; 149 // } else { 150 // SWIG_exception(SWIG_TypeError, "string expected"); 151 // } 152 // } 153 154 // %typemap(free) string * ($*1_ltype *temp) { 155 // delete temp; 156 // } 157 158 // %typemap(argout) string & { 159 // caml_list_append(swig_result,caml_val_string_len((*$1).c_str(), 160 // (*$1).size())); 161 // } 162 163 // %typemap(directorout) string { 164 // $result.assign((char *)caml_ptr_val($input,0), 165 // caml_string_len($input)); 166 // } 167 168 // %typemap(out) string { 169 // $result = caml_val_string_len($1.c_str(),$1.size()); 170 // } 171 172 // %typemap(out) string * { 173 // $result = caml_val_string_len((*$1).c_str(),(*$1).size()); 174 // } 175 } 176 177 // #ifdef ENABLE_CHARPTR_ARRAY 178 // char **c_charptr_array( const std::vector <string > &str_v ); 179 180 // %{ 181 // SWIGEXT char **c_charptr_array( const std::vector <string > &str_v ) { 182 // char **out = new char *[str_v.size() + 1]; 183 // out[str_v.size()] = 0; 184 // for( int i = 0; i < str_v.size(); i++ ) { 185 // out[i] = (char *)str_v[i].c_str(); 186 // } 187 // return out; 188 // } 189 // %} 190 // #endif 191 192 // #ifdef ENABLE_STRING_VECTOR 193 // %template (StringVector) std::vector<string >; 194 195 // %insert(ml) %{ 196 // (* Some STL convenience items *) 197 198 // let string_array_to_vector sa = 199 // let nv = _new_StringVector C_void in 200 // array_to_vector nv (fun x -> C_string x) sa ; nv 201 202 // let c_string_array ar = 203 // _c_charptr_array (string_array_to_vector ar) 204 // %} 205 206 // %insert(mli) %{ 207 // val c_string_array: string array -> c_obj 208 // %} 209 // #endif 210