1 /* ----------------------------------------------------------------------------- 2 * std_string.i 3 * 4 * Typemaps for std::string and const std::string& 5 * These are mapped to a Go string 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 %typemap(gotype) string, const string & "string" 23 24 %typemap(in) string 25 %{ $1.assign($input.p, $input.n); %} 26 27 %typemap(godirectorout) string 28 %{ 29 { 30 p := Swig_malloc(len($input)) 31 s := (*[1<<30]byte)(unsafe.Pointer(p))[:len($input)] 32 copy(s, $input) 33 $result = *(*string)(unsafe.Pointer(&s)) 34 } 35 %} 36 37 %typemap(directorout) string 38 %{ 39 $result.assign($input.p, $input.n); 40 free($input.p); 41 %} 42 43 %typemap(out,fragment="AllocateString") string 44 %{ $result = Swig_AllocateString($1.data(), $1.length()); %} 45 46 %typemap(goout,fragment="CopyString") string 47 %{ $result = swigCopyString($1) %} 48 49 %typemap(directorin,fragment="AllocateString") string 50 %{ $input = Swig_AllocateString($1.data(), $1.length()); %} 51 52 %typemap(godirectorin,fragment="CopyString") string 53 %{ $result = swigCopyString($input) %} 54 55 %typemap(in) const string & 56 %{ 57 $*1_ltype $1_str($input.p, $input.n); 58 $1 = &$1_str; 59 %} 60 61 %typemap(godirectorout) const string & 62 %{ 63 { 64 p := Swig_malloc(len($input)) 65 s := (*[1<<30]byte)(unsafe.Pointer(p))[:len($input)] 66 copy(s, $input) 67 $result = *(*string)(unsafe.Pointer(&s)) 68 } 69 %} 70 71 %typemap(directorout,warning=SWIGWARN_TYPEMAP_THREAD_UNSAFE_MSG) const string & 72 %{ 73 static $*1_ltype $1_str; 74 $1_str.assign($input.p, $input.n); 75 free($input.p); 76 $result = &$1_str; 77 %} 78 79 %typemap(out,fragment="AllocateString") const string & 80 %{ $result = Swig_AllocateString((*$1).data(), (*$1).length()); %} 81 82 %typemap(goout,fragment="CopyString") const string & 83 %{ $result = swigCopyString($1) %} 84 85 %typemap(directorin,fragment="AllocateString") const string & 86 %{ $input = Swig_AllocateString($1.data(), $1.length()); %} 87 88 %typemap(godirectorin,fragment="CopyString") const string & 89 %{ $result = swigCopyString($input) %} 90 91 } 92