Home | History | Annotate | Download | only in csharp
      1 // Users can provide their own SWIG_SHARED_PTR_TYPEMAPS macro before including this file to change the
      2 // visibility of the constructor and getCPtr method if desired to public if using multiple modules.
      3 #ifndef SWIG_SHARED_PTR_TYPEMAPS
      4 #define SWIG_SHARED_PTR_TYPEMAPS(CONST, TYPE...) SWIG_SHARED_PTR_TYPEMAPS_IMPLEMENTATION(internal, internal, CONST, TYPE)
      5 #endif
      6 
      7 %include <shared_ptr.i>
      8 
      9 // Language specific macro implementing all the customisations for handling the smart pointer
     10 %define SWIG_SHARED_PTR_TYPEMAPS_IMPLEMENTATION(PTRCTOR_VISIBILITY, CPTR_VISIBILITY, CONST, TYPE...)
     11 
     12 // %naturalvar is as documented for member variables
     13 %naturalvar TYPE;
     14 %naturalvar SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >;
     15 
     16 // destructor mods
     17 %feature("unref") TYPE
     18 //"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"
     19                                "(void)arg1; delete smartarg1;"
     20 
     21 // Typemap customisations...
     22 
     23 // plain value
     24 %typemap(in, canthrow=1) CONST TYPE ($&1_type argp = 0) %{
     25   argp = ((SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *)$input) ? ((SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *)$input)->get() : 0;
     26   if (!argp) {
     27     SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "Attempt to dereference null $1_type", 0);
     28     return $null;
     29   }
     30   $1 = *argp; %}
     31 %typemap(out) CONST TYPE
     32 %{ $result = new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >(new $1_ltype(($1_ltype &)$1)); %}
     33 
     34 // plain pointer
     35 %typemap(in, canthrow=1) CONST TYPE * (SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *smartarg = 0) %{
     36   smartarg = (SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *)$input;
     37   $1 = (TYPE *)(smartarg ? smartarg->get() : 0); %}
     38 %typemap(out, fragment="SWIG_null_deleter") CONST TYPE * %{
     39   $result = $1 ? new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >($1 SWIG_NO_NULL_DELETER_$owner) : 0;
     40 %}
     41 
     42 // plain reference
     43 %typemap(in, canthrow=1) CONST TYPE & %{
     44   $1 = ($1_ltype)(((SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *)$input) ? ((SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *)$input)->get() : 0);
     45   if (!$1) {
     46     SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "$1_type reference is null", 0);
     47     return $null;
     48   } %}
     49 %typemap(out, fragment="SWIG_null_deleter") CONST TYPE &
     50 %{ $result = new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >($1 SWIG_NO_NULL_DELETER_$owner); %}
     51 
     52 // plain pointer by reference
     53 %typemap(in) TYPE *CONST& ($*1_ltype temp = 0)
     54 %{ temp = (TYPE *)(((SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *)$input) ? ((SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *)$input)->get() : 0);
     55    $1 = &temp; %}
     56 %typemap(out, fragment="SWIG_null_deleter") TYPE *CONST&
     57 %{ $result = new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >(*$1 SWIG_NO_NULL_DELETER_$owner); %}
     58 
     59 // shared_ptr by value
     60 %typemap(in) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >
     61 %{ if ($input) $1 = *($&1_ltype)$input; %}
     62 %typemap(out) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >
     63 %{ $result = $1 ? new $1_ltype($1) : 0; %}
     64 
     65 // shared_ptr by reference
     66 %typemap(in, canthrow=1) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > & ($*1_ltype tempnull)
     67 %{ $1 = $input ? ($1_ltype)$input : &tempnull; %}
     68 %typemap(out) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > &
     69 %{ $result = *$1 ? new $*1_ltype(*$1) : 0; %}
     70 
     71 // shared_ptr by pointer
     72 %typemap(in) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > * ($*1_ltype tempnull)
     73 %{ $1 = $input ? ($1_ltype)$input : &tempnull; %}
     74 %typemap(out, fragment="SWIG_null_deleter") SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *
     75 %{ $result = ($1 && *$1) ? new $*1_ltype(*($1_ltype)$1) : 0;
     76    if ($owner) delete $1; %}
     77 
     78 // shared_ptr by pointer reference
     79 %typemap(in) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *& (SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > tempnull, $*1_ltype temp = 0)
     80 %{ temp = $input ? *($1_ltype)&$input : &tempnull;
     81    $1 = &temp; %}
     82 %typemap(out) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *&
     83 %{ *($1_ltype)&$result = (*$1 && **$1) ? new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >(**$1) : 0; %}
     84 
     85 // various missing typemaps - If ever used (unlikely) ensure compilation error rather than runtime bug
     86 %typemap(in) CONST TYPE[], CONST TYPE[ANY], CONST TYPE (CLASS::*) %{
     87 #error "typemaps for $1_type not available"
     88 %}
     89 %typemap(out) CONST TYPE[], CONST TYPE[ANY], CONST TYPE (CLASS::*) %{
     90 #error "typemaps for $1_type not available"
     91 %}
     92 
     93 
     94 %typemap (ctype)  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 > *,
     97                   SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *& "void *"
     98 %typemap (imtype, out="IntPtr") 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 > *& "HandleRef"
    102 %typemap (cstype) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >,
    103                   SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > &,
    104                   SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *,
    105                   SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *& "$typemap(cstype, TYPE)"
    106 
    107 %typemap(csin) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >,
    108                SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > &,
    109                SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *,
    110                SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *& "$typemap(cstype, TYPE).getCPtr($csinput)"
    111 
    112 %typemap(csout, excode=SWIGEXCODE) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > {
    113     IntPtr cPtr = $imcall;
    114     $typemap(cstype, TYPE) ret = (cPtr == IntPtr.Zero) ? null : new $typemap(cstype, TYPE)(cPtr, true);$excode
    115     return ret;
    116   }
    117 %typemap(csout, excode=SWIGEXCODE) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > & {
    118     IntPtr cPtr = $imcall;
    119     $typemap(cstype, TYPE) ret = (cPtr == IntPtr.Zero) ? null : new $typemap(cstype, TYPE)(cPtr, true);$excode
    120     return ret;
    121   }
    122 %typemap(csout, excode=SWIGEXCODE) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > * {
    123     IntPtr cPtr = $imcall;
    124     $typemap(cstype, TYPE) ret = (cPtr == IntPtr.Zero) ? null : new $typemap(cstype, TYPE)(cPtr, true);$excode
    125     return ret;
    126   }
    127 %typemap(csout, excode=SWIGEXCODE) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *& {
    128     IntPtr cPtr = $imcall;
    129     $typemap(cstype, TYPE) ret = (cPtr == IntPtr.Zero) ? null : new $typemap(cstype, TYPE)(cPtr, true);$excode
    130     return ret;
    131   }
    132 
    133 
    134 %typemap(csout, excode=SWIGEXCODE) CONST TYPE {
    135     $typemap(cstype, TYPE) ret = new $typemap(cstype, TYPE)($imcall, true);$excode
    136     return ret;
    137   }
    138 %typemap(csout, excode=SWIGEXCODE) CONST TYPE & {
    139     $typemap(cstype, TYPE) ret = new $typemap(cstype, TYPE)($imcall, true);$excode
    140     return ret;
    141   }
    142 %typemap(csout, excode=SWIGEXCODE) CONST TYPE * {
    143     IntPtr cPtr = $imcall;
    144     $typemap(cstype, TYPE) ret = (cPtr == IntPtr.Zero) ? null : new $typemap(cstype, TYPE)(cPtr, true);$excode
    145     return ret;
    146   }
    147 %typemap(csout, excode=SWIGEXCODE) TYPE *CONST& {
    148     IntPtr cPtr = $imcall;
    149     $typemap(cstype, TYPE) ret = (cPtr == IntPtr.Zero) ? null : new $typemap(cstype, TYPE)(cPtr, true);$excode
    150     return ret;
    151   }
    152 
    153 %typemap(csvarout, excode=SWIGEXCODE2) CONST TYPE & %{
    154     get {
    155       $csclassname ret = new $csclassname($imcall, true);$excode
    156       return ret;
    157     } %}
    158 %typemap(csvarout, excode=SWIGEXCODE2) CONST TYPE * %{
    159     get {
    160       IntPtr cPtr = $imcall;
    161       $csclassname ret = (cPtr == IntPtr.Zero) ? null : new $csclassname(cPtr, true);$excode
    162       return ret;
    163     } %}
    164 
    165 %typemap(csvarout, excode=SWIGEXCODE2) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > & %{
    166     get {
    167       IntPtr cPtr = $imcall;
    168       $typemap(cstype, TYPE) ret = (cPtr == IntPtr.Zero) ? null : new $typemap(cstype, TYPE)(cPtr, true);$excode
    169       return ret;
    170     } %}
    171 %typemap(csvarout, excode=SWIGEXCODE2) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > * %{
    172     get {
    173       IntPtr cPtr = $imcall;
    174       $typemap(cstype, TYPE) ret = (cPtr == IntPtr.Zero) ? null : new $typemap(cstype, TYPE)(cPtr, true);$excode
    175       return ret;
    176     } %}
    177 
    178 
    179 // Proxy classes (base classes, ie, not derived classes)
    180 %typemap(csbody) TYPE %{
    181   private HandleRef swigCPtr;
    182   private bool swigCMemOwnBase;
    183 
    184   PTRCTOR_VISIBILITY $csclassname(IntPtr cPtr, bool cMemoryOwn) {
    185     swigCMemOwnBase = cMemoryOwn;
    186     swigCPtr = new HandleRef(this, cPtr);
    187   }
    188 
    189   CPTR_VISIBILITY static HandleRef getCPtr($csclassname obj) {
    190     return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr;
    191   }
    192 %}
    193 
    194 // Derived proxy classes
    195 %typemap(csbody_derived) TYPE %{
    196   private HandleRef swigCPtr;
    197   private bool swigCMemOwnDerived;
    198 
    199   PTRCTOR_VISIBILITY $csclassname(IntPtr cPtr, bool cMemoryOwn) : base($imclassname.$csclazznameSWIGSmartPtrUpcast(cPtr), true) {
    200     swigCMemOwnDerived = cMemoryOwn;
    201     swigCPtr = new HandleRef(this, cPtr);
    202   }
    203 
    204   CPTR_VISIBILITY static HandleRef getCPtr($csclassname obj) {
    205     return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr;
    206   }
    207 %}
    208 
    209 %typemap(csdestruct, methodname="Dispose", methodmodifiers="public") TYPE {
    210     lock(this) {
    211       if (swigCPtr.Handle != IntPtr.Zero) {
    212         if (swigCMemOwnBase) {
    213           swigCMemOwnBase = false;
    214           $imcall;
    215         }
    216         swigCPtr = new HandleRef(null, IntPtr.Zero);
    217       }
    218       GC.SuppressFinalize(this);
    219     }
    220   }
    221 
    222 %typemap(csdestruct_derived, methodname="Dispose", methodmodifiers="public") TYPE {
    223     lock(this) {
    224       if (swigCPtr.Handle != IntPtr.Zero) {
    225         if (swigCMemOwnDerived) {
    226           swigCMemOwnDerived = false;
    227           $imcall;
    228         }
    229         swigCPtr = new HandleRef(null, IntPtr.Zero);
    230       }
    231       GC.SuppressFinalize(this);
    232       base.Dispose();
    233     }
    234   }
    235 
    236 %template() SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >;
    237 %enddef
    238 
    239