1 /* ----------------------------------------------------------------------------- 2 * std_string.i 3 * 4 * SWIG typemaps for std::string types 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 16 %{ 17 #include <string> 18 %} 19 20 namespace std { 21 22 %naturalvar string; 23 24 class string; 25 26 %typemap(typecheck,precedence=SWIG_TYPECHECK_STRING) string, const string& %{ 27 $1 = ( Z_TYPE_PP($input) == IS_STRING ) ? 1 : 0; 28 %} 29 30 %typemap(in) string %{ 31 convert_to_string_ex($input); 32 $1.assign(Z_STRVAL_PP($input), Z_STRLEN_PP($input)); 33 %} 34 35 %typemap(directorout) string %{ 36 convert_to_string_ex($input); 37 $result.assign(Z_STRVAL_PP($input), Z_STRLEN_PP($input)); 38 %} 39 40 %typemap(out) string %{ 41 ZVAL_STRINGL($result, const_cast<char*>($1.data()), $1.size(), 1); 42 %} 43 44 %typemap(directorin) string, const string& %{ 45 ZVAL_STRINGL($input, const_cast<char*>($1.data()), $1.size(), 1); 46 %} 47 48 %typemap(out) const string & %{ 49 ZVAL_STRINGL($result, const_cast<char*>($1->data()), $1->size(), 1); 50 %} 51 52 %typemap(throws) string, const string& %{ 53 zend_throw_exception(NULL, const_cast<char*>($1.c_str()), 0 TSRMLS_CC); 54 return; 55 %} 56 57 /* These next two handle a function which takes a non-const reference to 58 * a std::string and modifies the string. */ 59 %typemap(in) string & ($*1_ltype temp) %{ 60 convert_to_string_ex($input); 61 temp.assign(Z_STRVAL_PP($input), Z_STRLEN_PP($input)); 62 $1 = &temp; 63 %} 64 65 %typemap(directorout) string & ($*1_ltype *temp) %{ 66 convert_to_string_ex($input); 67 temp = new $*1_ltype(Z_STRVAL_PP($input), Z_STRLEN_PP($input)); 68 swig_acquire_ownership(temp); 69 $result = temp; 70 %} 71 72 %typemap(argout) string & %{ 73 ZVAL_STRINGL(*($input), const_cast<char*>($1->data()), $1->size(), 1); 74 %} 75 76 /* SWIG will apply the non-const typemap above to const string& without 77 * this more specific typemap. */ 78 %typemap(argout) const string & ""; 79 } 80