PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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