1 /* -----------------------------------------------------------------------------
2 * See the LICENSE file for information on copyright, usage and redistribution
3 * of SWIG, and the README file for authors - http://www.swig.org/release.html.
7 * SWIG typemaps for std::string
8 * ----------------------------------------------------------------------------- */
10 // ------------------------------------------------------------------------
11 // std::string is typemapped by value
12 // This can prevent exporting methods which return a string
13 // in order for the user to modify it.
14 // However, I think I'll wait until someone asks for it...
15 // ------------------------------------------------------------------------
17 %include <exception.i>
26 %include <std_vector.i>
28 %naturalvar std::string;
29 %naturalvar std::wstring;
32 template <class charT> class basic_string {
34 typedef charT *pointer;
35 typedef charT &reference;
36 typedef const charT &const_reference;
37 typedef size_t size_type;
38 typedef ptrdiff_t difference_type;
40 basic_string( charT *str );
42 charT operator []( int pos ) const;
44 basic_string<charT> &operator = ( const basic_string &ws );
45 basic_string<charT> &operator = ( const charT *str );
46 basic_string<charT> &append( const basic_string<charT> &other );
47 basic_string<charT> &append( const charT *str );
48 void push_back( charT c );
50 void reserve( size_type t );
51 void resize( size_type n, charT c = charT() );
52 int compare( const basic_string<charT> &other ) const;
53 int compare( const charT *str ) const;
54 basic_string<charT> &insert( size_type pos,
55 const basic_string<charT> &str );
56 size_type find( const basic_string<charT> &other, int pos = 0 ) const;
57 size_type find( charT c, int pos = 0 ) const;
59 bool operator == ( const basic_string<charT> &other ) const {
60 return self->compare( other ) == 0;
62 bool operator != ( const basic_string<charT> &other ) const {
63 return self->compare( other ) != 0;
65 bool operator < ( const basic_string<charT> &other ) const {
66 return self->compare( other ) == -1;
68 bool operator > ( const basic_string<charT> &other ) const {
69 return self->compare( other ) == 1;
71 bool operator <= ( const basic_string<charT> &other ) const {
72 return self->compare( other ) != 1;
74 bool operator >= ( const basic_string<charT> &other ) const {
75 return self->compare( other ) != -1;
80 %template(string) basic_string<char>;
81 %template(wstring) basic_string<wchar_t>;
82 typedef basic_string<char> string;
83 typedef basic_string<wchar_t> wstring;
85 /* Overloading check */
87 if (caml_ptr_check($input))
88 $1.assign((char *)caml_ptr_val($input,0),
89 caml_string_len($input));
91 SWIG_exception(SWIG_TypeError, "string expected");
94 %typemap(in) const string & (std::string temp) {
95 if (caml_ptr_check($input)) {
96 temp.assign((char *)caml_ptr_val($input,0),
97 caml_string_len($input));
100 SWIG_exception(SWIG_TypeError, "string expected");
104 %typemap(in) string & (std::string temp) {
105 if (caml_ptr_check($input)) {
106 temp.assign((char *)caml_ptr_val($input,0),
107 caml_string_len($input));
110 SWIG_exception(SWIG_TypeError, "string expected");
114 %typemap(in) string * (std::string *temp) {
115 if (caml_ptr_check($input)) {
116 temp = new std::string((char *)caml_ptr_val($input,0),
117 caml_string_len($input));
120 SWIG_exception(SWIG_TypeError, "string expected");
124 %typemap(free) string * (std::string *temp) {
128 %typemap(argout) string & {
129 caml_list_append(swig_result,caml_val_string_len((*$1).c_str(),
133 %typemap(directorout) string {
134 $result.assign((char *)caml_ptr_val($input,0),
135 caml_string_len($input));
138 %typemap(out) string {
139 $result = caml_val_string_len($1.c_str(),$1.size());
142 %typemap(out) string * {
143 $result = caml_val_string_len((*$1).c_str(),(*$1).size());
147 #ifdef ENABLE_CHARPTR_ARRAY
148 char **c_charptr_array( const std::vector <string > &str_v );
151 SWIGEXT char **c_charptr_array( const std::vector <string > &str_v ) {
152 char **out = new char *[str_v.size() + 1];
153 out[str_v.size()] = 0;
154 for( int i = 0; i < str_v.size(); i++ ) {
155 out[i] = (char *)str_v[i].c_str();
162 #ifdef ENABLE_STRING_VECTOR
163 %template (StringVector) std::vector<string >;
166 (* Some STL convenience items *)
168 let string_array_to_vector sa =
169 let nv = _new_StringVector C_void in
170 array_to_vector nv (fun x -> C_string x) sa ; nv
172 let c_string_array ar =
173 _c_charptr_array (string_array_to_vector ar)
177 val c_string_array: string array -> c_obj