diff --git a/Doc/Manual/Typemaps.html b/Doc/Manual/Typemaps.html index 8f3035dc8..c1e4340f2 100644 --- a/Doc/Manual/Typemaps.html +++ b/Doc/Manual/Typemaps.html @@ -75,6 +75,7 @@
  • More about %apply and %clear
  • Reducing wrapper code size
  • Passing data between typemaps +
  • C++ this pointer
  • Where to go for more information? @@ -3899,8 +3900,65 @@ sure that the typemaps sharing information have exactly the same types and names

    -

    10.15 Where to go for more information?

    +

    10.15 C++ "this" pointer

    +

    +All the rules discussed for Typemaps apply to C++ as well as C. +However in addition C++ passes an extra parameter into every +non-static class method -- the this pointer. Occasionally it can be +useful to apply a typemap to this pointer (for example to check +and make sure this is non-null before deferencing). +Actually, C also has an the equivalent of the this pointer which is used +when accessing variables in a C struct. +

    +

    +In order to customise the this pointer handling, target a variable named self in your typemaps. +self is the name SWIG uses to refer to the extra parameter in wrapped functions. +

    +

    +For example, if wrapping for Java generation: +

    + +
    +
    +%typemap(check) SWIGTYPE *self %{
    +if (!$1) {
    +  SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "swigCPtr null");
    +  return $null;
    +}
    +%}
    +
    +
    + +

    +In the above case, the $1 variable is expanded into the argument +name that SWIG is using as the this pointer. + +SWIG will then insert the check code before the actual C++ class method +is called, and will raise an exception rather than crash +the Java virtual machine. + +The generated code will look something like: +

    + +
    +
    +  if (!arg1) {
    +    SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException,
    +      "invalid native object; delete() likely already called");
    +    return ;
    +  }
    +  (arg1)->wrappedFunction(...);
    +
    +
    + +

    +Note that if you have a parameter named self then it +will also match the typemap. One work around is to create an interface file that wraps +the method, but give the argument a name other than self. +

    + +

    10.16 Where to go for more information?

    The