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