import source from 1.3.40
[external/swig.git] / Examples / test-suite / primitive_types.i
1 // Massive primitive datatype test.
2 %module(directors="1") primitive_types
3
4 %{
5 #if defined(_MSC_VER)
6   #pragma warning(disable: 4290) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
7 #endif
8 %}
9
10 // Ruby constant names
11 #pragma SWIG nowarn=SWIGWARN_RUBY_WRONG_NAME
12
13 // Using thread unsafe wrapping
14 #pragma SWIG nowarn=SWIGWARN_TYPEMAP_THREAD_UNSAFE,SWIGWARN_TYPEMAP_DIRECTOROUT_PTR
15  /*
16
17  if your language has problems with MyInt* and/or Hello*,
18  you need to change the constant reference typemaps from something
19  like:
20
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; %}
34
35   to the following:
36
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; %}
50
51   the other tipical change is to add the enum SWIGTYPE to the
52   integer throws typemaps:
53
54   %typemap(throws) int, 
55                   long, 
56                   short, 
57                   unsigned int, 
58                   unsigned long, 
59                   unsigned short,
60                   enum SWIGTYPE {
61     Tcl_SetObjResult(interp, Tcl_NewIntObj((long) $1));
62     SWIG_fail;
63   }
64
65   or just add the %apply directive after all the typemaps declaration
66
67   %apply int { enum SWIGTYPE };
68
69
70   Also note that this test should not only compile, if you run the
71   program
72
73      grep 'resultobj = SWIG_NewPointerObj' primitive_types_wrap.cxx 
74  
75   you should get only two calls:
76
77     resultobj = SWIG_NewPointerObj((void *) result, SWIGTYPE_p_Test, 1);
78     resultobj = SWIG_NewPointerObj((void *) result, SWIGTYPE_p_TestDirector, 1);
79
80   if you get a lot more, some typemap could be not defined.
81
82   The same with
83
84      grep SWIG_ConvertPtr primitive_types_wrap.cxx| egrep -v 'Test'
85
86   you should only get
87
88     #define SWIG_ConvertPtr(obj, pp, type, flags)
89
90  */
91
92 //
93 // Try your language module with and without 
94 // these nowarn flags.
95 //
96 %warnfilter(SWIGWARN_TYPEMAP_CHARLEAK);
97
98 %{
99 #include <stddef.h>
100 #include <iostream>
101 #include <sstream>
102 %}
103
104 %feature("director") TestDirector;
105
106 %{
107   // Integer class, only visible in C++
108   struct MyInt
109   {
110     char name[5];
111     int val;
112
113     MyInt(int v = 0): val(v) {
114     }
115     
116     operator int() const { return val; }
117   };
118
119   // Template primitive type, only visible in C++
120   template <class T>
121   struct Param
122   {
123     char name[5];
124     T val;
125
126     Param(T v = 0): val(v) {
127       name[0] = 0;
128       name[1] = 0;
129       name[2] = 0;
130       name[3] = 0;
131       name[4] = 0;
132     }
133     
134     operator T() const { return val; }
135   };
136
137   typedef char namet[5];
138   extern namet gbl_namet;
139   namet gbl_namet;
140
141 %}
142
143
144 //
145 // adding applies for incomplete swig type MyInt
146 //
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>& };
157
158
159
160 //
161 // These applies shouldn't be needed ....!!
162 //
163 //%apply const int& { const Hello&  };
164
165 %apply void* { pint };
166 %apply void* const& { const pint& };
167   
168
169 //
170 // Some simple types
171 %apply char FIXSIZE[ANY] {char fixsize[8]};
172
173   
174                     
175 %inline %{
176   enum Hello {
177     Hi, Hola
178   };
179
180   typedef char namet[5];
181   typedef char* pchar;
182   typedef const char* pcharc;
183   typedef char* pint;
184
185   char* const def_pchar = (char *const)"hello";
186   const char* const def_pcharc = "hija";
187
188   const namet def_namet = {'h','o',0, 'l','a'};
189
190   extern namet gbl_namet;
191
192   char fixsize[8] =  {'h','o',0, 'l','a', 0, 0, 0};
193 %}
194
195
196 /* all the primitive types */
197 #define def_bool 1
198 #define def_schar 1
199 #define def_uchar 1
200 #define def_int 1
201 #define def_uint 1
202 #define def_short 1
203 #define def_ushort 1
204 #define def_long 1
205 #define def_ulong 1
206 #define def_llong 1
207 #define def_ullong 1
208 #define def_float 1
209 #define def_double 1
210 #define def_char 'H'
211 #define def_pint  0
212 #define def_sizet 1
213 #define def_hello Hola
214 #define def_myint 1
215 #define def_parami 1
216 #define def_paramd 1
217 #define def_paramc 'c'
218
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)
224 macro(int,                pfx, int)
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)
233 %enddef
234
235
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)
240 %enddef
241
242 /* all the types */
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)
254 %enddef
255
256
257 /* function passing by value */
258 %define val_decl(type, pfx, name)
259   type pfx##_##name(type x) throw (type) { return x; }
260 %enddef
261 /* function passing by ref */
262 %define ref_decl(type, pfx, name)
263   const type& pfx##_##name(const type& x) throw (type) { return x; }
264 %enddef
265
266 /* C++ constant declaration */
267 %define cct_decl(type, pfx, name)
268   const type pfx##_##name = def##_##name;
269 %enddef
270
271 /* C++ static constant declaration */
272 %define stc_decl(type, pfx, name)
273   static const type pfx##_##name = def##_##name;
274 %enddef
275
276 /* Swig constant declaration */
277 %define sct_decl(type, pfx, name)
278   %constant type pfx##_##name = def##_##name;
279 %enddef
280
281 /* variable delaration */
282 %define var_decl(type, pfx, name)
283   type pfx##_##name;
284 %enddef
285
286 /* virtual function passing by value */
287 %define vval_decl(type, pfx, name)
288   virtual val_decl(type, pfx, name)
289 %enddef
290 /* virtual function passing by ref */
291 %define vref_decl(type, pfx, name)
292   virtual ref_decl(type, pfx, name)
293 %enddef
294
295
296 %test_prim_types(sct_decl, sct)
297
298 %inline {
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)
303
304   var_decl(namet, var, namet)
305
306   void var_init() 
307   {
308     var_pchar = 0;
309     var_pcharc = 0;
310     var_pint = 0;
311     var_namet[0] = 'h';
312   }
313   
314 }
315
316 /* check variables */
317 %define var_check(type, pfx, name)
318   if (pfx##_##name != def_##name) {
319     std::ostringstream a; std::ostringstream b;
320     a << pfx##_##name;
321     b << def_##name;
322     if (a.str() != b.str()) {
323       std::cout << "failing in pfx""_""name : "
324                 << a.str() << " : " << b.str() << std::endl;
325       //      return 0;
326     }
327   }
328 %enddef
329
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);
336     b << def_##name;
337     if (a.str() != b.str()) {
338       std::cout << "failing in pfx""_""name : "
339                 << a.str() << " : " << b.str() << std::endl;
340       // return 0;
341     }
342   }
343 %enddef
344
345 %define wrp_decl(type, pfx, name)
346   type wrp##_##pfx##_##name(type x) { 
347     return pfx##_##name(x); 
348   }
349 %enddef
350
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; }
355 %enddef
356
357
358 %apply (char *STRING, int LENGTH) { (const char *str, size_t len) }
359
360 %inline {
361   struct Foo
362   {
363     int _a;
364     
365     Foo (int a) : _a(a)
366     {
367     }
368     
369     Foo(const Foo&)
370     {
371     }
372
373     Foo copy(Foo x) 
374     {
375       return x;
376     }
377
378     const Foo copy_c(const Foo x) 
379     {
380       return x;
381     }
382
383     const Foo& copy_r(const Foo& x) 
384     {
385       return x;
386     }
387
388     Foo* this_p() 
389     {
390       return this;
391     }
392
393     Foo& this_r() 
394     {
395       return *this;
396     }
397     
398   };
399   
400   typedef Foo* foo_ptr;
401   
402   foo_ptr fptr_val(foo_ptr a) {
403     return a;
404   }
405
406   const foo_ptr& fptr_ref(const foo_ptr& a) {
407     return a;
408   }
409   
410   
411  struct Test 
412  {
413    Test()
414      : var_pchar(0), var_pcharc(0), var_pint(0)
415    {
416    }
417
418    virtual ~Test()
419    {
420    }
421    
422    %test_prim_types_stc(stc_decl, stc)
423    %test_prim_types(var_decl, var)
424    var_decl(namet, var, namet)
425
426
427    const char* val_namet(namet x) throw(namet)
428    {
429      return x;
430    }
431
432    const char* val_cnamet(const namet x) throw(namet)
433    {
434      return x;
435    }
436
437 #if 0
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)
441    {
442      return x;
443    }
444 #endif
445
446    
447    %test_prim_types(val_decl, val)
448    %test_prim_types(ref_decl, ref)
449
450    int c_check() 
451    {
452      %test_prim_types(call_check, val)
453      %test_prim_types(call_check, ref)
454      return 1;
455    }
456
457    int v_check() 
458    {
459      %test_prim_types_stc(var_check, stc)
460      %test_prim_types(var_check, var)
461      var_check(namet, var, namet);
462      return 1;
463    }
464
465    %test_prim_types_ovr(ovr_decl, ovr)
466
467    size_t strlen(const char *str, size_t len)
468    {
469      return len;
470    }
471
472    static const double stc_double;
473    static const double stc_float;
474
475    
476  };
477
478  struct TestDirector
479  {
480    TestDirector()
481      : var_pchar(0), var_pcharc(0), var_pint(0)
482    {
483    }
484
485    
486    virtual ~TestDirector()
487    {
488      var_namet[0]='h';
489    }
490
491    virtual const char* vval_namet(namet x) throw(namet)
492    {
493      return x;
494    }
495
496    virtual const char* vval_cnamet(const namet x) throw(namet)
497    {
498      return x;
499    }
500
501 #if 0
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)
505    {
506      return x;
507    }
508 #endif
509
510
511    %test_prim_types_stc(stc_decl, stc)
512    %test_prim_types(var_decl, var)
513    var_decl(namet, var, namet)
514
515    %test_prim_types(val_decl, val)
516    %test_prim_types(ref_decl, ref)
517
518    %test_prim_types(vval_decl, vval)
519    %test_prim_types(vref_decl, vref)
520
521    %test_prim_types(wrp_decl, vref)
522    %test_prim_types(wrp_decl, vval)
523
524    int c_check() 
525    {
526      %test_prim_types(call_check, vval)
527      %test_prim_types(call_check, vref)
528      return 1;
529    }
530
531    int v_check() 
532    {
533      %test_prim_types_stc(var_check, stc)
534      %test_prim_types(var_check, var)
535      return 1;
536    }
537
538    %test_prim_types_ovr(ovr_decl, ovr)
539    
540
541    virtual Test* vtest(Test* t) const throw (Test)
542    {
543      return t;
544    }
545    
546  }; 
547
548  int v_check() 
549  {
550    %test_prim_types(var_check, cct)
551    %test_prim_types(var_check, var)
552    var_check(namet, var, namet);
553    return 1;
554  }
555
556 }
557
558 %inline %{
559
560   const char* char_foo(float f, const char *s) {
561     return s;
562   }
563   
564   int char_foo(double d, int i) {
565     return i;
566   }
567   
568 %}
569
570 %{
571   const double Test::stc_double = 1;
572   const double Test::stc_float = 1;
573 %}
574
575
576 %inline
577 %{
578     namespace DCTypes
579     {
580         typedef const unsigned int cuint;
581     }
582
583     namespace DCSystem
584     {
585         using namespace DCTypes;
586         unsigned int SetPos(cuint& x, cuint& y) {return x + y;}
587     }
588
589     double val_double_2(double x, const double& y = 3.0) {
590       return x + y;
591     } 
592
593     double val_double(double x) {
594       return x;
595     } 
596
597     float val_float_2(float x, const float& y = 3.0) {
598       return x + y;
599     } 
600
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) {
604       return f;
605     } 
606
607     float val_float(float x) {
608       return x;
609     } 
610 %}
611
612
613 %apply SWIGTYPE* { char *};
614   
615 %include "carrays.i"
616 %array_functions(char,pchar);
617