PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/Root-branch-php-utl/SWIG/Examples/python/class/index.html

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