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