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:
string_class()10       string_class() {
11 	data = 0;
12       }
string_class(const char * s)13       string_class(const char *s) {
14         data = new char[strlen(s)+1];
15 	strcpy(data,s);
16       }
~string_class()17      ~string_class() {
18         if (data) delete [] data;
19       }
c_str()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       }
real()34       double real() {
35         return re;
36       }
imag()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 	SWIG_fail;
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 #ifdef SWIGGO
74 %typemap(gotype) test::test_complex * "complex128"
75 %typemap(in) test::test_complex * {
76     $1 = new test_complex(__real__ $input, __imag__ $input);
77 }
78 %typemap(freearg) test::test_complex * {
79     delete $1;
80 }
81 #endif
82 
83 namespace test {
84     class string_class;
85 #ifdef SWIGPYTHON
86 	%typemap(in) string_class * {
87 	    $1 = new string_class(SWIG_Python_str_AsChar($input));
88 	}
89 	%typemap(freearg) string_class * {
90 	    delete $1;
91 	}
92 #endif
93 #ifdef SWIGOCTAVE
94 	%typemap(in) string_class * {
95 	    $1 = new string_class($input.string_value().c_str());
96 	}
97 	%typemap(freearg) string_class * {
98 	    delete $1;
99 	}
100 #endif
101 #ifdef SWIGRUBY
102 	%typemap(in) string_class * {
103 	    $1 = new string_class(StringValuePtr($input));
104 	}
105 	%typemap(freearg) string_class * {
106 	    delete $1;
107 	}
108 #endif
109 #ifdef SWIGGO
110 	%typemap(gotype) string_class * "string"
111 	%typemap(in) string_class * {
112 	    char* buf = new char[$input.n + 1];
113 	    memcpy(buf, $input.p, $input.n);
114 	    buf[$input.n] = '\0';
115 	    $1 = new string_class(buf);
116 	    delete[] buf;
117 	}
118 	%typemap(freearg) string_class * {
119 	    delete $1;
120 	}
121 #endif
122 }
123 
124 %inline %{
125     namespace test {
126 	class string_class;
127 	class test_complex;
128 
129 	/* Functions in the namespace itself */
stest1(string_class * s)130 	char *stest1(string_class *s) {
131 	    return s->c_str();
132 	}
ctest1(test_complex * c)133 	double ctest1(test_complex *c) {
134 	    return c->real();
135 	}
136     }
137 
138     namespace test2 {
139 	using test::string_class;
140 	using test::test_complex;
141 
142 	/* Functions in another namespace */
stest2(string_class * s)143 	char *stest2(string_class *s) {
144 	    return s->c_str();
145 	}
ctest2(test_complex * c)146 	double ctest2(test_complex *c) {
147 	    return c->real();
148 	}
149     }
150 
151     namespace test3 {
152 	using namespace test;
153 
stest3(string_class * s)154 	char *stest3(string_class *s) {
155 	    return s->c_str();
156 	}
ctest3(test_complex * c)157 	double ctest3(test_complex *c) {
158 	    return c->real();
159 	}
160     }
161 
162     namespace test4 {
163 	using namespace test2;
164 
stest4(string_class * s)165 	char *stest4(string_class *s) {
166 	    return s->c_str();
167 	}
ctest4(test_complex * c)168 	double ctest4(test_complex *c) {
169 	    return c->real();
170 	}
171     }
172 
173     namespace test5 {
174 	using namespace test3;
175 
stest5(string_class * s)176 	char *stest5(string_class *s) {
177 	    return s->c_str();
178 	}
ctest5(test_complex * c)179 	double ctest5(test_complex *c) {
180 	    return c->real();
181 	}
182     }
183 
stest6(test::string_class * s)184     char *stest6(test::string_class *s) {
185 	return s->c_str();
186     }
ctest6(test::test_complex * c)187     double ctest6(test::test_complex *c) {
188 	return c->real();
189     }
190 
stest7(test2::string_class * s)191     char *stest7(test2::string_class *s) {
192 	return s->c_str();
193     }
ctest7(test2::test_complex * c)194     double ctest7(test2::test_complex *c) {
195 	return c->real();
196     }
197 
stest8(test3::string_class * s)198     char *stest8(test3::string_class *s) {
199 	return s->c_str();
200     }
ctest8(test3::test_complex * c)201     double ctest8(test3::test_complex *c) {
202 	return c->real();
203     }
204 
stest9(test4::string_class * s)205     char *stest9(test4::string_class *s) {
206 	return s->c_str();
207     }
ctest9(test4::test_complex * c)208     double ctest9(test4::test_complex *c) {
209 	return c->real();
210     }
211 
stest10(test5::string_class * s)212     char *stest10(test5::string_class *s) {
213 	return s->c_str();
214     }
ctest10(test5::test_complex * c)215     double ctest10(test5::test_complex *c) {
216 	return c->real();
217     }
218 
219     namespace test11 = test;
220 
stest11(test11::string_class * s)221     char *stest11(test11::string_class *s) {
222 	return s->c_str();
223     }
ctest11(test11::test_complex * c)224     double ctest11(test11::test_complex *c) {
225 	return c->real();
226     }
227 
228     using namespace test2;
229     using test::test_complex;
230 
stest12(string_class * s)231     char *stest12(string_class *s) {
232 	return s->c_str();
233     }
ctest12(test_complex * c)234     double ctest12(test_complex *c) {
235 	return c->real();
236     }
237 %}
238 
239 namespace Split {
240 #ifdef SWIGPYTHON
241     %typemap(in) PosInteger {
242 	$1 = PyInt_AsLong($input);
243 	if ($1 < 0) {
244 	    PyErr_SetString(PyExc_ValueError,"domain error\n");
245 	    SWIG_fail;
246 	}
247     }
248 #endif
249 #ifdef SWIGOCTAVE
250     %typemap(in) PosInteger {
251 	$1 = $input.long_value();
252 	if ($1 < 0) {
253 	  error("domain error");
254 	}
255     }
256 #endif
257 #ifdef SWIGRUBY
258     %typemap(in) PosInteger {
259 	$1 = NUM2INT($input);
260 	if ($1 < 0) {
261 	    rb_raise(rb_eRangeError, "domain error");
262 	}
263     }
264 #endif
265 #ifdef SWIGGO
266     %typemap(in) PosInteger {
267 	$1 = $input;
268 	if ($1 < 0) {
269 	    _swig_gopanic("domain error");
270 	}
271     }
272 #endif
273 }
274 
275 %inline %{
276     namespace Split {
277 	typedef int PosInteger;
ttest1(PosInteger x)278 	PosInteger ttest1(PosInteger x) {
279 	    return x;
280 	}
281     }
282 %}
283 
284 
285 
286 
287 
288 
289