| 1 | // |
| 2 | // SWIG typemaps for std::string |
| 3 | // Roy M. LeCates |
| 4 | // October 23, 2002 |
| 5 | // |
| 6 | // Perl implementation |
| 7 | |
| 8 | |
| 9 | // ------------------------------------------------------------------------ |
| 10 | // std::string is typemapped by value |
| 11 | // This can prevent exporting methods which return a string |
| 12 | // in order for the user to modify it. |
| 13 | // However, I think I'll wait until someone asks for it... |
| 14 | // ------------------------------------------------------------------------ |
| 15 | |
| 16 | %include exception.i |
| 17 | |
| 18 | %{ |
| 19 | #include <string> |
| 20 | %} |
| 21 | |
| 22 | namespace std { |
| 23 | |
| 24 | class string; |
| 25 | |
| 26 | /* Overloading check */ |
| 27 | %typemap(typecheck) string = char *; |
| 28 | %typemap(typecheck) const string & = char *; |
| 29 | |
| 30 | %typemap(in) string { |
| 31 | STRLEN len; |
| 32 | const char *ptr = SvPV($input, len); |
| 33 | if (!ptr) { |
| 34 | SWIG_croak("Undefined variable in argument $argnum of $symname."); |
| 35 | } else { |
| 36 | $1 = std::string(ptr, len); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | %typemap(in) string *INPUT(std::string temp), |
| 41 | const string & (std::string temp) { |
| 42 | STRLEN len; |
| 43 | const char *ptr = SvPV($input, len); |
| 44 | if (!ptr) { |
| 45 | SWIG_croak("Undefined variable in argument $argnum of $symname."); |
| 46 | } else { |
| 47 | temp.assign(ptr, len); |
| 48 | $1 = &temp; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | %typemap(out) string { |
| 53 | if (argvi >= items) EXTEND(sp, 1); // bump stack ptr, if needed |
| 54 | char *data = const_cast<char*>($1.data()); |
| 55 | sv_setpvn($result = sv_newmortal(), data, $1.size()); |
| 56 | ++argvi; |
| 57 | } |
| 58 | |
| 59 | %typemap(out) const string & { |
| 60 | if (argvi >= items) EXTEND(sp, 1); // bump stack ptr, if needed |
| 61 | char *data = const_cast<char*>($1->data()); |
| 62 | sv_setpvn($result = sv_newmortal(), data, $1->size()); |
| 63 | ++argvi; |
| 64 | } |
| 65 | |
| 66 | %typemap(throws) const string & { |
| 67 | SWIG_croak($1.c_str()); |
| 68 | } |
| 69 | |
| 70 | %typemap(throws) string { |
| 71 | SWIG_croak($1.c_str()); |
| 72 | } |
| 73 | |
| 74 | } |
| 75 | |