Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | // |
2 | // SWIG typemaps for std::string | |
3 | // Luigi Ballabio | |
4 | // Apr 8, 2002 | |
5 | // | |
6 | // Ruby 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 | // Ruby wants class names to start with a capital letter | |
25 | %rename(String) string; | |
26 | class string; | |
27 | ||
28 | /* Overloading check */ | |
29 | %typemap(typecheck) string = char *; | |
30 | %typemap(typecheck) const string & = char *; | |
31 | ||
32 | %typemap(in) string { | |
33 | if (TYPE($input) == T_STRING) { | |
34 | $1 = std::string(StringValuePtr($input)); | |
35 | } else { | |
36 | SWIG_exception(SWIG_TypeError, "not a string"); | |
37 | } | |
38 | } | |
39 | ||
40 | %typemap(in) const string & (std::string temp) { | |
41 | if (TYPE($input) == T_STRING) { | |
42 | temp = std::string(StringValuePtr($input)); | |
43 | $1 = &temp; | |
44 | } else { | |
45 | SWIG_exception(SWIG_TypeError, "not a string"); | |
46 | } | |
47 | } | |
48 | ||
49 | %typemap(out) string { | |
50 | $result = rb_str_new2($1.c_str()); | |
51 | } | |
52 | ||
53 | %typemap(out) const string & { | |
54 | $result = rb_str_new2($1->c_str()); | |
55 | } | |
56 | ||
57 | %typemap(directorin) string, const string &, string & { | |
58 | $input = rb_str_new2($1_name.c_str()); | |
59 | } | |
60 | ||
61 | %typemap(directorin) string *, const string * { | |
62 | $input = rb_str_new2($1_name->c_str()); | |
63 | } | |
64 | ||
65 | %typemap(directorout) string { | |
66 | if (TYPE($input) == T_STRING) | |
67 | $result = std::string(StringValuePtr($input)); | |
68 | else | |
69 | throw Swig::DirectorTypeMismatchException("string expected"); | |
70 | } | |
71 | ||
72 | %typemap(directorout,warning=SWIG_WARN_TYPEMAP_THREAD_UNSAFE) const string & { | |
73 | if (TYPE($input) == T_STRING) { | |
74 | static std::string temp = std::string(StringValuePtr($input)); | |
75 | $result = &temp; | |
76 | } else { | |
77 | throw Swig::DirectorTypeMismatchException("string expected"); | |
78 | } | |
79 | } | |
80 | ||
81 | %typemap(throws) string, const string & | |
82 | "rb_raise(rb_eRuntimeError, $1.c_str());"; | |
83 | ||
84 | %typemap(throws) string *, const string * | |
85 | "rb_raise(rb_eRuntimeError, $1->c_str());"; | |
86 | } |