1 // This tests SWIG's handling of typemaps and namespaces
2 %module namespace_typemap
6 /* A minimalistic string class */
13 string_class(const char *s) {
14 data = new char[strlen(s)+1];
18 if (data) delete [] data;
25 /* A minimalistic test_complex class */
30 test_complex(double r = 0, double i = 0) {
44 /* SWIG interface tests */
47 %typemap(in) test::test_complex * {
48 if (PyComplex_Check($input)) {
49 $1 = new test_complex(PyComplex_RealAsDouble($input),
50 PyComplex_ImagAsDouble($input));
52 PyErr_SetString(PyExc_TypeError,"Expected test_complex.\n");
56 %typemap(freearg) test::test_complex * {
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());
66 error("Expected test_complex.");
69 %typemap(freearg) test::test_complex * {
77 %typemap(in) string_class * {
78 $1 = new string_class(SWIG_Python_str_AsChar($input));
80 %typemap(freearg) string_class * {
85 %typemap(in) string_class * {
86 $1 = new string_class($input.string_value().c_str());
88 %typemap(freearg) string_class * {
93 %typemap(in) string_class * {
94 $1 = new string_class(STR2CSTR($input));
96 %typemap(freearg) string_class * {
107 /* Functions in the namespace itself */
108 char *stest1(string_class *s) {
111 double ctest1(test_complex *c) {
117 using test::string_class;
118 using test::test_complex;
120 /* Functions in another namespace */
121 char *stest2(string_class *s) {
124 double ctest2(test_complex *c) {
130 using namespace test;
132 char *stest3(string_class *s) {
135 double ctest3(test_complex *c) {
141 using namespace test2;
143 char *stest4(string_class *s) {
146 double ctest4(test_complex *c) {
152 using namespace test3;
154 char *stest5(string_class *s) {
157 double ctest5(test_complex *c) {
162 char *stest6(test::string_class *s) {
165 double ctest6(test::test_complex *c) {
169 char *stest7(test2::string_class *s) {
172 double ctest7(test2::test_complex *c) {
176 char *stest8(test3::string_class *s) {
179 double ctest8(test3::test_complex *c) {
183 char *stest9(test4::string_class *s) {
186 double ctest9(test4::test_complex *c) {
190 char *stest10(test5::string_class *s) {
193 double ctest10(test5::test_complex *c) {
197 namespace test11 = test;
199 char *stest11(test11::string_class *s) {
202 double ctest11(test11::test_complex *c) {
206 using namespace test2;
207 using test::test_complex;
209 char *stest12(string_class *s) {
212 double ctest12(test_complex *c) {
219 %typemap(in) PosInteger {
220 $1 = PyInt_AsLong($input);
222 PyErr_SetString(PyExc_ValueError,"domain error\n");
228 %typemap(in) PosInteger {
229 $1 = $input.long_value();
231 error("domain error");
236 %typemap(in) PosInteger {
237 $1 = NUM2INT($input);
239 rb_raise(rb_eRangeError, "domain error");
247 typedef int PosInteger;
248 PosInteger ttest1(PosInteger x) {