1 %include <shared_ptr.i> 2 3 // Set SHARED_PTR_DISOWN to $disown if required, for example 4 // #define SHARED_PTR_DISOWN $disown 5 #if !defined(SHARED_PTR_DISOWN) 6 #define SHARED_PTR_DISOWN 0 7 #endif 8 9 %fragment("SWIG_null_deleter_python", "header", fragment="SWIG_null_deleter") { 10 %#define SWIG_NO_NULL_DELETER_SWIG_BUILTIN_INIT 11 } 12 13 // Language specific macro implementing all the customisations for handling the smart pointer 14 %define SWIG_SHARED_PTR_TYPEMAPS(CONST, TYPE...) 15 16 // %naturalvar is as documented for member variables 17 %naturalvar TYPE; 18 %naturalvar SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >; 19 20 // destructor wrapper customisation 21 %feature("unref") TYPE 22 //"if (debug_shared) { cout << \"deleting use_count: \" << (*smartarg1).use_count() << \" [\" << (boost::get_deleter<SWIG_null_deleter>(*smartarg1) ? std::string(\"CANNOT BE DETERMINED SAFELY\") : ( (*smartarg1).get() ? (*smartarg1)->getValue() : std::string(\"NULL PTR\") )) << \"]\" << endl << flush; }\n" 23 "(void)arg1; delete smartarg1;" 24 25 // Typemap customisations... 26 27 // plain value 28 %typemap(in) CONST TYPE (void *argp, int res = 0) { 29 int newmem = 0; 30 res = SWIG_ConvertPtrAndOwn($input, &argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), %convertptr_flags, &newmem); 31 if (!SWIG_IsOK(res)) { 32 %argument_fail(res, "$type", $symname, $argnum); 33 } 34 if (!argp) { 35 %argument_nullref("$type", $symname, $argnum); 36 } else { 37 $1 = *(%reinterpret_cast(argp, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *)->get()); 38 if (newmem & SWIG_CAST_NEW_MEMORY) delete %reinterpret_cast(argp, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *); 39 } 40 } 41 %typemap(out) CONST TYPE { 42 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *smartresult = new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >(new $1_ltype(($1_ltype &)$1)); 43 %set_output(SWIG_NewPointerObj(%as_voidptr(smartresult), $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), SWIG_POINTER_OWN)); 44 } 45 46 %typemap(varin) CONST TYPE { 47 void *argp = 0; 48 int newmem = 0; 49 int res = SWIG_ConvertPtrAndOwn($input, &argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), %convertptr_flags, &newmem); 50 if (!SWIG_IsOK(res)) { 51 %variable_fail(res, "$type", "$name"); 52 } 53 if (!argp) { 54 %argument_nullref("$type", $symname, $argnum); 55 } else { 56 $1 = *(%reinterpret_cast(argp, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *)->get()); 57 if (newmem & SWIG_CAST_NEW_MEMORY) delete %reinterpret_cast(argp, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *); 58 } 59 } 60 %typemap(varout) CONST TYPE { 61 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *smartresult = new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >(new $1_ltype(($1_ltype &)$1)); 62 %set_varoutput(SWIG_NewPointerObj(%as_voidptr(smartresult), $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), SWIG_POINTER_OWN)); 63 } 64 65 // plain pointer 66 // Note: $disown not implemented by default as it will lead to a memory leak of the shared_ptr instance 67 %typemap(in) CONST TYPE * (void *argp = 0, int res = 0, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > tempshared, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *smartarg = 0) { 68 int newmem = 0; 69 res = SWIG_ConvertPtrAndOwn($input, &argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), SHARED_PTR_DISOWN | %convertptr_flags, &newmem); 70 if (!SWIG_IsOK(res)) { 71 %argument_fail(res, "$type", $symname, $argnum); 72 } 73 if (newmem & SWIG_CAST_NEW_MEMORY) { 74 tempshared = *%reinterpret_cast(argp, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *); 75 delete %reinterpret_cast(argp, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *); 76 $1 = %const_cast(tempshared.get(), $1_ltype); 77 } else { 78 smartarg = %reinterpret_cast(argp, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *); 79 $1 = %const_cast((smartarg ? smartarg->get() : 0), $1_ltype); 80 } 81 } 82 83 %typemap(out, fragment="SWIG_null_deleter_python") CONST TYPE * { 84 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *smartresult = $1 ? new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >($1 SWIG_NO_NULL_DELETER_$owner) : 0; 85 %set_output(SWIG_NewPointerObj(%as_voidptr(smartresult), $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), $owner | SWIG_POINTER_OWN)); 86 } 87 88 %typemap(varin) CONST TYPE * { 89 void *argp = 0; 90 int newmem = 0; 91 int res = SWIG_ConvertPtrAndOwn($input, &argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), %convertptr_flags, &newmem); 92 if (!SWIG_IsOK(res)) { 93 %variable_fail(res, "$type", "$name"); 94 } 95 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > tempshared; 96 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *smartarg = 0; 97 if (newmem & SWIG_CAST_NEW_MEMORY) { 98 tempshared = *%reinterpret_cast(argp, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *); 99 delete %reinterpret_cast(argp, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *); 100 $1 = %const_cast(tempshared.get(), $1_ltype); 101 } else { 102 smartarg = %reinterpret_cast(argp, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *); 103 $1 = %const_cast((smartarg ? smartarg->get() : 0), $1_ltype); 104 } 105 } 106 %typemap(varout, fragment="SWIG_null_deleter_python") CONST TYPE * { 107 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *smartresult = $1 ? new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >($1 SWIG_NO_NULL_DELETER_0) : 0; 108 %set_varoutput(SWIG_NewPointerObj(%as_voidptr(smartresult), $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), SWIG_POINTER_OWN)); 109 } 110 111 // plain reference 112 %typemap(in) CONST TYPE & (void *argp = 0, int res = 0, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > tempshared) { 113 int newmem = 0; 114 res = SWIG_ConvertPtrAndOwn($input, &argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), %convertptr_flags, &newmem); 115 if (!SWIG_IsOK(res)) { 116 %argument_fail(res, "$type", $symname, $argnum); 117 } 118 if (!argp) { %argument_nullref("$type", $symname, $argnum); } 119 if (newmem & SWIG_CAST_NEW_MEMORY) { 120 tempshared = *%reinterpret_cast(argp, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *); 121 delete %reinterpret_cast(argp, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *); 122 $1 = %const_cast(tempshared.get(), $1_ltype); 123 } else { 124 $1 = %const_cast(%reinterpret_cast(argp, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *)->get(), $1_ltype); 125 } 126 } 127 %typemap(out, fragment="SWIG_null_deleter_python") CONST TYPE & { 128 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *smartresult = new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >($1 SWIG_NO_NULL_DELETER_$owner); 129 %set_output(SWIG_NewPointerObj(%as_voidptr(smartresult), $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), SWIG_POINTER_OWN)); 130 } 131 132 %typemap(varin) CONST TYPE & { 133 void *argp = 0; 134 int newmem = 0; 135 int res = SWIG_ConvertPtrAndOwn($input, &argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), %convertptr_flags, &newmem); 136 if (!SWIG_IsOK(res)) { 137 %variable_fail(res, "$type", "$name"); 138 } 139 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > tempshared; 140 if (!argp) { %argument_nullref("$type", $symname, $argnum); } 141 if (newmem & SWIG_CAST_NEW_MEMORY) { 142 tempshared = *%reinterpret_cast(argp, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *); 143 delete %reinterpret_cast(argp, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *); 144 $1 = *%const_cast(tempshared.get(), $1_ltype); 145 } else { 146 $1 = *%const_cast(%reinterpret_cast(argp, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *)->get(), $1_ltype); 147 } 148 } 149 %typemap(varout, fragment="SWIG_null_deleter_python") CONST TYPE & { 150 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *smartresult = new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >(&$1 SWIG_NO_NULL_DELETER_0); 151 %set_varoutput(SWIG_NewPointerObj(%as_voidptr(smartresult), $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), SWIG_POINTER_OWN)); 152 } 153 154 // plain pointer by reference 155 // Note: $disown not implemented by default as it will lead to a memory leak of the shared_ptr instance 156 %typemap(in) TYPE *CONST& (void *argp = 0, int res = 0, $*1_ltype temp = 0, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > tempshared) { 157 int newmem = 0; 158 res = SWIG_ConvertPtrAndOwn($input, &argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), SHARED_PTR_DISOWN | %convertptr_flags, &newmem); 159 if (!SWIG_IsOK(res)) { 160 %argument_fail(res, "$type", $symname, $argnum); 161 } 162 if (newmem & SWIG_CAST_NEW_MEMORY) { 163 tempshared = *%reinterpret_cast(argp, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *); 164 delete %reinterpret_cast(argp, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *); 165 temp = %const_cast(tempshared.get(), $*1_ltype); 166 } else { 167 temp = %const_cast(%reinterpret_cast(argp, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *)->get(), $*1_ltype); 168 } 169 $1 = &temp; 170 } 171 %typemap(out, fragment="SWIG_null_deleter_python") TYPE *CONST& { 172 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *smartresult = new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >(*$1 SWIG_NO_NULL_DELETER_$owner); 173 %set_output(SWIG_NewPointerObj(%as_voidptr(smartresult), $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), SWIG_POINTER_OWN)); 174 } 175 176 %typemap(varin) TYPE *CONST& %{ 177 #error "varin typemap not implemented" 178 %} 179 %typemap(varout) TYPE *CONST& %{ 180 #error "varout typemap not implemented" 181 %} 182 183 // shared_ptr by value 184 %typemap(in) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > (void *argp, int res = 0) { 185 int newmem = 0; 186 res = SWIG_ConvertPtrAndOwn($input, &argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), %convertptr_flags, &newmem); 187 if (!SWIG_IsOK(res)) { 188 %argument_fail(res, "$type", $symname, $argnum); 189 } 190 if (argp) $1 = *(%reinterpret_cast(argp, $<ype)); 191 if (newmem & SWIG_CAST_NEW_MEMORY) delete %reinterpret_cast(argp, $<ype); 192 } 193 %typemap(out) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > { 194 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *smartresult = $1 ? new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >($1) : 0; 195 %set_output(SWIG_NewPointerObj(%as_voidptr(smartresult), $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), SWIG_POINTER_OWN)); 196 } 197 198 %typemap(varin) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > { 199 int newmem = 0; 200 void *argp = 0; 201 int res = SWIG_ConvertPtrAndOwn($input, &argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), %convertptr_flags, &newmem); 202 if (!SWIG_IsOK(res)) { 203 %variable_fail(res, "$type", "$name"); 204 } 205 $1 = argp ? *(%reinterpret_cast(argp, $<ype)) : SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE >(); 206 if (newmem & SWIG_CAST_NEW_MEMORY) delete %reinterpret_cast(argp, $<ype); 207 } 208 %typemap(varout) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > { 209 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *smartresult = $1 ? new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >($1) : 0; 210 %set_varoutput(SWIG_NewPointerObj(%as_voidptr(smartresult), $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), SWIG_POINTER_OWN)); 211 } 212 213 // shared_ptr by reference 214 %typemap(in) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > & (void *argp, int res = 0, $*1_ltype tempshared) { 215 int newmem = 0; 216 res = SWIG_ConvertPtrAndOwn($input, &argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), %convertptr_flags, &newmem); 217 if (!SWIG_IsOK(res)) { 218 %argument_fail(res, "$type", $symname, $argnum); 219 } 220 if (newmem & SWIG_CAST_NEW_MEMORY) { 221 if (argp) tempshared = *%reinterpret_cast(argp, $ltype); 222 delete %reinterpret_cast(argp, $ltype); 223 $1 = &tempshared; 224 } else { 225 $1 = (argp) ? %reinterpret_cast(argp, $ltype) : &tempshared; 226 } 227 } 228 %typemap(out) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > & { 229 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *smartresult = *$1 ? new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >(*$1) : 0; 230 %set_output(SWIG_NewPointerObj(%as_voidptr(smartresult), $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), SWIG_POINTER_OWN)); 231 } 232 233 %typemap(varin) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > & %{ 234 #error "varin typemap not implemented" 235 %} 236 %typemap(varout) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > & %{ 237 #error "varout typemap not implemented" 238 %} 239 240 // shared_ptr by pointer 241 %typemap(in) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > * (void *argp, int res = 0, $*1_ltype tempshared) { 242 int newmem = 0; 243 res = SWIG_ConvertPtrAndOwn($input, &argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), %convertptr_flags, &newmem); 244 if (!SWIG_IsOK(res)) { 245 %argument_fail(res, "$type", $symname, $argnum); 246 } 247 if (newmem & SWIG_CAST_NEW_MEMORY) { 248 if (argp) tempshared = *%reinterpret_cast(argp, $ltype); 249 delete %reinterpret_cast(argp, $ltype); 250 $1 = &tempshared; 251 } else { 252 $1 = (argp) ? %reinterpret_cast(argp, $ltype) : &tempshared; 253 } 254 } 255 %typemap(out) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > * { 256 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *smartresult = $1 && *$1 ? new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >(*$1) : 0; 257 %set_output(SWIG_NewPointerObj(%as_voidptr(smartresult), $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), SWIG_POINTER_OWN)); 258 if ($owner) delete $1; 259 } 260 261 %typemap(varin) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > * %{ 262 #error "varin typemap not implemented" 263 %} 264 %typemap(varout) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > * %{ 265 #error "varout typemap not implemented" 266 %} 267 268 // shared_ptr by pointer reference 269 %typemap(in) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *& (void *argp, int res = 0, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > tempshared, $*1_ltype temp = 0) { 270 int newmem = 0; 271 res = SWIG_ConvertPtrAndOwn($input, &argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), %convertptr_flags, &newmem); 272 if (!SWIG_IsOK(res)) { 273 %argument_fail(res, "$type", $symname, $argnum); 274 } 275 if (argp) tempshared = *%reinterpret_cast(argp, $*ltype); 276 if (newmem & SWIG_CAST_NEW_MEMORY) delete %reinterpret_cast(argp, $*ltype); 277 temp = &tempshared; 278 $1 = &temp; 279 } 280 %typemap(out) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *& { 281 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *smartresult = *$1 && **$1 ? new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >(**$1) : 0; 282 %set_output(SWIG_NewPointerObj(%as_voidptr(smartresult), $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), SWIG_POINTER_OWN)); 283 } 284 285 %typemap(varin) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *& %{ 286 #error "varin typemap not implemented" 287 %} 288 %typemap(varout) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *& %{ 289 #error "varout typemap not implemented" 290 %} 291 292 // Typecheck typemaps 293 // Note: SWIG_ConvertPtr with void ** parameter set to 0 instead of using SWIG_ConvertPtrAndOwn, so that the casting 294 // function is not called thereby avoiding a possible smart pointer copy constructor call when casting up the inheritance chain. 295 %typemap(typecheck,precedence=SWIG_TYPECHECK_POINTER,noblock=1) 296 TYPE CONST, 297 TYPE CONST &, 298 TYPE CONST *, 299 TYPE *CONST&, 300 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >, 301 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > &, 302 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *, 303 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *& { 304 int res = SWIG_ConvertPtr($input, 0, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), 0); 305 $1 = SWIG_CheckState(res); 306 } 307 308 309 // various missing typemaps - If ever used (unlikely) ensure compilation error rather than runtime bug 310 %typemap(in) CONST TYPE[], CONST TYPE[ANY], CONST TYPE (CLASS::*) %{ 311 #error "typemaps for $1_type not available" 312 %} 313 %typemap(out) CONST TYPE[], CONST TYPE[ANY], CONST TYPE (CLASS::*) %{ 314 #error "typemaps for $1_type not available" 315 %} 316 317 318 %template() SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >; 319 320 321 %enddef 322 323