/tags/rel-1-3-24/SWIG/Examples/perl5/class/index.html

# · HTML · 210 lines · 164 code · 46 blank · 0 comment · 0 complexity · 6b1942dda3c30b28c9cd8f89887a1ee1 MD5 · raw file

  1. <html>
  2. <head>
  3. <title>SWIG:Examples:perl5:class</title>
  4. </head>
  5. <body bgcolor="#ffffff">
  6. <tt>SWIG/Examples/perl5/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 Perl script</h2>
  73. Click <a href="runme.pl">here</a> to see a script that calls the C++ functions from Perl.
  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 data member is defined is used. For example <tt>Shape_x_get()</tt>.
  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. <p>
  117. <li>Static member variables are wrapped as C global variables. For example:
  118. <blockquote>
  119. <pre>
  120. $n = $example::Shape_nshapes; # Get a static data member
  121. $example::Shapes_nshapes = 13; # Set a static data member
  122. </pre>
  123. </blockquote>
  124. </ul>
  125. <h2>General Comments</h2>
  126. <ul>
  127. <li>This low-level interface is not the only way to handle C++ code. Shadow classes
  128. provide a much higher-level interface.
  129. <p>
  130. <li>SWIG *does* know how to properly perform upcasting of objects in an inheritance
  131. hierarchy (including multiple inheritance). Therefore it is perfectly safe to pass
  132. an object of a derived class to any function involving a base class.
  133. <p>
  134. <li>A wide variety of C++ features are not currently supported by SWIG. Here is the
  135. short and incomplete list:
  136. <p>
  137. <ul>
  138. <li>Overloaded methods and functions. SWIG wrappers don't know how to resolve name
  139. conflicts so you must give an alternative name to any overloaded method name using the
  140. %name directive like this:
  141. <blockquote>
  142. <pre>
  143. void foo(int a);
  144. %name(foo2) void foo(double a, double b);
  145. </pre>
  146. </blockquote>
  147. <p>
  148. <li>Overloaded operators. Not supported at all. The only workaround for this is
  149. to write a helper function. For example:
  150. <blockquote>
  151. <pre>
  152. %inline %{
  153. Vector *vector_add(Vector *a, Vector *b) {
  154. ... whatever ...
  155. }
  156. %}
  157. </pre>
  158. </blockquote>
  159. <p>
  160. <li>Namespaces. Not supported at all. Won't be supported until SWIG2.0 (if at all).
  161. </ul>
  162. <hr>
  163. </body>
  164. </html>