PageRenderTime 22ms CodeModel.GetById 17ms app.highlight 3ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/rel-1-3-26/SWIG/Lib/swig.swg

#
Unknown | 477 lines | 398 code | 79 blank | 0 comment | 0 complexity | 04685815e24dd213e3c64e0a72674b22 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1/* -----------------------------------------------------------------------------
  2 * swig.swg
  3 *
  4 * $Header$
  5 *
  6 * Common macro definitions for various SWIG directives.  This file is always 
  7 * included at the top of each input file.
  8 * ----------------------------------------------------------------------------- */
  9
 10/* Deprecated SWIG directives */
 11
 12#define %disabledoc     %warn "104:%disabledoc is deprecated"
 13#define %enabledoc      %warn "105:%enabledoc is deprecated"
 14#define %doconly        %warn "106:%doconly is deprecated"
 15#define %style          %warn "107:%style is deprecated" /##/
 16#define %localstyle     %warn "108:%localstyle is deprecated" /##/
 17#define %title          %warn "109:%title is deprecated" /##/
 18#define %section        %warn "110:%section is deprecated" /##/
 19#define %subsection     %warn "111:%subsection is deprecated" /##/
 20#define %subsubsection  %warn "112:%subsubsection is deprecated" /##/
 21#define %new            %warn "117:%new is deprecated. Use %newobject"
 22#define %text           %insert("null")
 23
 24/* Code insertion directives such as %wrapper %{ ... %} */
 25
 26#define %init        %insert("init")
 27#define %wrapper     %insert("wrapper")
 28#define %header      %insert("header")
 29#define %runtime     %insert("runtime")
 30
 31/* Class extension */
 32
 33#define %addmethods  %warn "113:%addmethods is now %extend" %extend
 34
 35/* %ignore directive */
 36
 37#define %ignore         %rename($ignore)
 38#define %ignorewarn(x)  %rename("$ignore:" x)
 39
 40/* Access control directives */
 41
 42#define %readonly    %warn "114:%readonly is deprecated. Use %immutable; " %feature("immutable");
 43#define %readwrite   %warn "115:%readwrite is deprecated. Use %mutable; " %feature("immutable","");
 44
 45#define %immutable       %feature("immutable")
 46#define %noimmutable     %feature("immutable","0")
 47#define %clearimmutable  %feature("immutable","")
 48#define %mutable         %noimmutable
 49
 50/* Generation of default constructors/destructors */
 51
 52#define %nodefault       %feature("nodefault","1")
 53#define %default         %feature("nodefault","0")
 54#define %clearnodefault  %feature("nodefault","")
 55#define %makedefault     %default
 56
 57/* the %exception directive */
 58
 59#ifdef SWIGCSHARP
 60#define %exception      %feature("except", canthrow=1)
 61#else
 62#define %exception      %feature("except")
 63#endif
 64#define %noexception    %feature("except","0")
 65#define %clearexception %feature("except","")
 66
 67/* the %exceptionclass directive */
 68
 69#define %exceptionclass      %feature("exceptionclass")
 70#define %noexceptionclass    %feature("exceptionclass","0")
 71#define %clearexceptionclass %feature("exceptionclass","")
 72
 73/* the %newobject directive */
 74
 75#define %newobject        %feature("new")
 76#define %nonewobject      %feature("new","0")
 77#define %clearnewobject   %feature("new","")
 78
 79/* the %refobject/%unrefobject directives */
 80
 81#define %refobject         %feature("ref")
 82#define %norefobject       %feature("ref","0")
 83#define %clearrefobject    %feature("ref","")
 84
 85#define %unrefobject       %feature("unref")
 86#define %nounrefobject     %feature("unref","0")
 87#define %clearunrefobject  %feature("unref","")
 88
 89/* Directives for callback functions (experimental) */
 90
 91#define %callback(x)    %feature("callback",`x`)
 92#define %nocallback     %feature("callback","0")
 93#define %clearcallback  %feature("callback","")
 94
 95
 96/* Common features */
 97
 98/* Warnings */
 99#define %warnfilter(...) %feature("warnfilter",`__VA_ARGS__`)
100
101/* Warnings used in typemaps. Macro names are the same as those in swigwarn.h but prefixed by SWIG_. */
102%define SWIG_WARN_TYPEMAP_THREAD_UNSAFE "470:Thread/reentrant unsafe wrapping, consider returning by value instead." %enddef
103
104/* Contract support - Experimental and undocumented */
105
106#define %contract    %feature("contract")
107
108/* Default handling of certain overloaded operators */
109
110#ifdef __cplusplus
111%ignorewarn("350:operator new ignored")     operator new;
112%ignorewarn("351:operator delete ignored")  operator delete;
113%ignorewarn("394:operator new[] ignored")   operator new[];
114%ignorewarn("395:operator delete[] ignored") operator delete[];
115
116/* Smart pointer handling */
117
118%rename(__deref__) *::operator->;
119%rename(__ref__) *::operator*();
120%rename(__ref__) *::operator*() const;
121
122/* Define std namespace */
123namespace std {
124}
125#endif
126
127/* Set up the typemap for handling new return strings */
128
129#ifdef __cplusplus
130%typemap(newfree) char * "delete [] $1;";
131#else
132%typemap(newfree) char * "free($1);";
133#endif
134
135/* Default typemap for handling char * members */
136
137#ifdef __cplusplus
138%typemap(memberin) char * {
139  if ($1) delete [] $1;
140  if ($input) {
141     $1 = ($1_type) (new char[strlen($input)+1]);
142     strcpy((char *) $1,$input);
143  } else {
144     $1 = 0;
145  }
146}
147%typemap(memberin,warning="451:Setting const char * member may leak memory.") const char * {
148  if ($input) {
149     $1 = ($1_type) (new char[strlen($input)+1]);
150     strcpy((char *) $1,$input);
151  } else {
152     $1 = 0;
153  }
154}
155%typemap(globalin) char * {
156  if ($1) delete [] $1;
157  if ($input) {
158     $1 = ($1_type) (new char[strlen($input)+1]);
159     strcpy((char *) $1,$input);
160  } else {
161     $1 = 0;
162  }
163}
164%typemap(globalin,warning="451:Setting const char * variable may leak memory.") const char * {
165  if ($input) {
166     $1 = ($1_type) (new char[strlen($input)+1]);
167     strcpy((char *) $1,$input);
168  } else {
169     $1 = 0;
170  }
171}
172#else
173%typemap(memberin) char * {
174  if ($1) free((char*)$1);
175  if ($input) {
176     $1 = ($1_type) malloc(strlen($input)+1);
177     strcpy((char*)$1,$input);
178  } else {
179     $1 = 0;
180  }
181}
182%typemap(memberin,warning="451:Setting const char * member may leak memory.") const char * {
183  if ($input) {
184     $1 = ($1_type) malloc(strlen($input)+1);
185     strcpy((char*)$1,$input);
186  } else {
187     $1 = 0;
188  }
189}
190%typemap(globalin) char * {
191  if ($1) free((char*)$1);
192  if ($input) {
193     $1 = ($1_type) malloc(strlen($input)+1);
194     strcpy((char*)$1,$input);
195  } else {
196     $1 = 0;
197  }
198}
199%typemap(globalin,warning="451:Setting const char * variable may leak memory.") const char * {
200  if ($input) {
201     $1 = ($1_type) malloc(strlen($input)+1);
202     strcpy((char*)$1,$input);
203  } else {
204     $1 = 0;
205  }
206}
207
208#endif
209
210/* Character array handling */
211
212%typemap(memberin) char [ANY] {
213  if ($input) strncpy($1,$input,$1_dim0);
214  else $1[0] = 0;
215}
216
217%typemap(globalin) char [ANY] {
218  if ($input) strncpy($1,$input,$1_dim0);
219  else $1[0] = 0;
220}
221
222%typemap(memberin) char [] {
223  if ($input) strcpy($1,$input);
224  else $1[0] = 0;
225}
226
227%typemap(globalin) char [] {
228  if ($input) strcpy($1,$input);
229  else $1[0] = 0;
230}
231
232/* memberin/globalin typemap for arrays. */
233
234%typemap(memberin) SWIGTYPE [ANY] {
235  size_t ii;
236  $1_basetype *b = ($1_basetype *) $1;
237  for (ii = 0; ii < (size_t)$1_size; ii++) b[ii] = *(($1_basetype *) $input + ii);
238}
239
240%typemap(globalin) SWIGTYPE [ANY] {
241  size_t ii;
242  $1_basetype *b = ($1_basetype *) $1;
243  for (ii = 0; ii < (size_t)$1_size; ii++) b[ii] = *(($1_basetype *) $input + ii);
244}
245
246/* memberin/globalin typemap for double arrays. */
247
248%typemap(memberin) SWIGTYPE [ANY][ANY] {
249  $basetype (*inp)[$dim1] = ($basetype (*)[$dim1])($input);
250  $basetype (*dest)[$dim1] = ($basetype (*)[$dim1])($1);
251  size_t ii = 0;
252  for (; ii < $dim0; ++ii) {
253    $basetype *ip = inp[ii];
254    $basetype *dp = dest[ii];
255    size_t jj = 0;
256    for (; jj < $dim1; ++jj) dp[jj] = ip[jj];
257  }
258}
259
260%typemap(globalin) SWIGTYPE [ANY][ANY] {
261  $basetype (*inp)[$dim1] = ($basetype (*)[$dim1])($input);
262  $basetype (*dest)[$dim1] = ($basetype (*)[$dim1])($1);
263  size_t ii = 0;
264  for (; ii < $dim0; ++ii) {
265    $basetype *ip = inp[ii];
266    $basetype *dp = dest[ii];
267    size_t jj = 0;
268    for (; jj < $dim1; ++jj) dp[jj] = ip[jj];
269  }
270}
271
272/* Typemap for variable length arguments sentinel value.  Used
273   by the %varargs directive. */
274
275%typemap(in,numinputs=0) SWIGTYPE *VARARGS_SENTINEL, SWIGTYPE VARARGS_SENTINEL "";
276
277
278/*
279 * Function/method overloading support.   This is done through typemaps,
280 * but also involve a precedence level. 
281 */
282
283/* Macro for overload resolution */
284
285#define %typecheck(_x) %typemap(typecheck, precedence=_x)
286
287/* Macros for precedence levels */
288
289%define SWIG_TYPECHECK_POINTER       0     %enddef
290%define SWIG_TYPECHECK_VOIDPTR       10    %enddef
291%define SWIG_TYPECHECK_BOOL          15    %enddef
292%define SWIG_TYPECHECK_UINT8         20    %enddef
293%define SWIG_TYPECHECK_INT8          25    %enddef
294%define SWIG_TYPECHECK_UINT16        30    %enddef
295%define SWIG_TYPECHECK_INT16         35    %enddef
296%define SWIG_TYPECHECK_UINT32        40    %enddef
297%define SWIG_TYPECHECK_INT32         45    %enddef
298%define SWIG_TYPECHECK_UINT64        50    %enddef
299%define SWIG_TYPECHECK_INT64         55    %enddef
300%define SWIG_TYPECHECK_UINT128       60    %enddef
301%define SWIG_TYPECHECK_INT128        65    %enddef
302%define SWIG_TYPECHECK_INTEGER       70    %enddef
303%define SWIG_TYPECHECK_FLOAT         80    %enddef
304%define SWIG_TYPECHECK_DOUBLE        90    %enddef
305%define SWIG_TYPECHECK_CPLXFLT       95    %enddef
306%define SWIG_TYPECHECK_CPLXDBL      100    %enddef
307%define SWIG_TYPECHECK_COMPLEX      105    %enddef
308%define SWIG_TYPECHECK_UNICHAR      110    %enddef
309%define SWIG_TYPECHECK_UNISTRING    120    %enddef
310%define SWIG_TYPECHECK_CHAR         130    %enddef
311%define SWIG_TYPECHECK_STRING       140    %enddef
312%define SWIG_TYPECHECK_PAIR         150    %enddef
313%define SWIG_TYPECHECK_VECTOR       160    %enddef
314%define SWIG_TYPECHECK_DEQUE        170    %enddef
315%define SWIG_TYPECHECK_LIST         180    %enddef
316%define SWIG_TYPECHECK_SET          190    %enddef
317%define SWIG_TYPECHECK_MULTISET     200    %enddef
318%define SWIG_TYPECHECK_MAP          210    %enddef
319%define SWIG_TYPECHECK_MULTIMAP     220    %enddef
320
321%define SWIG_TYPECHECK_BOOL_ARRAY        1015    %enddef
322%define SWIG_TYPECHECK_INT8_ARRAY        1025    %enddef
323%define SWIG_TYPECHECK_INT16_ARRAY       1035    %enddef
324%define SWIG_TYPECHECK_INT32_ARRAY       1045    %enddef
325%define SWIG_TYPECHECK_INT64_ARRAY       1055    %enddef
326%define SWIG_TYPECHECK_INT128_ARRAY      1065    %enddef
327%define SWIG_TYPECHECK_FLOAT_ARRAY       1080    %enddef
328%define SWIG_TYPECHECK_DOUBLE_ARRAY      1090    %enddef
329%define SWIG_TYPECHECK_CHAR_ARRAY        1130    %enddef
330%define SWIG_TYPECHECK_STRING_ARRAY      1140    %enddef
331%define SWIG_TYPECHECK_OBJECT_ARRAY      1150    %enddef
332
333%define SWIG_TYPECHECK_BOOL_PTR          2015    %enddef
334%define SWIG_TYPECHECK_UINT8_PTR         2020    %enddef
335%define SWIG_TYPECHECK_INT8_PTR          2025    %enddef
336%define SWIG_TYPECHECK_UINT16_PTR        2030    %enddef
337%define SWIG_TYPECHECK_INT16_PTR         2035    %enddef
338%define SWIG_TYPECHECK_UINT32_PTR        2040    %enddef
339%define SWIG_TYPECHECK_INT32_PTR         2045    %enddef
340%define SWIG_TYPECHECK_UINT64_PTR        2050    %enddef
341%define SWIG_TYPECHECK_INT64_PTR         2055    %enddef
342%define SWIG_TYPECHECK_FLOAT_PTR         2080    %enddef
343%define SWIG_TYPECHECK_DOUBLE_PTR        2090    %enddef
344%define SWIG_TYPECHECK_CHAR_PTR          2130    %enddef
345
346/*  
347 * This template wrapper is used to handle C++ objects that are passed or 
348 * returned by value.   This is necessary to handle objects that define
349 * no default-constructor (making it difficult for SWIG to properly declare
350 * local variables).
351 *
352 * The wrapper is used as follows.  First consider a function like this:
353 *
354 *      Vector cross_product(Vector a, Vector b)
355 *
356 * Now, if Vector is defined as a C++ class with no default constructor, 
357 * code is generated as follows:
358 *
359 *     Vector *wrap_cross_product(Vector *inarg1, Vector *inarg2) {
360 *          SwigValueWrapper<Vector>  arg1;
361 *          SwigValueWrapper<Vector>  arg2;
362 *          SwigValueWrapper<Vector> result;
363 *
364 *          arg1 = *inarg1;
365 *          arg2 = *inarg2;
366 *          ...            
367 *          result = cross_product(arg1,arg2);
368 *          ...
369 *          return new Vector(result);
370 *    }
371 *         
372 * In the wrappers, the template SwigValueWrapper simply provides a thin
373 * layer around a Vector *.  However, it does this in a way that allows
374 * the object to be bound after the variable declaration (which is not possible
375 * with the bare object when it lacks a default constructor).  
376 *
377 * An observant reader will notice that the code after the variable declarations
378 * is *identical* to the code used for classes that do define default constructors.
379 * Thus, this neat trick allows us to fix this special case without having to
380 * make massive changes to typemaps and other parts of the SWIG code generator.
381 *
382 * Note: this code is not included when SWIG runs in C-mode, when classes
383 * define default constructors, or when pointers and references are used.
384 * SWIG tries to avoid doing this except in very special circumstances.
385 *
386 * Note: This solution suffers from making a large number of copies
387 * of the underlying object.  However, this is needed in the interest of
388 * safety and in order to cover all of the possible ways in which a value
389 * might be assigned.  For example:
390 *
391 *       arg1 = *inarg1;       // Assignment from a pointer
392 *       arg1 = Vector(1,2,3); // Assignment from a value  
393 *
394 * This wrapping technique was suggested by William Fulton and is henceforth
395 * known as the "Fulton Transform" :-).
396 */
397
398#ifdef __cplusplus
399%insert("runtime") %{
400#ifdef __cplusplus
401template<class T> class SwigValueWrapper {
402    T *tt;
403public:
404    SwigValueWrapper() : tt(0) { }
405    SwigValueWrapper(const SwigValueWrapper<T>& rhs) : tt(new T(*rhs.tt)) { }
406    SwigValueWrapper(const T& t) : tt(new T(t)) { }
407    ~SwigValueWrapper() { delete tt; } 
408    SwigValueWrapper& operator=(const T& t) { delete tt; tt = new T(t); return *this; }
409    operator T&() const { return *tt; }
410    T *operator&() { return tt; }
411private:
412    SwigValueWrapper& operator=(const SwigValueWrapper<T>& rhs);
413};
414#endif
415%}
416#endif
417
418/* Macro for setting a dynamic cast function */
419%define DYNAMIC_CAST(mangle,func)
420%init %{
421   mangle->dcast = (swig_dycast_func) func;
422%}
423%enddef
424
425/*
426
427   This macro performs constant aggregation.  Basically the idea of
428   constant aggregation is that you can group a collection of constants 
429   together.  For example, suppose you have some code like this:
430
431       #define UP  1
432       #define DOWN 2
433       #define LEFT 3
434       #define RIGHT 4
435
436   Now, suppose you had a function like this:
437
438       int move(int direction)
439
440   In this case, you might want to restrict the direction argument to one of the supplied
441   constant names. To do this, you could write some typemap code by hand.  Alternatively,
442   you can use the %aggregate_check macro defined here to create a simple check function
443   for you.  Here is an example:
444
445      %aggregate_check(int, check_direction, UP, DOWN, LEFT, RIGHT);
446
447   Now, using a typemap
448
449       %typemap(check) int direction {
450            if (!check_direction($1)) SWIG_exception(SWIG_ValueError,"Bad direction.");
451       }
452
453   or a contract (better)
454
455       %contract move(int x) {
456       require:
457           check_direction(x);
458       }
459*/
460   
461%define %aggregate_check(TYPE, NAME, FIRST, ...)
462%wrapper %{
463static int NAME(TYPE x) {
464    static  TYPE values[] = { FIRST, ##__VA_ARGS__ };
465    static  int size = sizeof(values);
466    int     i,j;
467    for (i = 0, j = 0; i < size; i+=sizeof(TYPE),j++) {
468        if (x == values[j]) return 1; 
469    }
470    return 0;
471}
472%}
473%enddef
474
475%insert("runtime") "swiglabels.swg"
476
477