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