/tags/rel-1-3-15/SWIG/Lib/swig.swg
Unknown | 282 lines | 233 code | 49 blank | 0 comment | 0 complexity | b20af478c78adafd6e6595b84fc11cf8 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/* Access control directives */
36
37#define %readonly %warn "114:%readonly is deprecated. Use %immutable; " %feature("immutable");
38#define %readwrite %warn "115:%readwrite is deprecated. Use %mutable; " %feature("immutable","");
39
40#define %immutable %feature("immutable")
41#define %mutable %feature("immutable","")
42
43/* Directives for callback functions */
44
45/* Experimental */
46
47#define %callback(x) %feature("callback") `x`;
48#define %nocallback %feature("callback","");
49
50/* Directives for attribute functions */
51
52#define %attributefunc(_x,_y) %pragma(swig) attributefunction=`_x`":"`_y`;
53#define %noattributefunc %pragma(swig) noattributefunction;
54
55/* %ignore directive */
56
57#define %ignore %rename($ignore)
58#define %ignorewarn(x) %rename("$ignore:" x)
59
60/* Generation of default constructors/destructors */
61
62#define %nodefault %feature("nodefault")
63#define %makedefault %feature("nodefault","")
64
65/* Common features */
66
67#define %exception %feature("except")
68#define %noexception %feature("except","")
69#define %newobject %feature("new")
70
71/* Warnings */
72#define %warnfilter(...) %feature("warnfilter",`__VA_ARGS__`)
73
74/* Default handling of certain overloaded operators */
75
76#ifdef __cplusplus
77%ignorewarn("350:operator new ignored") operator new;
78%ignorewarn("351:operator delete ignored") operator delete;
79%ignorewarn("394:operator new[] ignored") operator new[];
80%ignorewarn("395:operator delete[] ignored") operator delete[];
81
82/* Smart pointer handling */
83%rename(__deref__) operator->;
84
85/* Define std namespace */
86namespace std {
87}
88#endif
89
90/* Set up the typemap for handling new return strings */
91
92#ifdef __cplusplus
93%typemap(newfree) char * "delete [] $1;";
94#else
95%typemap(newfree) char * "free($1);";
96#endif
97
98/* Default typemap for handling char * members */
99
100#ifdef __cplusplus
101%typemap(memberin) char * {
102 if ($1) delete [] $1;
103 $1 = ($1_type) (new char[strlen($input)+1]);
104 strcpy((char *) $1,$input);
105}
106%typemap(memberin,warning="451:Setting const char * member may leak memory.") const char * {
107 $1 = ($1_type) (new char[strlen($input)+1]);
108 strcpy((char *) $1,$input);
109}
110%typemap(globalin) char * {
111 if ($1) delete [] $1;
112 $1 = ($1_type) (new char[strlen($input)+1]);
113 strcpy((char *) $1,$input);
114}
115%typemap(globalin,warning="451:Setting const char * variable may leak memory.") const char * {
116 $1 = ($1_type) (new char[strlen($input)+1]);
117 strcpy((char *) $1,$input);
118}
119#else
120%typemap(memberin) char * {
121 if ($1) free((char*)$1);
122 $1 = ($1_type) malloc(strlen($input)+1);
123 strcpy((char*)$1,$input);
124}
125%typemap(memberin,warning="451:Setting const char * member may leak memory.") const char * {
126 $1 = ($1_type) malloc(strlen($input)+1);
127 strcpy((char*)$1,$input);
128}
129%typemap(globalin) char * {
130 if ($1) free((char*)$1);
131 $1 = ($1_type) malloc(strlen($input)+1);
132 strcpy((char*)$1,$input);
133}
134%typemap(globalin,warning="451:Setting const char * variable may leak memory.") const char * {
135 $1 = ($1_type) malloc(strlen($input)+1);
136 strcpy((char*)$1,$input);
137}
138
139#endif
140
141/* Character array handling */
142
143%typemap(memberin) char [ANY] {
144 if ($input) strncpy($1,$input,$1_dim0);
145 else $1[0] = 0;
146}
147
148%typemap(globalin) char [ANY] {
149 if ($input) strncpy($1,$input,$1_dim0);
150 else $1[0] = 0;
151}
152
153/* memberin typemap for arrays. */
154
155%typemap(memberin) SWIGTYPE [] {
156 int ii;
157 $1_basetype *b = ($1_basetype *) $1;
158 for (ii = 0; ii < $1_size; ii++) b[ii] = *(($1_basetype *) $input + ii);
159}
160
161%typemap(globalin) SWIGTYPE [] {
162 int ii;
163 $1_basetype *b = ($1_basetype *) $1;
164 for (ii = 0; ii < $1_size; ii++) b[ii] = *(($1_basetype *) $input + ii);
165}
166
167/* Typemap for variable length arguments sentinel value. Used
168 by the %varargs directive. */
169
170%typemap(ignore) SWIGTYPE *VARARGS_SENTINEL, SWIGTYPE VARARGS_SENTINEL "";
171
172
173/*
174 * Function/method overloading support. This is done through typemaps,
175 * but also involve a precedence level.
176 */
177
178/* Macro for overload resolution */
179
180#define %typecheck(_x) %typemap(typecheck, precedence=_x)
181
182/* Macros for precedence levels */
183
184%define SWIG_TYPECHECK_POINTER 0 %enddef
185%define SWIG_TYPECHECK_VOIDPTR 10 %enddef
186%define SWIG_TYPECHECK_BOOL 15 %enddef
187%define SWIG_TYPECHECK_UINT8 20 %enddef
188%define SWIG_TYPECHECK_INT8 25 %enddef
189%define SWIG_TYPECHECK_UINT16 30 %enddef
190%define SWIG_TYPECHECK_INT16 35 %enddef
191%define SWIG_TYPECHECK_UINT32 40 %enddef
192%define SWIG_TYPECHECK_INT32 45 %enddef
193%define SWIG_TYPECHECK_UINT64 50 %enddef
194%define SWIG_TYPECHECK_INT64 55 %enddef
195%define SWIG_TYPECHECK_UINT128 60 %enddef
196%define SWIG_TYPECHECK_INT128 65 %enddef
197%define SWIG_TYPECHECK_INTEGER 70 %enddef
198%define SWIG_TYPECHECK_FLOAT 80 %enddef
199%define SWIG_TYPECHECK_DOUBLE 90 %enddef
200%define SWIG_TYPECHECK_COMPLEX 100 %enddef
201%define SWIG_TYPECHECK_UNICHAR 110 %enddef
202%define SWIG_TYPECHECK_UNISTRING 120 %enddef
203%define SWIG_TYPECHECK_CHAR 130 %enddef
204%define SWIG_TYPECHECK_STRING 140 %enddef
205%define SWIG_TYPECHECK_VECTOR 150 %enddef
206
207/*
208 * This template wrapper is used to handle C++ objects that are passed or
209 * returned by value. This is necessary to handle objects that define
210 * no default-constructor (making it difficult for SWIG to properly declare
211 * local variables).
212 *
213 * The wrapper is used as follows. First consider a function like this:
214 *
215 * Vector cross_product(Vector a, Vector b)
216 *
217 * Now, if Vector is defined as a C++ class with no default constructor,
218 * code is generated as follows:
219 *
220 * Vector *wrap_cross_product(Vector *inarg1, Vector *inarg2) {
221 * SwigValueWrapper<Vector> arg1;
222 * SwigValueWrapper<Vector> arg2;
223 * SwigValueWrapper<Vector> result;
224 *
225 * arg1 = *inarg1;
226 * arg2 = *inarg2;
227 * ...
228 * result = cross_product(arg1,arg2);
229 * ...
230 * return new Vector(result);
231 * }
232 *
233 * In the wrappers, the template SwigValueWrapper simply provides a thin
234 * layer around a Vector *. However, it does this in a way that allows
235 * the object to be bound after the variable declaration (which is not possible
236 * with the bare object when it lacks a default constructor).
237 *
238 * An observant reader will notice that the code after the variable declarations
239 * is *identical* to the code used for classes that do define default constructors.
240 * Thus, this neat trick allows us to fix this special case without having to
241 * make massive changes to typemaps and other parts of the SWIG code generator.
242 *
243 * Note: this code is not included when SWIG runs in C-mode, when classes
244 * define default constructors, or when pointers and references are used.
245 * SWIG tries to avoid doing this except in very special circumstances.
246 *
247 * Note: This solution suffers from making a large number of copies
248 * of the underlying object. However, this is needed in the interest of
249 * safety and in order to cover all of the possible ways in which a value
250 * might be assigned. For example:
251 *
252 * arg1 = *inarg1; // Assignment from a pointer
253 * arg1 = Vector(1,2,3); // Assignment from a value
254 *
255 * This wrapping technique was suggested by William Fulton and is henceforth
256 * known as the "Fulton Transform" :-).
257 */
258
259#ifdef __cplusplus
260%insert("runtime") %{
261#ifdef __cplusplus
262template<class T> class SwigValueWrapper {
263 T *tt;
264public:
265 inline SwigValueWrapper() : tt(0) { }
266 inline ~SwigValueWrapper() { if (tt) delete tt; }
267 inline SwigValueWrapper& operator=(const T& t) { tt = new T(t); return *this; }
268 inline operator T&() const { return *tt; }
269 inline T *operator&() { return tt; }
270};
271#endif
272%}
273#endif
274
275/* Macro for setting a dynamic cast function */
276%define DYNAMIC_CAST(mangle,func)
277%init %{
278 mangle->dcast = (swig_dycast_func) func;
279%}
280%enddef
281
282