import source from 1.3.40
[external/swig.git] / Examples / test-suite / namespace_typemap.i
1 // This tests SWIG's handling of typemaps and namespaces
2 %module namespace_typemap
3
4 %{
5 namespace test {
6    /* A minimalistic string class */
7    class string_class {
8       char *data;
9    public:
10       string_class() {
11         data = 0;
12       }
13       string_class(const char *s) {
14         data = new char[strlen(s)+1];
15         strcpy(data,s);
16       }
17      ~string_class() {
18         if (data) delete [] data;
19       }
20       char *c_str() {
21         return data;
22       }
23    };
24
25    /* A minimalistic test_complex class */
26    class test_complex {
27       double re;
28       double im;
29    public:
30       test_complex(double r = 0, double i = 0) {
31         re = r;
32         im = i;
33       }
34       double real() {
35         return re;
36       }
37       double imag() {
38         return im;
39       }
40    };
41 }
42  %}
43
44 /* SWIG interface tests */
45
46 #ifdef SWIGPYTHON
47 %typemap(in) test::test_complex * {
48     if (PyComplex_Check($input)) {
49         $1 = new test_complex(PyComplex_RealAsDouble($input),
50                          PyComplex_ImagAsDouble($input));
51     } else {
52         PyErr_SetString(PyExc_TypeError,"Expected test_complex.\n");
53         return NULL;
54     }
55 }
56 %typemap(freearg) test::test_complex * {
57     delete $1;
58 }
59 #endif
60 #ifdef SWIGOCTAVE
61 %typemap(in) test::test_complex * {
62     if ($input.is_complex_scalar()) {
63         $1 = new test_complex($input.complex_value().real(),
64                               $input.complex_value().imag());
65     } else {
66         error("Expected test_complex.");
67     }
68 }
69 %typemap(freearg) test::test_complex * {
70     delete $1;
71 }
72 #endif
73
74 namespace test {
75     class string_class;
76 #ifdef SWIGPYTHON
77         %typemap(in) string_class * {
78             $1 = new string_class(SWIG_Python_str_AsChar($input));
79         }
80         %typemap(freearg) string_class * {
81             delete $1;
82         }
83 #endif
84 #ifdef SWIGOCTAVE
85         %typemap(in) string_class * {
86             $1 = new string_class($input.string_value().c_str());
87         }
88         %typemap(freearg) string_class * {
89             delete $1;
90         }
91 #endif
92 #ifdef SWIGRUBY
93         %typemap(in) string_class * {
94             $1 = new string_class(STR2CSTR($input));
95         }
96         %typemap(freearg) string_class * {
97             delete $1;
98         }
99 #endif
100 }
101
102 %inline %{
103     namespace test {
104         class string_class;
105         class test_complex;
106
107         /* Functions in the namespace itself */
108         char *stest1(string_class *s) {
109             return s->c_str();
110         }
111         double ctest1(test_complex *c) {
112             return c->real();
113         }
114     }
115
116     namespace test2 {
117         using test::string_class;
118         using test::test_complex;
119
120         /* Functions in another namespace */
121         char *stest2(string_class *s) {
122             return s->c_str();
123         }
124         double ctest2(test_complex *c) {
125             return c->real();
126         }
127     }
128
129     namespace test3 {
130         using namespace test;
131
132         char *stest3(string_class *s) {
133             return s->c_str();
134         }
135         double ctest3(test_complex *c) {
136             return c->real();
137         }
138     }
139     
140     namespace test4 {
141         using namespace test2;
142
143         char *stest4(string_class *s) {
144             return s->c_str();
145         }
146         double ctest4(test_complex *c) {
147             return c->real();
148         }
149     }
150
151     namespace test5 {
152         using namespace test3;
153
154         char *stest5(string_class *s) {
155             return s->c_str();
156         }
157         double ctest5(test_complex *c) {
158             return c->real();
159         }
160     }
161
162     char *stest6(test::string_class *s) {
163         return s->c_str();
164     }
165     double ctest6(test::test_complex *c) {
166         return c->real();
167     }
168
169     char *stest7(test2::string_class *s) {
170         return s->c_str();
171     }
172     double ctest7(test2::test_complex *c) {
173         return c->real();
174     }
175
176     char *stest8(test3::string_class *s) {
177         return s->c_str();
178     }
179     double ctest8(test3::test_complex *c) {
180         return c->real();
181     }
182
183     char *stest9(test4::string_class *s) {
184         return s->c_str();
185     }
186     double ctest9(test4::test_complex *c) {
187         return c->real();
188     }
189
190     char *stest10(test5::string_class *s) {
191         return s->c_str();
192     }
193     double ctest10(test5::test_complex *c) {
194         return c->real();
195     }
196
197     namespace test11 = test;
198     
199     char *stest11(test11::string_class *s) {
200         return s->c_str();
201     }
202     double ctest11(test11::test_complex *c) {
203         return c->real();
204     }
205
206     using namespace test2;
207     using test::test_complex;
208
209     char *stest12(string_class *s) {
210         return s->c_str();
211     }
212     double ctest12(test_complex *c) {
213         return c->real();
214     }
215 %}
216
217 namespace Split {
218 #ifdef SWIGPYTHON
219     %typemap(in) PosInteger {
220         $1 = PyInt_AsLong($input);
221         if ($1 < 0) {
222             PyErr_SetString(PyExc_ValueError,"domain error\n");
223             return NULL;
224         }
225     }   
226 #endif
227 #ifdef SWIGOCTAVE
228     %typemap(in) PosInteger {
229         $1 = $input.long_value();
230         if ($1 < 0) {
231           error("domain error");
232         }
233     }   
234 #endif
235 #ifdef SWIGRUBY
236     %typemap(in) PosInteger {
237         $1 = NUM2INT($input);
238         if ($1 < 0) {
239             rb_raise(rb_eRangeError, "domain error");
240         }
241     }   
242 #endif
243 }
244     
245 %inline %{
246     namespace Split {
247         typedef int PosInteger;
248         PosInteger ttest1(PosInteger x) {
249             return x;
250         }
251     }
252 %}
253
254
255
256
257
258