1 /* ----------------------------------------------------------------------------- 2 * std_string.i 3 * 4 * Typemaps for std::string and const std::string& 5 * These are mapped to a D char[] and are passed around by value. 6 * 7 * To use non-const std::string references, use the following %apply. Note 8 * that they are passed by value. 9 * %apply const std::string & {std::string &}; 10 * ----------------------------------------------------------------------------- */ 11 12 %{ 13 #include <string> 14 %} 15 16 namespace std { 17 18 %naturalvar string; 19 20 class string; 21 22 %define SWIGD_STD_STRING_TYPEMAPS(DW_STRING_TYPE, DP_STRING_TYPE, FROM_STRINGZ, TO_STRINGZ) 23 // string 24 %typemap(ctype) string, const string & "char *" 25 %typemap(imtype) string, const string & #DW_STRING_TYPE 26 %typemap(dtype) string, const string & #DP_STRING_TYPE 27 28 %typemap(in, canthrow=1) string, const string & 29 %{ if (!$input) { 30 SWIG_DSetPendingException(SWIG_DIllegalArgumentException, "null string"); 31 return $null; 32 } 33 $1.assign($input); %} 34 %typemap(in, canthrow=1) const string & 35 %{ if (!$input) { 36 SWIG_DSetPendingException(SWIG_DIllegalArgumentException, "null string"); 37 return $null; 38 } 39 $*1_ltype $1_str($input); 40 $1 = &$1_str; %} 41 42 %typemap(out) string %{ $result = SWIG_d_string_callback($1.c_str()); %} 43 %typemap(out) const string & %{ $result = SWIG_d_string_callback($1->c_str()); %} 44 45 %typemap(din) string, const string & "($dinput ? TO_STRINGZ($dinput) : null)" 46 %typemap(dout, excode=SWIGEXCODE) string, const string & { 47 DP_STRING_TYPE ret = FROM_STRINGZ($imcall);$excode 48 return ret; 49 } 50 51 %typemap(directorin) string, const string & %{ $input = SWIG_d_string_callback($1.c_str()); %} 52 53 %typemap(directorout, canthrow=1) string 54 %{ if (!$input) { 55 SWIG_DSetPendingException(SWIG_DIllegalArgumentException, "null string"); 56 return $null; 57 } 58 $result.assign($input); %} 59 60 %typemap(directorout, canthrow=1, warning=SWIGWARN_TYPEMAP_THREAD_UNSAFE_MSG) const string & 61 %{ if (!$input) { 62 SWIG_DSetPendingException(SWIG_DIllegalArgumentException, "null string"); 63 return $null; 64 } 65 /* possible thread/reentrant code problem */ 66 static $*1_ltype $1_str; 67 $1_str = $input; 68 $result = &$1_str; %} 69 70 %typemap(ddirectorin) string, const string & "FROM_STRINGZ($winput)" 71 %typemap(ddirectorout) string, const string & "TO_STRINGZ($dcall)" 72 73 %typemap(throws, canthrow=1) string, const string & 74 %{ SWIG_DSetPendingException(SWIG_DException, $1.c_str()); 75 return $null; %} 76 77 %typemap(typecheck) string, const string & = char *; 78 %enddef 79 80 // We need to have the \0-terminated string conversion functions available in 81 // the D proxy modules. 82 #if (SWIG_D_VERSION == 1) 83 // Could be easily extended to support Phobos as well. 84 SWIGD_STD_STRING_TYPEMAPS(char*, char[], tango.stdc.stringz.fromStringz, tango.stdc.stringz.toStringz) 85 86 %pragma(d) globalproxyimports = "static import tango.stdc.stringz;"; 87 #else 88 SWIGD_STD_STRING_TYPEMAPS(const(char)*, string, std.conv.to!string, std.string.toStringz) 89 90 %pragma(d) globalproxyimports = %{ 91 static import std.conv; 92 static import std.string; 93 %} 94 #endif 95 96 #undef SWIGD_STD_STRING_TYPEMAPS 97 98 } // namespace std 99