Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

670 lines
23 KiB

  1. /* -----------------------------------------------------------------------------
  2. * swig.swg
  3. *
  4. * Common macro definitions for various SWIG directives. This file is always
  5. * included at the top of each input file.
  6. * ----------------------------------------------------------------------------- */
  7. /* -----------------------------------------------------------------------------
  8. * User Directives
  9. * ----------------------------------------------------------------------------- */
  10. /* Deprecated SWIG directives */
  11. #define %disabledoc %warn "104:%disabledoc is deprecated"
  12. #define %enabledoc %warn "105:%enabledoc is deprecated"
  13. #define %doconly %warn "106:%doconly is deprecated"
  14. #define %style %warn "107:%style is deprecated" /##/
  15. #define %localstyle %warn "108:%localstyle is deprecated" /##/
  16. #define %title %warn "109:%title is deprecated" /##/
  17. #define %section %warn "110:%section is deprecated" /##/
  18. #define %subsection %warn "111:%subsection is deprecated" /##/
  19. #define %subsubsection %warn "112:%subsubsection is deprecated" /##/
  20. #define %new %warn "117:%new is deprecated. Use %newobject"
  21. #define %text %insert("null")
  22. /* Code insertion directives such as %wrapper %{ ... %} */
  23. #define %init %insert("init")
  24. #define %wrapper %insert("wrapper")
  25. #define %header %insert("header")
  26. #define %runtime %insert("runtime")
  27. /* Class extension */
  28. #define %addmethods %warn "113:%addmethods is now %extend" %extend
  29. /* %ignore directive */
  30. #define %ignore %rename($ignore)
  31. #define %ignorewarn(x) %rename("$ignore:" x)
  32. /* Access control directives */
  33. #define %readonly %warn "114:%readonly is deprecated. Use %immutable; " %feature("immutable");
  34. #define %readwrite %warn "115:%readwrite is deprecated. Use %mutable; " %feature("immutable","");
  35. #define %immutable %feature("immutable")
  36. #define %noimmutable %feature("immutable","0")
  37. #define %clearimmutable %feature("immutable","")
  38. #define %mutable %clearimmutable
  39. /* Generation of default constructors/destructors (old form, don't use) */
  40. #define %nodefault %feature("nodefault","1")
  41. #define %default %feature("nodefault","0")
  42. #define %clearnodefault %feature("nodefault","")
  43. #define %makedefault %clearnodefault
  44. /* Disable the generation of implicit default constructor */
  45. #define %nodefaultctor %feature("nodefaultctor","1")
  46. #define %defaultctor %feature("nodefaultctor","0")
  47. #define %clearnodefaultctor %feature("nodefaultctor","")
  48. /* Disable the generation of implicit default destructor (dangerous) */
  49. #define %nodefaultdtor %feature("nodefaultdtor","1")
  50. #define %defaultdtor %feature("nodefaultdtor","0")
  51. #define %clearnodefaultdtor %feature("nodefaultdtor","")
  52. /* Enable the generation of copy constructor */
  53. #define %copyctor %feature("copyctor","1")
  54. #define %nocopyctor %feature("copyctor","0")
  55. #define %clearcopyctor %feature("copyctor","")
  56. /* Force the old nodefault behavior, ie disable both constructor and destructor */
  57. #define %oldnodefault %feature("oldnodefault","1")
  58. #define %nooldnodefault %feature("oldnodefault","0")
  59. #define %clearoldnodefault %feature("oldnodefault","")
  60. /* the %exception directive */
  61. #ifdef SWIGCSHARP
  62. #define %exception %feature("except", canthrow=1)
  63. #else
  64. #define %exception %feature("except")
  65. #endif
  66. #define %noexception %feature("except","0")
  67. #define %clearexception %feature("except","")
  68. /* the %allowexception directive allows the %exception feature to
  69. be applied to set/get variable methods */
  70. #define %allowexception %feature("allowexcept")
  71. #define %noallowexception %feature("allowexcept","0")
  72. #define %clearallowexception %feature("allowexcept","")
  73. /* the %exceptionvar directive, as %exception but it is only applied
  74. to set/get variable methods. You don't need to use the
  75. %allowexception directive when using %exceptionvar.
  76. */
  77. #ifdef SWIGCSHARP
  78. #define %exceptionvar %feature("exceptvar", canthrow=1)
  79. #else
  80. #define %exceptionvar %feature("exceptvar")
  81. #endif
  82. #define %noexceptionvar %feature("exceptvar","0")
  83. #define %clearexceptionvar %feature("exceptvar","")
  84. /* the %catches directive */
  85. #define %catches(tlist...) %feature("catches","("`tlist`")")
  86. #define %clearcatches %feature("catches","")
  87. /* the %exceptionclass directive */
  88. #define %exceptionclass %feature("exceptionclass")
  89. #define %noexceptionclass %feature("exceptionclass","0")
  90. #define %clearexceptionclass %feature("exceptionclass","")
  91. /* the %newobject directive */
  92. #define %newobject %feature("new")
  93. #define %nonewobject %feature("new","0")
  94. #define %clearnewobject %feature("new","")
  95. /* the %delobject directive */
  96. #define %delobject %feature("del")
  97. #define %nodelobject %feature("del","0")
  98. #define %cleardelobject %feature("del","")
  99. /* the %refobject/%unrefobject directives */
  100. #define %refobject %feature("ref")
  101. #define %norefobject %feature("ref","0")
  102. #define %clearrefobject %feature("ref","")
  103. #define %unrefobject %feature("unref")
  104. #define %nounrefobject %feature("unref","0")
  105. #define %clearunrefobject %feature("unref","")
  106. /* Directives for callback functions (experimental) */
  107. #define %callback(x) %feature("callback",`x`)
  108. #define %nocallback %feature("callback","0")
  109. #define %clearcallback %feature("callback","")
  110. /* the %fastdispatch directive */
  111. #define %fastdispatch %feature("fastdispatch")
  112. #define %nofastdispatch %feature("fastdispatch","0")
  113. #define %clearfastdispatch %feature("fastdispatch","")
  114. /* directors directives */
  115. #define %director %feature("director")
  116. #define %nodirector %feature("director","0")
  117. #define %cleardirector %feature("director","")
  118. /* naturalvar directives */
  119. #define %naturalvar %feature("naturalvar")
  120. #define %nonaturalvar %feature("naturalvar","0")
  121. #define %clearnaturalvar %feature("naturalvar","")
  122. /* valuewrapper directives */
  123. #define %valuewrapper %feature("valuewrapper",)
  124. #define %clearvaluewrapper %feature("valuewrapper","")
  125. #define %novaluewrapper %feature("novaluewrapper")
  126. #define %clearnovaluewrapper %feature("novaluewrapper","")
  127. /* Contract support - Experimental and undocumented */
  128. #define %contract %feature("contract")
  129. #define %nocontract %feature("contract","0")
  130. #define %clearcontract %feature("contract","")
  131. /* Macro for setting a dynamic cast function */
  132. %define DYNAMIC_CAST(mangle,func)
  133. %init %{
  134. mangle->dcast = (swig_dycast_func) func;
  135. %}
  136. %enddef
  137. /* aggregation support */
  138. /*
  139. This macro performs constant aggregation. Basically the idea of
  140. constant aggregation is that you can group a collection of constants
  141. together. For example, suppose you have some code like this:
  142. #define UP 1
  143. #define DOWN 2
  144. #define LEFT 3
  145. #define RIGHT 4
  146. Now, suppose you had a function like this:
  147. int move(int direction)
  148. In this case, you might want to restrict the direction argument to
  149. one of the supplied constant names. To do this, you could write some
  150. typemap code by hand. Alternatively, you can use the
  151. %aggregate_check macro defined here to create a simple check
  152. function for you. Here is an example:
  153. %aggregate_check(int, check_direction, UP, DOWN, LEFT, RIGHT);
  154. Now, using a typemap
  155. %typemap(check) int direction {
  156. if (!check_direction($1)) SWIG_exception(SWIG_ValueError,"Bad direction.");
  157. }
  158. or a contract (better)
  159. %contract move(int x) {
  160. require:
  161. check_direction(x);
  162. }
  163. */
  164. %define %aggregate_check(TYPE, NAME, FIRST, ...)
  165. %wrapper %{
  166. static int NAME(TYPE x) {
  167. static TYPE values[] = { FIRST, ##__VA_ARGS__ };
  168. static int size = sizeof(values);
  169. int i,j;
  170. for (i = 0, j = 0; i < size; i+=sizeof(TYPE),j++) {
  171. if (x == values[j]) return 1;
  172. }
  173. return 0;
  174. }
  175. %}
  176. %enddef
  177. /* -----------------------------------------------------------------------------
  178. * %rename predicates
  179. * ----------------------------------------------------------------------------- */
  180. /*
  181. Predicates to be used with %rename, for example:
  182. - to rename all the functions:
  183. %rename("%(utitle)s", %$isfunction) "";
  184. - to rename only the member methods:
  185. %rename("m_%(utitle)s", %$isfunction, %$ismember) "";
  186. - to rename only the global functions:
  187. %rename("m_%(utitle)s", %$isfunction, %$not %$ismember) "";
  188. or
  189. %rename("g_%(utitle)s", %$isfunction, %$isglobal) "";
  190. - to ignore the enumitems in a given class:
  191. %rename("$ignore", %$isenumitem, %$classname="MyClass") "";
  192. we use the prefix '%$' to avoid clashings with other swig
  193. macros/directives.
  194. */
  195. %define %$not "not" %enddef
  196. %define %$isenum "match"="enum" %enddef
  197. %define %$isenumitem "match"="enumitem" %enddef
  198. %define %$isaccess "match"="access" %enddef
  199. %define %$isclass "match"="class","notmatch$template$templatetype"="class" %enddef
  200. %define %$isextend "match"="extend" %enddef
  201. %define %$isextend "match"="extend" %enddef
  202. %define %$isconstructor "match"="constructor" %enddef
  203. %define %$isdestructor "match"="destructor" %enddef
  204. %define %$isnamespace "match"="namespace" %enddef
  205. %define %$istemplate "match"="template" %enddef
  206. %define %$isconstant "match"="constant" %enddef /* %constant definition */
  207. %define %$isunion "match$kind"="union" %enddef
  208. %define %$isfunction "match$kind"="function" %enddef
  209. %define %$isvariable "match$kind"="variable" %enddef
  210. %define %$isimmutable "match$feature:immutable"="1" %enddef
  211. %define %$hasconsttype "match$hasconsttype"="1" %enddef
  212. %define %$hasvalue "match$hasvalue"="1" %enddef
  213. %define %$isextension "match$isextension"="1" %enddef
  214. %define %$isstatic "match$storage"="static" %enddef
  215. %define %$isfriend "match$storage"="friend" %enddef
  216. %define %$istypedef "match$storage"="typedef" %enddef
  217. %define %$isvirtual "match$storage"="virtual" %enddef
  218. %define %$isexplicit "match$storage"="explicit" %enddef
  219. %define %$isextern "match$storage"="extern" %enddef
  220. %define %$ismember "match$ismember"="1" %enddef
  221. %define %$isglobal %not %ismember %enddef
  222. %define %$innamespace "match$parentNode$nodeType"="namespace" %enddef
  223. %define %$ispublic "match$access"="public" %enddef
  224. %define %$isprotected "match$access"="protected" %enddef
  225. %define %$isprivate "match$access"="private" %enddef
  226. %define %$ismemberget "match$memberget"="1" %enddef
  227. %define %$ismemberset "match$memberset"="1" %enddef
  228. %define %$classname %ismember,match$parentNode$name %enddef
  229. /* -----------------------------------------------------------------------------
  230. * Include all the warnings labels and macros
  231. * ----------------------------------------------------------------------------- */
  232. %include <swigwarnings.swg>
  233. /* -----------------------------------------------------------------------------
  234. * Default handling of certain overloaded operators
  235. * ----------------------------------------------------------------------------- */
  236. #ifdef __cplusplus
  237. %ignoreoperator(NEW) operator new;
  238. %ignoreoperator(DELETE) operator delete;
  239. %ignoreoperator(NEWARR) operator new[];
  240. %ignoreoperator(DELARR) operator delete[];
  241. /* add C++ operator aliases */
  242. %rename("operator &&") operator and; // `and' `&&'
  243. %rename("operator ||") operator or; // `or' `||'
  244. %rename("operator !") operator not; // `not' `!'
  245. %rename("operator &=") operator and_eq; // `and_eq' `&='
  246. %rename("operator &") operator bitand; // `bitand' `&'
  247. %rename("operator |") operator bitor; // `bitor' `|'
  248. %rename("operator ~") operator compl; // `compl' `~'
  249. %rename("operator !=") operator not_eq; // `not_eq' `!='
  250. %rename("operator |=") operator or_eq; // `or_eq' `|='
  251. %rename("operator ^") operator xor; // `xor' `^'
  252. %rename("operator ^=") operator xor_eq; // `xor_eq' `^='
  253. /* Smart pointer handling */
  254. %rename(__deref__) *::operator->;
  255. %rename(__ref__) *::operator*();
  256. %rename(__ref__) *::operator*() const;
  257. /* Define std namespace */
  258. namespace std {
  259. }
  260. #endif
  261. /* -----------------------------------------------------------------------------
  262. * Default char * and C array typemaps
  263. * ----------------------------------------------------------------------------- */
  264. /* Set up the typemap for handling new return strings */
  265. #ifdef __cplusplus
  266. %typemap(newfree) char * "delete [] $1;";
  267. #else
  268. %typemap(newfree) char * "free($1);";
  269. #endif
  270. /* Default typemap for handling char * members */
  271. #ifdef __cplusplus
  272. %typemap(memberin) char * {
  273. if ($1) delete [] $1;
  274. if ($input) {
  275. $1 = ($1_type) (new char[strlen((const char *)$input)+1]);
  276. strcpy((char *)$1, (const char *)$input);
  277. } else {
  278. $1 = 0;
  279. }
  280. }
  281. %typemap(memberin,warning=SWIGWARN_TYPEMAP_CHARLEAK_MSG) const char * {
  282. if ($input) {
  283. $1 = ($1_type) (new char[strlen((const char *)$input)+1]);
  284. strcpy((char *)$1, (const char *)$input);
  285. } else {
  286. $1 = 0;
  287. }
  288. }
  289. %typemap(globalin) char * {
  290. if ($1) delete [] $1;
  291. if ($input) {
  292. $1 = ($1_type) (new char[strlen((const char *)$input)+1]);
  293. strcpy((char *)$1, (const char *)$input);
  294. } else {
  295. $1 = 0;
  296. }
  297. }
  298. %typemap(globalin,warning=SWIGWARN_TYPEMAP_CHARLEAK_MSG) const char * {
  299. if ($input) {
  300. $1 = ($1_type) (new char[strlen((const char *)$input)+1]);
  301. strcpy((char *)$1, (const char *)$input);
  302. } else {
  303. $1 = 0;
  304. }
  305. }
  306. #else
  307. %typemap(memberin) char * {
  308. if ($1) free((char *)$1);
  309. if ($input) {
  310. $1 = ($1_type) malloc(strlen((const char *)$input)+1);
  311. strcpy((char *)$1, (const char *)$input);
  312. } else {
  313. $1 = 0;
  314. }
  315. }
  316. %typemap(memberin,warning=SWIGWARN_TYPEMAP_CHARLEAK_MSG) const char * {
  317. if ($input) {
  318. $1 = ($1_type) malloc(strlen((const char *)$input)+1);
  319. strcpy((char *)$1, (const char *)$input);
  320. } else {
  321. $1 = 0;
  322. }
  323. }
  324. %typemap(globalin) char * {
  325. if ($1) free((char *)$1);
  326. if ($input) {
  327. $1 = ($1_type) malloc(strlen((const char *)$input)+1);
  328. strcpy((char *)$1, (const char *)$input);
  329. } else {
  330. $1 = 0;
  331. }
  332. }
  333. %typemap(globalin,warning=SWIGWARN_TYPEMAP_CHARLEAK_MSG) const char * {
  334. if ($input) {
  335. $1 = ($1_type) malloc(strlen((const char *)$input)+1);
  336. strcpy((char *)$1, (const char *)$input);
  337. } else {
  338. $1 = 0;
  339. }
  340. }
  341. #endif
  342. /* Character array handling */
  343. %typemap(memberin) char [ANY] {
  344. if ($input) strncpy((char *)$1, (const char *)$input, $1_dim0);
  345. else $1[0] = 0;
  346. }
  347. %typemap(globalin) char [ANY] {
  348. if ($input) strncpy((char *)$1, (const char *)$input, $1_dim0);
  349. else $1[0] = 0;
  350. }
  351. %typemap(memberin) char [] {
  352. if ($input) strcpy((char *)$1, (const char *)$input);
  353. else $1[0] = 0;
  354. }
  355. %typemap(globalin) char [] {
  356. if ($input) strcpy((char *)$1, (const char *)$input);
  357. else $1[0] = 0;
  358. }
  359. /* memberin/globalin typemap for arrays. */
  360. %typemap(memberin) SWIGTYPE [ANY] {
  361. size_t ii;
  362. $1_basetype *b = ($1_basetype *) $1;
  363. for (ii = 0; ii < (size_t)$1_size; ii++) b[ii] = *(($1_basetype *) $input + ii);
  364. }
  365. %typemap(globalin) SWIGTYPE [ANY] {
  366. size_t ii;
  367. $1_basetype *b = ($1_basetype *) $1;
  368. for (ii = 0; ii < (size_t)$1_size; ii++) b[ii] = *(($1_basetype *) $input + ii);
  369. }
  370. /* memberin/globalin typemap for double arrays. */
  371. %typemap(memberin) SWIGTYPE [ANY][ANY] {
  372. $basetype (*inp)[$dim1] = ($basetype (*)[$dim1])($input);
  373. $basetype (*dest)[$dim1] = ($basetype (*)[$dim1])($1);
  374. size_t ii = 0;
  375. for (; ii < $dim0; ++ii) {
  376. $basetype *ip = inp[ii];
  377. $basetype *dp = dest[ii];
  378. size_t jj = 0;
  379. for (; jj < $dim1; ++jj) dp[jj] = ip[jj];
  380. }
  381. }
  382. %typemap(globalin) SWIGTYPE [ANY][ANY] {
  383. $basetype (*inp)[$dim1] = ($basetype (*)[$dim1])($input);
  384. $basetype (*dest)[$dim1] = ($basetype (*)[$dim1])($1);
  385. size_t ii = 0;
  386. for (; ii < $dim0; ++ii) {
  387. $basetype *ip = inp[ii];
  388. $basetype *dp = dest[ii];
  389. size_t jj = 0;
  390. for (; jj < $dim1; ++jj) dp[jj] = ip[jj];
  391. }
  392. }
  393. /* Typemap for variable length arguments sentinel value. Used
  394. by the %varargs directive. */
  395. %typemap(in,numinputs=0) SWIGTYPE *VARARGS_SENTINEL, SWIGTYPE VARARGS_SENTINEL "";
  396. /* -----------------------------------------------------------------------------
  397. * Overloading support
  398. * ----------------------------------------------------------------------------- */
  399. /*
  400. * Function/method overloading support. This is done through typemaps,
  401. * but also involve a precedence level.
  402. */
  403. /* Macro for overload resolution */
  404. %define %typecheck(_x...) %typemap(typecheck, precedence=_x) %enddef
  405. /* Macros for precedence levels */
  406. %define SWIG_TYPECHECK_POINTER 0 %enddef
  407. %define SWIG_TYPECHECK_ITERATOR 5 %enddef
  408. %define SWIG_TYPECHECK_VOIDPTR 10 %enddef
  409. %define SWIG_TYPECHECK_BOOL 15 %enddef
  410. %define SWIG_TYPECHECK_UINT8 20 %enddef
  411. %define SWIG_TYPECHECK_INT8 25 %enddef
  412. %define SWIG_TYPECHECK_UINT16 30 %enddef
  413. %define SWIG_TYPECHECK_INT16 35 %enddef
  414. %define SWIG_TYPECHECK_UINT32 40 %enddef
  415. %define SWIG_TYPECHECK_INT32 45 %enddef
  416. %define SWIG_TYPECHECK_SIZE 47 %enddef
  417. %define SWIG_TYPECHECK_PTRDIFF 48 %enddef
  418. %define SWIG_TYPECHECK_UINT64 50 %enddef
  419. %define SWIG_TYPECHECK_INT64 55 %enddef
  420. %define SWIG_TYPECHECK_UINT128 60 %enddef
  421. %define SWIG_TYPECHECK_INT128 65 %enddef
  422. %define SWIG_TYPECHECK_INTEGER 70 %enddef
  423. %define SWIG_TYPECHECK_FLOAT 80 %enddef
  424. %define SWIG_TYPECHECK_DOUBLE 90 %enddef
  425. %define SWIG_TYPECHECK_CPLXFLT 95 %enddef
  426. %define SWIG_TYPECHECK_CPLXDBL 100 %enddef
  427. %define SWIG_TYPECHECK_COMPLEX 105 %enddef
  428. %define SWIG_TYPECHECK_UNICHAR 110 %enddef
  429. %define SWIG_TYPECHECK_STDUNISTRING 115 %enddef
  430. %define SWIG_TYPECHECK_UNISTRING 120 %enddef
  431. %define SWIG_TYPECHECK_CHAR 130 %enddef
  432. %define SWIG_TYPECHECK_STDSTRING 135 %enddef
  433. %define SWIG_TYPECHECK_STRING 140 %enddef
  434. %define SWIG_TYPECHECK_PAIR 150 %enddef
  435. %define SWIG_TYPECHECK_VECTOR 160 %enddef
  436. %define SWIG_TYPECHECK_DEQUE 170 %enddef
  437. %define SWIG_TYPECHECK_LIST 180 %enddef
  438. %define SWIG_TYPECHECK_SET 190 %enddef
  439. %define SWIG_TYPECHECK_MULTISET 200 %enddef
  440. %define SWIG_TYPECHECK_MAP 210 %enddef
  441. %define SWIG_TYPECHECK_MULTIMAP 220 %enddef
  442. %define SWIG_TYPECHECK_STACK 230 %enddef
  443. %define SWIG_TYPECHECK_QUEUE 240 %enddef
  444. %define SWIG_TYPECHECK_BOOL_ARRAY 1015 %enddef
  445. %define SWIG_TYPECHECK_INT8_ARRAY 1025 %enddef
  446. %define SWIG_TYPECHECK_INT16_ARRAY 1035 %enddef
  447. %define SWIG_TYPECHECK_INT32_ARRAY 1045 %enddef
  448. %define SWIG_TYPECHECK_INT64_ARRAY 1055 %enddef
  449. %define SWIG_TYPECHECK_INT128_ARRAY 1065 %enddef
  450. %define SWIG_TYPECHECK_FLOAT_ARRAY 1080 %enddef
  451. %define SWIG_TYPECHECK_DOUBLE_ARRAY 1090 %enddef
  452. %define SWIG_TYPECHECK_CHAR_ARRAY 1130 %enddef
  453. %define SWIG_TYPECHECK_STRING_ARRAY 1140 %enddef
  454. %define SWIG_TYPECHECK_OBJECT_ARRAY 1150 %enddef
  455. %define SWIG_TYPECHECK_BOOL_PTR 2015 %enddef
  456. %define SWIG_TYPECHECK_UINT8_PTR 2020 %enddef
  457. %define SWIG_TYPECHECK_INT8_PTR 2025 %enddef
  458. %define SWIG_TYPECHECK_UINT16_PTR 2030 %enddef
  459. %define SWIG_TYPECHECK_INT16_PTR 2035 %enddef
  460. %define SWIG_TYPECHECK_UINT32_PTR 2040 %enddef
  461. %define SWIG_TYPECHECK_INT32_PTR 2045 %enddef
  462. %define SWIG_TYPECHECK_UINT64_PTR 2050 %enddef
  463. %define SWIG_TYPECHECK_INT64_PTR 2055 %enddef
  464. %define SWIG_TYPECHECK_FLOAT_PTR 2080 %enddef
  465. %define SWIG_TYPECHECK_DOUBLE_PTR 2090 %enddef
  466. %define SWIG_TYPECHECK_CHAR_PTR 2130 %enddef
  467. %define SWIG_TYPECHECK_SWIGOBJECT 5000 %enddef
  468. /* -----------------------------------------------------------------------------
  469. * Runtime code
  470. * ----------------------------------------------------------------------------- */
  471. /* The SwigValueWrapper class */
  472. /*
  473. * This template wrapper is used to handle C++ objects that are passed or
  474. * returned by value. This is necessary to handle objects that define
  475. * no default-constructor (making it difficult for SWIG to properly declare
  476. * local variables).
  477. *
  478. * The wrapper is used as follows. First consider a function like this:
  479. *
  480. * Vector cross_product(Vector a, Vector b)
  481. *
  482. * Now, if Vector is defined as a C++ class with no default constructor,
  483. * code is generated as follows:
  484. *
  485. * Vector *wrap_cross_product(Vector *inarg1, Vector *inarg2) {
  486. * SwigValueWrapper<Vector> arg1;
  487. * SwigValueWrapper<Vector> arg2;
  488. * SwigValueWrapper<Vector> result;
  489. *
  490. * arg1 = *inarg1;
  491. * arg2 = *inarg2;
  492. * ...
  493. * result = cross_product(arg1,arg2);
  494. * ...
  495. * return new Vector(result);
  496. * }
  497. *
  498. * In the wrappers, the template SwigValueWrapper simply provides a thin
  499. * layer around a Vector *. However, it does this in a way that allows
  500. * the object to be bound after the variable declaration (which is not possible
  501. * with the bare object when it lacks a default constructor).
  502. *
  503. * An observant reader will notice that the code after the variable declarations
  504. * is *identical* to the code used for classes that do define default constructors.
  505. * Thus, this neat trick allows us to fix this special case without having to
  506. * make massive changes to typemaps and other parts of the SWIG code generator.
  507. *
  508. * Note: this code is not included when SWIG runs in C-mode, when classes
  509. * define default constructors, or when pointers and references are used.
  510. * SWIG tries to avoid doing this except in very special circumstances.
  511. *
  512. * Note: This solution suffers from making a large number of copies
  513. * of the underlying object. However, this is needed in the interest of
  514. * safety and in order to cover all of the possible ways in which a value
  515. * might be assigned. For example:
  516. *
  517. * arg1 = *inarg1; // Assignment from a pointer
  518. * arg1 = Vector(1,2,3); // Assignment from a value
  519. *
  520. * This wrapping technique was suggested by William Fulton and is henceforth
  521. * known as the "Fulton Transform" :-).
  522. */
  523. #ifdef __cplusplus
  524. %insert("runtime") %{
  525. #ifdef __cplusplus
  526. template<typename T> class SwigValueWrapper {
  527. T *tt;
  528. public:
  529. SwigValueWrapper() : tt(0) { }
  530. SwigValueWrapper(const SwigValueWrapper<T>& rhs) : tt(new T(*rhs.tt)) { }
  531. SwigValueWrapper(const T& t) : tt(new T(t)) { }
  532. ~SwigValueWrapper() { delete tt; }
  533. SwigValueWrapper& operator=(const T& t) { delete tt; tt = new T(t); return *this; }
  534. operator T&() const { return *tt; }
  535. T *operator&() { return tt; }
  536. private:
  537. SwigValueWrapper& operator=(const SwigValueWrapper<T>& rhs);
  538. };%}
  539. /*
  540. * SwigValueInit() is a generic initialisation solution as the following approach:
  541. *
  542. * T c_result = T();
  543. *
  544. * doesn't compile for all types for example:
  545. *
  546. * unsigned int c_result = unsigned int();
  547. */
  548. %insert("runtime") %{
  549. template <typename T> T SwigValueInit() {
  550. return T();
  551. }
  552. #endif
  553. %}
  554. #endif
  555. /* The swiglabels */
  556. %insert("runtime") "swiglabels.swg"