PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/perl5/class/index.html

#
HTML | 208 lines | 163 code | 45 blank | 0 comment | 0 complexity | b9f03cb0c22119e26f9d571c7a49c7a1 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: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. <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 Perl script</h2>
  72. Click <a href="runme.pl">here</a> to see a script that calls the C++ functions from Perl.
  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 data member is defined is used. For example <tt>Shape_x_get()</tt>.
  92. <p>
  93. <li>To invoke a member function, you simply do this
  94. <blockquote>
  95. <pre>
  96. print "The area is ", example::Shape_area($c);
  97. </pre>
  98. </blockquote>
  99. <p>
  100. <li>Type checking knows about the inheritance structure of C++. For example:
  101. <blockquote>
  102. <pre>
  103. example::Shape_area($c); # Works (c is a Shape)
  104. example::Circle_area($c); # Works (c is a Circle)
  105. example::Square_area($c); # Fails (c is definitely not a Square)
  106. </pre>
  107. </blockquote>
  108. <p>
  109. <li>To invoke a destructor, simply do this
  110. <blockquote>
  111. <pre>
  112. example::delete_Shape($c); # Deletes a shape
  113. </pre>
  114. </blockquote>
  115. <p>
  116. <li>Static member variables are wrapped as C global variables. For example:
  117. <blockquote>
  118. <pre>
  119. $n = $example::Shape_nshapes; # Get a static data member
  120. $example::Shapes_nshapes = 13; # Set a static data member
  121. </pre>
  122. </blockquote>
  123. </ul>
  124. <h2>General Comments</h2>
  125. <ul>
  126. <li>This low-level interface is not the only way to handle C++ code. Proxy classes
  127. provide a much higher-level interface.
  128. <p>
  129. <li>SWIG *does* know how to properly perform upcasting of objects in an inheritance
  130. hierarchy (including multiple inheritance). Therefore it is perfectly safe to pass
  131. an object of a derived class to any function involving a base class.
  132. <p>
  133. <li>A wide variety of C++ features are not currently supported by SWIG. Here is the
  134. short and incomplete list:
  135. <p>
  136. <ul>
  137. <li>Overloaded methods and functions. SWIG wrappers don't know how to resolve name
  138. conflicts so you must give an alternative name to any overloaded method name using the
  139. %name directive like this:
  140. <blockquote>
  141. <pre>
  142. void foo(int a);
  143. %name(foo2) void foo(double a, double b);
  144. </pre>
  145. </blockquote>
  146. <p>
  147. <li>Overloaded operators. Not supported at all. The only workaround for this is
  148. to write a helper function. For example:
  149. <blockquote>
  150. <pre>
  151. %inline %{
  152. Vector *vector_add(Vector *a, Vector *b) {
  153. ... whatever ...
  154. }
  155. %}
  156. </pre>
  157. </blockquote>
  158. <p>
  159. <li>Namespaces. Not supported at all. Won't be supported until SWIG2.0 (if at all).
  160. </ul>
  161. <hr>
  162. </body>
  163. </html>