1 // Massive primitive datatype test.
2 %module(directors="1") primitive_types
6 #pragma warning(disable: 4290) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
10 // Ruby constant names
11 #pragma SWIG nowarn=SWIGWARN_RUBY_WRONG_NAME
13 // Using thread unsafe wrapping
14 #pragma SWIG nowarn=SWIGWARN_TYPEMAP_THREAD_UNSAFE,SWIGWARN_TYPEMAP_DIRECTOROUT_PTR
17 if your language has problems with MyInt* and/or Hello*,
18 you need to change the constant reference typemaps from something
21 %typemap(in) const char & (char temp),
22 const signed char & (signed char temp),
23 const unsigned char & (unsigned char temp),
24 const short & (short temp),
25 const unsigned short & (unsigned short temp),
26 const int & (int temp),
27 const unsigned int & (unsigned int temp),
28 const long & (long temp),
29 const unsigned long & (unsigned long temp),
30 const long long & ($*1_ltype temp),
31 const float & (float temp),
32 const double & (double temp)
33 %{ temp = ($*1_ltype)$input; $1 = &temp; %}
37 %typemap(in) const char & ($basetype temp),
38 const signed char & ($basetype temp),
39 const unsigned char & ($basetype temp),
40 const short & ($basetype temp),
41 const unsigned short & ($basetype temp),
42 const int & ($basetype temp),
43 const unsigned int & ($basetype temp),
44 const long & ($basetype temp),
45 const unsigned long & ($basetype temp),
46 const long long & ($basetype temp),
47 const float & ($basetype temp),
48 const double & ($basetype temp)
49 %{ temp = ($basetype)$input; $1 = &temp; %}
51 the other tipical change is to add the enum SWIGTYPE to the
52 integer throws typemaps:
61 Tcl_SetObjResult(interp, Tcl_NewIntObj((long) $1));
65 or just add the %apply directive after all the typemaps declaration
67 %apply int { enum SWIGTYPE };
70 Also note that this test should not only compile, if you run the
73 grep 'resultobj = SWIG_NewPointerObj' primitive_types_wrap.cxx
75 you should get only two calls:
77 resultobj = SWIG_NewPointerObj((void *) result, SWIGTYPE_p_Test, 1);
78 resultobj = SWIG_NewPointerObj((void *) result, SWIGTYPE_p_TestDirector, 1);
80 if you get a lot more, some typemap could be not defined.
84 grep SWIG_ConvertPtr primitive_types_wrap.cxx| egrep -v 'Test'
88 #define SWIG_ConvertPtr(obj, pp, type, flags)
93 // Try your language module with and without
94 // these nowarn flags.
96 %warnfilter(SWIGWARN_TYPEMAP_CHARLEAK);
104 %feature("director") TestDirector;
107 // Integer class, only visible in C++
113 MyInt(int v = 0): val(v) {
116 operator int() const { return val; }
119 // Template primitive type, only visible in C++
126 Param(T v = 0): val(v) {
134 operator T() const { return val; }
137 typedef char namet[5];
138 extern namet gbl_namet;
145 // adding applies for incomplete swig type MyInt
147 %apply int { MyInt };
148 %apply const int& { const MyInt& };
149 %apply int { Param<int> };
150 %apply char { Param<char> };
151 %apply float { Param<float> };
152 %apply double { Param<double> };
153 %apply const int& { const Param<int>& };
154 %apply const char& { const Param<char>& };
155 %apply const float& { const Param<float>& };
156 %apply const double& { const Param<double>& };
161 // These applies shouldn't be needed ....!!
163 //%apply const int& { const Hello& };
165 %apply void* { pint };
166 %apply void* const& { const pint& };
171 %apply char FIXSIZE[ANY] {char fixsize[8]};
180 typedef char namet[5];
182 typedef const char* pcharc;
185 char* const def_pchar = (char *const)"hello";
186 const char* const def_pcharc = "hija";
188 const namet def_namet = {'h','o',0, 'l','a'};
190 extern namet gbl_namet;
192 char fixsize[8] = {'h','o',0, 'l','a', 0, 0, 0};
196 /* all the primitive types */
213 #define def_hello Hola
217 #define def_paramc 'c'
219 /* types that can be declared as static const class members */
220 %define %test_prim_types_stc(macro, pfx)
221 macro(bool, pfx, bool)
222 macro(signed char, pfx, schar)
223 macro(unsigned char, pfx, uchar)
225 macro(unsigned int, pfx, uint)
226 macro(short, pfx, short)
227 macro(unsigned short, pfx, ushort)
228 macro(long, pfx, long)
229 macro(unsigned long, pfx, ulong)
230 macro(long long, pfx, llong)
231 macro(unsigned long long, pfx, ullong)
232 macro(char, pfx, char)
236 /* types that can be used to test overloading */
237 %define %test_prim_types_ovr(macro, pfx)
238 %test_prim_types_stc(macro, pfx)
239 macro(pchar, pfx, pchar)
243 %define %test_prim_types(macro, pfx)
244 %test_prim_types_ovr(macro, pfx)
245 macro(pcharc, pfx, pcharc)
246 macro(pint, pfx, pint)
247 /* these ones should behave like primitive types too */
248 macro(Hello, pfx, hello)
249 macro(MyInt, pfx, myint)
250 macro(Param<int>, pfx, parami)
251 macro(Param<double>, pfx, paramd)
252 macro(Param<char>, pfx, paramc)
253 macro(size_t, pfx, sizet)
257 /* function passing by value */
258 %define val_decl(type, pfx, name)
259 type pfx##_##name(type x) throw (type) { return x; }
261 /* function passing by ref */
262 %define ref_decl(type, pfx, name)
263 const type& pfx##_##name(const type& x) throw (type) { return x; }
266 /* C++ constant declaration */
267 %define cct_decl(type, pfx, name)
268 const type pfx##_##name = def##_##name;
271 /* C++ static constant declaration */
272 %define stc_decl(type, pfx, name)
273 static const type pfx##_##name = def##_##name;
276 /* Swig constant declaration */
277 %define sct_decl(type, pfx, name)
278 %constant type pfx##_##name = def##_##name;
281 /* variable delaration */
282 %define var_decl(type, pfx, name)
286 /* virtual function passing by value */
287 %define vval_decl(type, pfx, name)
288 virtual val_decl(type, pfx, name)
290 /* virtual function passing by ref */
291 %define vref_decl(type, pfx, name)
292 virtual ref_decl(type, pfx, name)
296 %test_prim_types(sct_decl, sct)
299 %test_prim_types(val_decl, val)
300 %test_prim_types(ref_decl, ref)
301 %test_prim_types(cct_decl, cct)
302 %test_prim_types(var_decl, var)
304 var_decl(namet, var, namet)
316 /* check variables */
317 %define var_check(type, pfx, name)
318 if (pfx##_##name != def_##name) {
319 std::ostringstream a; std::ostringstream b;
322 if (a.str() != b.str()) {
323 std::cout << "failing in pfx""_""name : "
324 << a.str() << " : " << b.str() << std::endl;
330 /* check a function call */
331 %define call_check(type, pfx, name)
332 type pfx##_##tmp##name = def_##name;
333 if (pfx##_##name(pfx##_##tmp##name) != def_##name) {
334 std::ostringstream a; std::ostringstream b;
335 a << pfx##_##name(pfx##_##tmp##name);
337 if (a.str() != b.str()) {
338 std::cout << "failing in pfx""_""name : "
339 << a.str() << " : " << b.str() << std::endl;
345 %define wrp_decl(type, pfx, name)
346 type wrp##_##pfx##_##name(type x) {
347 return pfx##_##name(x);
351 /* function passing by value */
352 %define ovr_decl(type, pfx, name)
353 virtual int pfx##_##val(type x) { return 1; }
354 virtual int pfx##_##ref(const type& x) { return 1; }
358 %apply (char *STRING, int LENGTH) { (const char *str, size_t len) }
378 const Foo copy_c(const Foo x)
383 const Foo& copy_r(const Foo& x)
400 typedef Foo* foo_ptr;
402 foo_ptr fptr_val(foo_ptr a) {
406 const foo_ptr& fptr_ref(const foo_ptr& a) {
414 : var_pchar(0), var_pcharc(0), var_pint(0)
422 %test_prim_types_stc(stc_decl, stc)
423 %test_prim_types(var_decl, var)
424 var_decl(namet, var, namet)
427 const char* val_namet(namet x) throw(namet)
432 const char* val_cnamet(const namet x) throw(namet)
438 /* I have no idea how to define a typemap for
439 const namet&, where namet is a char[ANY] array */
440 const namet& ref_namet(const namet& x) throw(namet)
447 %test_prim_types(val_decl, val)
448 %test_prim_types(ref_decl, ref)
452 %test_prim_types(call_check, val)
453 %test_prim_types(call_check, ref)
459 %test_prim_types_stc(var_check, stc)
460 %test_prim_types(var_check, var)
461 var_check(namet, var, namet);
465 %test_prim_types_ovr(ovr_decl, ovr)
467 size_t strlen(const char *str, size_t len)
472 static const double stc_double;
473 static const double stc_float;
481 : var_pchar(0), var_pcharc(0), var_pint(0)
486 virtual ~TestDirector()
491 virtual const char* vval_namet(namet x) throw(namet)
496 virtual const char* vval_cnamet(const namet x) throw(namet)
502 /* I have no idea how to define a typemap for
503 const namet&, where namet is a char[ANY] array */
504 virtual const namet& vref_namet(const namet& x) throw(namet)
511 %test_prim_types_stc(stc_decl, stc)
512 %test_prim_types(var_decl, var)
513 var_decl(namet, var, namet)
515 %test_prim_types(val_decl, val)
516 %test_prim_types(ref_decl, ref)
518 %test_prim_types(vval_decl, vval)
519 %test_prim_types(vref_decl, vref)
521 %test_prim_types(wrp_decl, vref)
522 %test_prim_types(wrp_decl, vval)
526 %test_prim_types(call_check, vval)
527 %test_prim_types(call_check, vref)
533 %test_prim_types_stc(var_check, stc)
534 %test_prim_types(var_check, var)
538 %test_prim_types_ovr(ovr_decl, ovr)
541 virtual Test* vtest(Test* t) const throw (Test)
550 %test_prim_types(var_check, cct)
551 %test_prim_types(var_check, var)
552 var_check(namet, var, namet);
560 const char* char_foo(float f, const char *s) {
564 int char_foo(double d, int i) {
571 const double Test::stc_double = 1;
572 const double Test::stc_float = 1;
580 typedef const unsigned int cuint;
585 using namespace DCTypes;
586 unsigned int SetPos(cuint& x, cuint& y) {return x + y;}
589 double val_double_2(double x, const double& y = 3.0) {
593 double val_double(double x) {
597 float val_float_2(float x, const float& y = 3.0) {
601 // Regression test for bug1699646 - we weren't handling
602 // + or - after e for float constants.
603 float regression_test_for_bug1699646(float f = 1e-02f) {
607 float val_float(float x) {
613 %apply SWIGTYPE* { char *};
616 %array_functions(char,pchar);