PageRenderTime 13ms CodeModel.GetById 8ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/test-suite/namespace_typemap.i

#
Swig | 284 lines | 247 code | 32 blank | 5 comment | 0 complexity | fcc6e29b4c60a689ab8a9b2489ff74eb MD5 | raw file
  1// This tests SWIG's handling of typemaps and namespaces
  2%module namespace_typemap
  3
  4%{
  5namespace 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#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
 83namespace 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(STR2CSTR($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            $1 = new string_class($input.p);
113	}
114	%typemap(freearg) string_class * {
115	    delete $1;
116	}
117#endif
118}
119
120%inline %{
121    namespace test {
122	class string_class;
123	class test_complex;
124
125	/* Functions in the namespace itself */
126	char *stest1(string_class *s) {
127	    return s->c_str();
128	}
129	double ctest1(test_complex *c) {
130	    return c->real();
131	}
132    }
133
134    namespace test2 {
135	using test::string_class;
136	using test::test_complex;
137
138	/* Functions in another namespace */
139	char *stest2(string_class *s) {
140	    return s->c_str();
141	}
142	double ctest2(test_complex *c) {
143	    return c->real();
144	}
145    }
146
147    namespace test3 {
148	using namespace test;
149
150	char *stest3(string_class *s) {
151	    return s->c_str();
152	}
153	double ctest3(test_complex *c) {
154	    return c->real();
155	}
156    }
157    
158    namespace test4 {
159	using namespace test2;
160
161	char *stest4(string_class *s) {
162	    return s->c_str();
163	}
164	double ctest4(test_complex *c) {
165	    return c->real();
166	}
167    }
168
169    namespace test5 {
170	using namespace test3;
171
172	char *stest5(string_class *s) {
173	    return s->c_str();
174	}
175	double ctest5(test_complex *c) {
176	    return c->real();
177	}
178    }
179
180    char *stest6(test::string_class *s) {
181	return s->c_str();
182    }
183    double ctest6(test::test_complex *c) {
184	return c->real();
185    }
186
187    char *stest7(test2::string_class *s) {
188	return s->c_str();
189    }
190    double ctest7(test2::test_complex *c) {
191	return c->real();
192    }
193
194    char *stest8(test3::string_class *s) {
195	return s->c_str();
196    }
197    double ctest8(test3::test_complex *c) {
198	return c->real();
199    }
200
201    char *stest9(test4::string_class *s) {
202	return s->c_str();
203    }
204    double ctest9(test4::test_complex *c) {
205	return c->real();
206    }
207
208    char *stest10(test5::string_class *s) {
209	return s->c_str();
210    }
211    double ctest10(test5::test_complex *c) {
212	return c->real();
213    }
214
215    namespace test11 = test;
216    
217    char *stest11(test11::string_class *s) {
218	return s->c_str();
219    }
220    double ctest11(test11::test_complex *c) {
221	return c->real();
222    }
223
224    using namespace test2;
225    using test::test_complex;
226
227    char *stest12(string_class *s) {
228	return s->c_str();
229    }
230    double ctest12(test_complex *c) {
231	return c->real();
232    }
233%}
234
235namespace Split {
236#ifdef SWIGPYTHON
237    %typemap(in) PosInteger {
238	$1 = PyInt_AsLong($input);
239	if ($1 < 0) {
240	    PyErr_SetString(PyExc_ValueError,"domain error\n");
241	    return NULL;
242	}
243    }	
244#endif
245#ifdef SWIGOCTAVE
246    %typemap(in) PosInteger {
247	$1 = $input.long_value();
248	if ($1 < 0) {
249	  error("domain error");
250	}
251    }	
252#endif
253#ifdef SWIGRUBY
254    %typemap(in) PosInteger {
255	$1 = NUM2INT($input);
256	if ($1 < 0) {
257	    rb_raise(rb_eRangeError, "domain error");
258	}
259    }	
260#endif
261#ifdef SWIGGO
262    %typemap(in) PosInteger {
263	$1 = $input;
264	if ($1 < 0) {
265	    _swig_gopanic("domain error");
266	}
267    }
268#endif
269}
270    
271%inline %{
272    namespace Split {
273	typedef int PosInteger;
274	PosInteger ttest1(PosInteger x) {
275	    return x;
276	}
277    }
278%}
279
280
281
282
283
284