PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/python/class/index.html

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