PageRenderTime 17ms CodeModel.GetById 16ms app.highlight 0ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/rel-1-3-29/SWIG/Examples/test-suite/namespace_typemap.i

#
Swig | 229 lines | 192 code | 32 blank | 5 comment | 0 complexity | 98f616ac9eff227a7305b246bce83c06 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  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 complex class */
 26   class complex {
 27      double re;
 28      double im;
 29   public:
 30      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::complex * {
 48    if (PyComplex_Check($input)) {
 49	$1 = new complex(PyComplex_RealAsDouble($input),
 50			 PyComplex_ImagAsDouble($input));
 51    } else {
 52	PyErr_SetString(PyExc_TypeError,"Expected complex.\n");
 53	return NULL;
 54    }
 55}
 56%typemap(freearg) test::complex * {
 57    delete $1;
 58}
 59#endif
 60
 61namespace test {
 62    class string_class;
 63#ifdef SWIGPYTHON
 64	%typemap(in) string_class * {
 65	    $1 = new string_class(PyString_AsString($input));
 66	}
 67	%typemap(freearg) string_class * {
 68	    delete $1;
 69	}
 70#endif
 71#ifdef SWIGRUBY
 72	%typemap(in) string_class * {
 73	    $1 = new string_class(STR2CSTR($input));
 74	}
 75	%typemap(freearg) string_class * {
 76	    delete $1;
 77	}
 78#endif
 79}
 80
 81%inline %{
 82    namespace test {
 83	class string_class;
 84	class complex;
 85
 86	/* Functions in the namespace itself */
 87	char *stest1(string_class *s) {
 88	    return s->c_str();
 89	}
 90	double ctest1(complex *c) {
 91	    return c->real();
 92	}
 93    }
 94
 95    namespace test2 {
 96	using test::string_class;
 97	using test::complex;
 98
 99	/* Functions in another namespace */
100	char *stest2(string_class *s) {
101	    return s->c_str();
102	}
103	double ctest2(complex *c) {
104	    return c->real();
105	}
106    }
107
108    namespace test3 {
109	using namespace test;
110
111	char *stest3(string_class *s) {
112	    return s->c_str();
113	}
114	double ctest3(complex *c) {
115	    return c->real();
116	}
117    }
118    
119    namespace test4 {
120	using namespace test2;
121
122	char *stest4(string_class *s) {
123	    return s->c_str();
124	}
125	double ctest4(complex *c) {
126	    return c->real();
127	}
128    }
129
130    namespace test5 {
131	using namespace test3;
132
133	char *stest5(string_class *s) {
134	    return s->c_str();
135	}
136	double ctest5(complex *c) {
137	    return c->real();
138	}
139    }
140
141    char *stest6(test::string_class *s) {
142	return s->c_str();
143    }
144    double ctest6(test::complex *c) {
145	return c->real();
146    }
147
148    char *stest7(test2::string_class *s) {
149	return s->c_str();
150    }
151    double ctest7(test2::complex *c) {
152	return c->real();
153    }
154
155    char *stest8(test3::string_class *s) {
156	return s->c_str();
157    }
158    double ctest8(test3::complex *c) {
159	return c->real();
160    }
161
162    char *stest9(test4::string_class *s) {
163	return s->c_str();
164    }
165    double ctest9(test4::complex *c) {
166	return c->real();
167    }
168
169    char *stest10(test5::string_class *s) {
170	return s->c_str();
171    }
172    double ctest10(test5::complex *c) {
173	return c->real();
174    }
175
176    namespace test11 = test;
177    
178    char *stest11(test11::string_class *s) {
179	return s->c_str();
180    }
181    double ctest11(test11::complex *c) {
182	return c->real();
183    }
184
185    using namespace test2;
186    using test::complex;
187
188    char *stest12(string_class *s) {
189	return s->c_str();
190    }
191    double ctest12(complex *c) {
192	return c->real();
193    }
194%}
195
196namespace Split {
197#ifdef SWIGPYTHON
198    %typemap(in) PosInteger {
199	$1 = PyInt_AsLong($input);
200	if ($1 < 0) {
201	    PyErr_SetString(PyExc_ValueError,"domain error\n");
202	    return NULL;
203	}
204    }	
205#endif
206#ifdef SWIGRUBY
207    %typemap(in) PosInteger {
208	$1 = NUM2INT($input);
209	if ($1 < 0) {
210	    rb_raise(rb_eRangeError, "domain error");
211	}
212    }	
213#endif
214}
215    
216%inline %{
217    namespace Split {
218	typedef int PosInteger;
219	PosInteger ttest1(PosInteger x) {
220	    return x;
221	}
222    }
223%}
224
225
226
227
228
229