From: William F. <wsf...@us...> - 2006-11-08 00:52:35
|
Update of /cvsroot/swig/SWIG/Doc/Manual In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv12347/Doc/Manual Modified Files: Lua.html Python.html Ruby.html SWIG.html SWIGPlus.html Log Message: $self special variable for %extend Index: Python.html =================================================================== RCS file: /cvsroot/swig/SWIG/Doc/Manual/Python.html,v retrieving revision 1.44 retrieving revision 1.45 diff -C2 -d -r1.44 -r1.45 *** Python.html 31 Oct 2006 22:43:55 -0000 1.44 --- Python.html 8 Nov 2006 00:52:32 -0000 1.45 *************** *** 3102,3106 **** char *__str__() { static char tmp[1024]; ! sprintf(tmp,"Vector(%g,%g,%g)", self->x,self->y,self->z); return tmp; } --- 3102,3106 ---- char *__str__() { static char tmp[1024]; ! sprintf(tmp,"Vector(%g,%g,%g)", $self->x,$self->y,$self->z); return tmp; } *************** *** 3139,3145 **** Vector __add__(Vector *other) { Vector v; ! v.x = self->x + other->x; ! v.y = self->y + other->y; ! v.z = self->z + other->z; return v; } --- 3139,3145 ---- Vector __add__(Vector *other) { Vector v; ! v.x = $self->x + other->x; ! v.y = $self->y + other->y; ! v.z = $self->z + other->z; return v; } Index: SWIGPlus.html =================================================================== RCS file: /cvsroot/swig/SWIG/Doc/Manual/SWIGPlus.html,v retrieving revision 1.47 retrieving revision 1.48 diff -C2 -d -r1.47 -r1.48 *** SWIGPlus.html 31 Oct 2006 22:43:55 -0000 1.47 --- SWIGPlus.html 8 Nov 2006 00:52:32 -0000 1.48 *************** *** 2735,2739 **** char *__str__() { static char temp[256]; ! sprintf(temp,"[ %g, %g, %g ]", self->x,self->y,self->z); return &temp[0]; } --- 2735,2739 ---- char *__str__() { static char temp[256]; ! sprintf(temp,"[ %g, %g, %g ]", $self->x,$self->y,$self->z); return &temp[0]; } *************** *** 2762,2768 **** <p> The<tt> %extend</tt> directive follows all of the same conventions ! as its use with C structures. Please refer to the <a href="SWIG.html#SWIG">SWIG Basics</a> ! chapter for further details. </p> --- 2762,2795 ---- <p> + The C++ 'this' pointer is often needed to access member variables, methods etc. + The <tt>$self</tt> special variable should be used wherever you could use 'this'. + The example above demonstrates this for accessing member variables. + The implicit 'this' pointer that is present in C++ methods is not present in <tt>%extend</tt> methods. + In order to access anything in the extended class or its base class, an explicit 'this' is required. + The following example shows how one could access base class members: + </p> + + <div class="code"><pre> + struct Base { + virtual void method(int v) { + ... + } + int value; + }; + struct Derived : Base { + }; + %extend Derived { + virtual void method(int v) { + $self->Base::method(v); + $self->value = v; + ... + } + } + </pre></div> + + <p> The<tt> %extend</tt> directive follows all of the same conventions ! as its use with C structures. Please refer to the <a href="SWIG.html#SWIG_adding_member_functions">Adding member functions to C structures</a> ! section for further details. </p> *************** *** 3447,3451 **** /* Make a copy */ T *__copy__() { ! return new List<T>(*self); } }; --- 3474,3478 ---- /* Make a copy */ T *__copy__() { ! return new List<T>(*$self); } }; Index: Ruby.html =================================================================== RCS file: /cvsroot/swig/SWIG/Doc/Manual/Ruby.html,v retrieving revision 1.43 retrieving revision 1.44 diff -C2 -d -r1.43 -r1.44 *** Ruby.html 27 Sep 2006 17:25:03 -0000 1.43 --- Ruby.html 8 Nov 2006 00:52:32 -0000 1.44 *************** *** 990,994 **** example: </p> <div class="code"> ! <pre>class MyArray {<br>public:<br> // Construct an empty array<br> MyArray();<br> <br> // Return the size of this array<br> size_t length() const;<br>};<br><br>%extend MyArray {<br> // MyArray#size is an alias for MyArray#length<br> size_t size() const {<br> return self->length();<br> }<br>} </pre> </div> --- 990,994 ---- example: </p> <div class="code"> ! <pre>class MyArray {<br>public:<br> // Construct an empty array<br> MyArray();<br> <br> // Return the size of this array<br> size_t length() const;<br>};<br><br>%extend MyArray {<br> // MyArray#size is an alias for MyArray#length<br> size_t size() const {<br> return $self->length();<br> }<br>} </pre> </div> Index: SWIG.html =================================================================== RCS file: /cvsroot/swig/SWIG/Doc/Manual/SWIG.html,v retrieving revision 1.30 retrieving revision 1.31 diff -C2 -d -r1.30 -r1.31 *** SWIG.html 27 Sep 2006 17:25:03 -0000 1.30 --- SWIG.html 8 Nov 2006 00:52:32 -0000 1.31 *************** *** 2357,2367 **** } ~Vector() { ! free(self); } double magnitude() { ! return sqrt(self->x*self->x+self->y*self->y+self->z*self->z); } void print() { ! printf("Vector [%g, %g, %g]\n", self->x,self->y,self->z); } }; --- 2357,2367 ---- } ~Vector() { ! free($self); } double magnitude() { ! return sqrt($self->x*$self->x+$self->y*$self->y+$self->z*$self->z); } void print() { ! printf("Vector [%g, %g, %g]\n", $self->x,$self->y,$self->z); } }; *************** *** 2370,2373 **** --- 2370,2378 ---- <p> + Note the usage of the <tt>$self</tt> special variable. + Its usage is identical to a C++ 'this' pointer and should be used whenever access to the struct instance is required. + </p> + + <p> Now, when used with proxy classes in Python, you can do things like this :</p> Index: Lua.html =================================================================== RCS file: /cvsroot/swig/SWIG/Doc/Manual/Lua.html,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** Lua.html 4 Mar 2006 23:43:20 -0000 1.10 --- Lua.html 8 Nov 2006 00:52:31 -0000 1.11 *************** *** 825,833 **** const char *__str__() { static char tmp[1024]; ! sprintf(tmp,"Complex(%g,%g)", self->re(),self->im()); return tmp; } bool operator==(const Complex& c) ! { return (self->re()==c.re() && self->im()==c.im();} }; </pre></div> --- 825,833 ---- const char *__str__() { static char tmp[1024]; ! sprintf(tmp,"Complex(%g,%g)", $self->re(),$self->im()); return tmp; } bool operator==(const Complex& c) ! { return ($self->re()==c.re() && $self->im()==c.im();} }; </pre></div> *************** *** 847,851 **** </pre></div> <p> ! Extend works with both C and C++ code, on classes and structs. It does not modify the underlying object in any way---the extensions only show up in the Lua interface. The only item to take note of is the code has to use the 'self' instead of 'this', and that you cannot access protected/private members of the code (as you are not officially part of the class). </p> <H3><a name="Lua_nn20"></a>22.3.13 C++ templates</H3> --- 847,851 ---- </pre></div> <p> ! Extend works with both C and C++ code, on classes and structs. It does not modify the underlying object in any way---the extensions only show up in the Lua interface. The only item to take note of is the code has to use the '$self' instead of 'this', and that you cannot access protected/private members of the code (as you are not officially part of the class). </p> <H3><a name="Lua_nn20"></a>22.3.13 C++ templates</H3> |