/trunk/Examples/python/class/index.html
HTML | 216 lines | 169 code | 47 blank | 0 comment | 0 complexity | 8a034148e919dca45a8b19dddb637b9c MD5 | raw file
1<html> 2<head> 3<title>SWIG:Examples:python:class</title> 4</head> 5 6<body bgcolor="#ffffff"> 7 8 9<tt>SWIG/Examples/python/class/</tt> 10<hr> 11 12<H2>Wrapping a simple C++ class</H2> 13 14<p> 15This example illustrates the most primitive form of C++ class wrapping performed 16by SWIG. In this case, C++ classes are simply transformed into a collection of 17C-style functions that provide access to class members. 18 19<h2>The C++ Code</h2> 20 21Suppose you have some C++ classes described by the following (and admittedly lame) 22header file: 23 24<blockquote> 25<pre> 26/* File : example.h */ 27 28class Shape { 29public: 30 Shape() { 31 nshapes++; 32 } 33 virtual ~Shape() { 34 nshapes--; 35 }; 36 double x, y; 37 void move(double dx, double dy); 38 virtual double area() = 0; 39 virtual double perimeter() = 0; 40 static int nshapes; 41}; 42 43class Circle : public Shape { 44private: 45 double radius; 46public: 47 Circle(double r) : radius(r) { }; 48 virtual double area(); 49 virtual double perimeter(); 50}; 51 52class Square : public Shape { 53private: 54 double width; 55public: 56 Square(double w) : width(w) { }; 57 virtual double area(); 58 virtual double perimeter(); 59}; 60</pre> 61</blockquote> 62 63<h2>The SWIG interface</h2> 64 65A simple SWIG interface for this can be built by simply grabbing the header file 66like this: 67 68<blockquote> 69<pre> 70/* File : example.i */ 71%module example 72 73%{ 74#include "example.h" 75%} 76 77/* Let's just grab the original header file here */ 78%include "example.h" 79</pre> 80</blockquote> 81 82Note: when creating a C++ extension, you must run SWIG with the <tt>-c++</tt> option like this: 83<blockquote> 84<pre> 85% swig -c++ -python example.i 86</pre> 87</blockquote> 88 89<h2>A sample Python script</h2> 90 91Click <a href="example.py">here</a> to see a script that calls the C++ functions from Python. 92 93<h2>Key points</h2> 94 95<ul> 96<li>To create a new object, you call a constructor like this: 97 98<blockquote> 99<pre> 100c = example.new_Circle(10.0) 101</pre> 102</blockquote> 103 104<p> 105<li>To access member data, a pair of accessor functions are used. 106For example: 107 108<blockquote> 109<pre> 110example.Shape_x_set(c,15) # Set member data 111x = example.Shape_x_get(c) # Get member data 112</pre> 113</blockquote> 114 115Note: when accessing member data, the name of the class in which 116the member data was must be used. In this case, <tt>Shape_x_get()</tt> 117and <tt>Shape_x_set()</tt> are used since 'x' was defined in Shape. 118 119<p> 120<li>To invoke a member function, you simply do this 121 122<blockquote> 123<pre> 124print "The area is ", example.Shape_area(c) 125</pre> 126</blockquote> 127 128<p> 129<li>Type checking knows about the inheritance structure of C++. For example: 130 131<blockquote> 132<pre> 133example.Shape_area(c) # Works (c is a Shape) 134example.Circle_area(c) # Works (c is a Circle) 135example.Square_area(c) # Fails (c is definitely not a Square) 136</pre> 137</blockquote> 138 139<p> 140<li>To invoke a destructor, simply do this 141 142<blockquote> 143<pre> 144example.delete_Shape(c) # Deletes a shape 145</pre> 146</blockquote> 147 148(Note: destructors are currently not inherited. This might change later). 149 150<p> 151<li>Static member variables are wrapped as C global variables. For example: 152 153<blockquote> 154<pre> 155n = example.cvar.Shape_nshapes # Get a static data member 156example.cvar.Shapes_nshapes = 13 # Set a static data member 157</pre> 158</blockquote> 159 160</ul> 161 162<h2>General Comments</h2> 163 164<ul> 165<li>This low-level interface is not the only way to handle C++ code. 166Proxy classes provide a much higher-level interface. 167 168<p> 169<li>SWIG *does* know how to properly perform upcasting of objects in 170an inheritance hierarchy (including multiple inheritance). Therefore 171it is perfectly safe to pass an object of a derived class to any 172function involving a base class. 173 174<p> 175<li>A wide variety of C++ features are not currently supported by SWIG. Here is the 176short and incomplete list: 177 178<p> 179<ul> 180<li>Overloaded methods and functions. SWIG wrappers don't know how to resolve name 181conflicts so you must give an alternative name to any overloaded method name using the 182%name directive like this: 183 184<blockquote> 185<pre> 186void foo(int a); 187%name(foo2) void foo(double a, double b); 188</pre> 189</blockquote> 190 191<p> 192<li>Overloaded operators. Not supported at all. The only workaround for this is 193to write a helper function. For example: 194 195<blockquote> 196<pre> 197%inline %{ 198 Vector *vector_add(Vector *a, Vector *b) { 199 ... whatever ... 200 } 201%} 202</pre> 203</blockquote> 204 205<p> 206<li>Namespaces. Not supported at all. Won't be supported until SWIG2.0 (if at all). 207 208<p> 209<li>Dave's snide remark: Like a large bottle of strong Tequilla, it's better to 210use C++ in moderation. 211 212</ul> 213 214<hr> 215</body> 216</html>