1 %include <shared_ptr.i> 2 3 %define SWIG_SHARED_PTR_TYPEMAPS(CONST, TYPE...) 4 5 %naturalvar TYPE; 6 %naturalvar SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >; 7 8 // destructor mods 9 %feature("unref") TYPE 10 //"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" 11 "(void)arg1; delete smartarg1;" 12 13 14 // plain value 15 %typemap(in, canthrow=1) CONST TYPE ($&1_type argp = 0) %{ 16 argp = ((SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *)$input) ? ((SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *)$input)->get() : 0; 17 if (!argp) { 18 SWIG_DSetPendingException(SWIG_DIllegalArgumentException, "Attempt to dereference null $1_type"); 19 return $null; 20 } 21 $1 = *argp; %} 22 %typemap(out) CONST TYPE 23 %{ $result = new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >(new $1_ltype(($1_ltype &)$1)); %} 24 25 // plain pointer 26 %typemap(in, canthrow=1) CONST TYPE * (SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *smartarg = 0) %{ 27 smartarg = (SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *)$input; 28 $1 = (TYPE *)(smartarg ? smartarg->get() : 0); %} 29 %typemap(out, fragment="SWIG_null_deleter") CONST TYPE * %{ 30 $result = $1 ? new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >($1 SWIG_NO_NULL_DELETER_$owner) : 0; 31 %} 32 33 // plain reference 34 %typemap(in, canthrow=1) CONST TYPE & %{ 35 $1 = ($1_ltype)(((SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *)$input) ? ((SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *)$input)->get() : 0); 36 if (!$1) { 37 SWIG_DSetPendingException(SWIG_DIllegalArgumentException, "$1_type reference is null"); 38 return $null; 39 } %} 40 %typemap(out, fragment="SWIG_null_deleter") CONST TYPE & 41 %{ $result = new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >($1 SWIG_NO_NULL_DELETER_$owner); %} 42 43 // plain pointer by reference 44 %typemap(in) TYPE *CONST& ($*1_ltype temp = 0) 45 %{ temp = (TYPE *)(((SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *)$input) ? ((SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *)$input)->get() : 0); 46 $1 = &temp; %} 47 %typemap(out, fragment="SWIG_null_deleter") TYPE *CONST& 48 %{ $result = new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >(*$1 SWIG_NO_NULL_DELETER_$owner); %} 49 50 // shared_ptr by value 51 %typemap(in) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > 52 %{ if ($input) $1 = *($&1_ltype)$input; %} 53 %typemap(out) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > 54 %{ $result = $1 ? new $1_ltype($1) : 0; %} 55 56 // shared_ptr by reference 57 %typemap(in, canthrow=1) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > & ($*1_ltype tempnull) 58 %{ $1 = $input ? ($1_ltype)$input : &tempnull; %} 59 %typemap(out) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > & 60 %{ $result = *$1 ? new $*1_ltype(*$1) : 0; %} 61 62 // shared_ptr by pointer 63 %typemap(in) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > * ($*1_ltype tempnull) 64 %{ $1 = $input ? ($1_ltype)$input : &tempnull; %} 65 %typemap(out, fragment="SWIG_null_deleter") SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > * 66 %{ $result = ($1 && *$1) ? new $*1_ltype(*($1_ltype)$1) : 0; 67 if ($owner) delete $1; %} 68 69 // shared_ptr by pointer reference 70 %typemap(in) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *& (SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > tempnull, $*1_ltype temp = 0) 71 %{ temp = $input ? *($1_ltype)&$input : &tempnull; 72 $1 = &temp; %} 73 %typemap(out) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *& 74 %{ *($1_ltype)&$result = (*$1 && **$1) ? new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >(**$1) : 0; %} 75 76 // various missing typemaps - If ever used (unlikely) ensure compilation error rather than runtime bug 77 %typemap(in) CONST TYPE[], CONST TYPE[ANY], CONST TYPE (CLASS::*) %{ 78 #error "typemaps for $1_type not available" 79 %} 80 %typemap(out) CONST TYPE[], CONST TYPE[ANY], CONST TYPE (CLASS::*) %{ 81 #error "typemaps for $1_type not available" 82 %} 83 84 85 %typemap (ctype) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >, 86 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > &, 87 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *, 88 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *& "void *" 89 %typemap (imtype) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >, 90 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > &, 91 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *, 92 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *& "void*" 93 %typemap (dtype) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >, 94 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > &, 95 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *, 96 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *& "$typemap(dtype, TYPE)" 97 98 %typemap(din) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >, 99 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > &, 100 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *, 101 SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *& "$typemap(dtype, TYPE).swigGetCPtr($dinput)" 102 103 %typemap(dout, excode=SWIGEXCODE) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > { 104 void* cPtr = $imcall; 105 auto ret = (cPtr is null) ? null : new $typemap(dtype, TYPE)(cPtr, true);$excode 106 return ret; 107 } 108 %typemap(dout, excode=SWIGEXCODE) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > & { 109 void* cPtr = $imcall; 110 auto ret = (cPtr is null) ? null : new $typemap(dtype, TYPE)(cPtr, true);$excode 111 return ret; 112 } 113 %typemap(dout, excode=SWIGEXCODE) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > * { 114 void* cPtr = $imcall; 115 auto ret = (cPtr is null) ? null : new $typemap(dtype, TYPE)(cPtr, true);$excode 116 return ret; 117 } 118 %typemap(dout, excode=SWIGEXCODE) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *& { 119 void* cPtr = $imcall; 120 auto ret = (cPtr is null) ? null : new $typemap(dtype, TYPE)(cPtr, true);$excode 121 return ret; 122 } 123 124 125 %typemap(dout, excode=SWIGEXCODE) CONST TYPE { 126 auto ret = new $typemap(dtype, TYPE)($imcall, true);$excode 127 return ret; 128 } 129 %typemap(dout, excode=SWIGEXCODE) CONST TYPE & { 130 auto ret = new $typemap(dtype, TYPE)($imcall, true);$excode 131 return ret; 132 } 133 %typemap(dout, excode=SWIGEXCODE) CONST TYPE * { 134 void* cPtr = $imcall; 135 auto ret = (cPtr is null) ? null : new $typemap(dtype, TYPE)(cPtr, true);$excode 136 return ret; 137 } 138 %typemap(dout, excode=SWIGEXCODE) TYPE *CONST& { 139 void* cPtr = $imcall; 140 auto ret = (cPtr is null) ? null : new $typemap(dtype, TYPE)(cPtr, true);$excode 141 return ret; 142 } 143 144 // For shared pointers, both the derived and the base class have to own their 145 // pointer; otherwise the reference count is not decreased properly on destruction. 146 %typemap(dbody) SWIGTYPE %{ 147 private void* swigCPtr; 148 private bool swigCMemOwn; 149 150 public this(void* cObject, bool ownCObject) { 151 swigCPtr = cObject; 152 swigCMemOwn = ownCObject; 153 } 154 155 public static void* swigGetCPtr($dclassname obj) { 156 return (obj is null) ? null : obj.swigCPtr; 157 } 158 %} 159 160 %typemap(dbody_derived) SWIGTYPE %{ 161 private void* swigCPtr; 162 private bool swigCMemOwn; 163 164 public this(void* cObject, bool ownCObject) { 165 super($imdmodule.$dclazznameSmartPtrUpcast(cObject), ownCObject); 166 swigCPtr = cObject; 167 swigCMemOwn = ownCObject; 168 } 169 170 public static void* swigGetCPtr($dclassname obj) { 171 return (obj is null) ? null : obj.swigCPtr; 172 } 173 %} 174 175 %typemap(ddispose, methodname="dispose", methodmodifiers="public") TYPE { 176 synchronized(this) { 177 if (swigCPtr !is null) { 178 if (swigCMemOwn) { 179 swigCMemOwn = false; 180 $imcall; 181 } 182 swigCPtr = null; 183 } 184 } 185 } 186 187 %typemap(ddispose_derived, methodname="dispose", methodmodifiers="public") TYPE { 188 synchronized(this) { 189 if (swigCPtr !is null) { 190 if (swigCMemOwn) { 191 swigCMemOwn = false; 192 $imcall; 193 } 194 swigCPtr = null; 195 super.dispose(); 196 } 197 } 198 } 199 200 %template() SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >; 201 %enddef 202