summaryrefslogtreecommitdiff
path: root/Lib/d/std_string.i
blob: 7a4accf18982feffa2133c934f8f58182d819665 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* -----------------------------------------------------------------------------
 * std_string.i
 *
 * Typemaps for std::string and const std::string&
 * These are mapped to a D char[] and are passed around by value.
 *
 * To use non-const std::string references, use the following %apply. Note
 * that they are passed by value.
 * %apply const std::string & {std::string &};
 * ----------------------------------------------------------------------------- */

%{
#include <string>
%}

namespace std {

%naturalvar string;

class string;

%define SWIGD_STD_STRING_TYPEMAPS(DW_STRING_TYPE, DP_STRING_TYPE, FROM_STRINGZ, TO_STRINGZ)
// string
%typemap(ctype) string, const string & "char *"
%typemap(imtype) string, const string & #DW_STRING_TYPE
%typemap(dtype) string, const string & #DP_STRING_TYPE

%typemap(in, canthrow=1) string, const string &
%{ if (!$input) {
    SWIG_DSetPendingException(SWIG_DIllegalArgumentException, "null string");
    return $null;
  }
  $1.assign($input); %}
%typemap(in, canthrow=1) const string &
%{ if (!$input) {
    SWIG_DSetPendingException(SWIG_DIllegalArgumentException, "null string");
    return $null;
   }
   std::string $1_str($input);
   $1 = &$1_str; %}

%typemap(out) string %{ $result = SWIG_d_string_callback($1.c_str()); %}
%typemap(out) const string & %{ $result = SWIG_d_string_callback($1->c_str()); %}

%typemap(din) string, const string & "($dinput ? TO_STRINGZ($dinput) : null)"
%typemap(dout, excode=SWIGEXCODE) string, const string & {
  DP_STRING_TYPE ret = FROM_STRINGZ($imcall);$excode
  return ret;
}

%typemap(directorin) string, const string & %{ $input = SWIG_d_string_callback($1.c_str()); %}

%typemap(directorout, canthrow=1) string
%{ if (!$input) {
    SWIG_DSetPendingException(SWIG_DIllegalArgumentException, "null string");
    return $null;
  }
  $result.assign($input); %}

%typemap(directorout, canthrow=1, warning=SWIGWARN_TYPEMAP_THREAD_UNSAFE_MSG) const string &
%{ if (!$input) {
    SWIG_DSetPendingException(SWIG_DIllegalArgumentException, "null string");
    return $null;
  }
  /* possible thread/reentrant code problem */
  static std::string $1_str;
  $1_str = $input;
  $result = &$1_str; %}

%typemap(ddirectorin) string, const string & "FROM_STRINGZ($winput)"
%typemap(ddirectorout) string, const string & "TO_STRINGZ($dcall)"

%typemap(throws, canthrow=1) string, const string &
%{ SWIG_DSetPendingException(SWIG_DException, $1.c_str());
  return $null; %}

%typemap(typecheck) string, const string & = char *;
%enddef

// We need to have the \0-terminated string conversion functions available in
// the D proxy modules.
#if (SWIG_D_VERSION == 1)
// Could be easily extended to support Phobos as well.
SWIGD_STD_STRING_TYPEMAPS(char*, char[], tango.stdc.stringz.fromStringz, tango.stdc.stringz.toStringz)

%pragma(d) globalproxyimports = "static import tango.stdc.stringz;";
#else
SWIGD_STD_STRING_TYPEMAPS(const(char)*, string, std.conv.to!string, std.string.toStringz)

%pragma(d) globalproxyimports = %{
static import std.conv;
static import std.string;
%}
#endif

#undef SWIGD_STD_STRING_TYPEMAPS

} // namespace std