PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/tcl/class/index.html

#
HTML | 274 lines | 217 code | 57 blank | 0 comment | 0 complexity | 5c90d2a56c2ff3541193c400f4497d56 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:tcl:class</title>
  4. </head>
  5. <body bgcolor="#ffffff">
  6. <tt>SWIG/Examples/tcl/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++ -tcl example.i
  69. </pre>
  70. </blockquote>
  71. <h2>Some sample Tcl scripts</h2>
  72. SWIG performs two forms of C++ wrapping-- a low level interface and a high level widget-like interface.
  73. <ul>
  74. <li>
  75. Click <a href="example1.tcl">here</a> to see a script that calls the C++ functions using the
  76. low-level interface.
  77. <li>
  78. Click <a href="example2.tcl">here</a> to see a the same script written with the high-level
  79. interface.
  80. </ul>
  81. <h2>Key points</h2>
  82. <ul>
  83. <li>The low-level C++ interface works like this:
  84. <p>
  85. <ul>
  86. <li>To create a new object, you call a constructor like this:
  87. <blockquote>
  88. <pre>
  89. set c [new_Circle 10.0]
  90. </pre>
  91. </blockquote>
  92. <p>
  93. <li>To access member data, a pair of accessor functions are used.
  94. For example:
  95. <blockquote>
  96. <pre>
  97. Shape_x_set $c 15 ;# Set member data
  98. set x [Shape_x_get $c] ;# Get member data
  99. </pre>
  100. </blockquote>
  101. Note: when accessing member data, the name of the base class must
  102. be used such as <tt>Shape_x_get</tt>
  103. <p>
  104. <li>To invoke a member function, you simply do this
  105. <blockquote>
  106. <pre>
  107. puts "The area is [Shape_area $c]"
  108. </pre>
  109. </blockquote>
  110. <p>
  111. <li>Type checking knows about the inheritance structure of C++. For example:
  112. <blockquote>
  113. <pre>
  114. Shape_area $c # Works (c is a Shape)
  115. Circle_area $c # Works (c is a Circle)
  116. Square_area $c # Fails (c is definitely not a Square)
  117. </pre>
  118. </blockquote>
  119. <p>
  120. <li>To invoke a destructor, simply do this
  121. <blockquote>
  122. <pre>
  123. delete_Shape $c # Deletes a shape
  124. </pre>
  125. </blockquote>
  126. <p>
  127. <li>Static member variables are wrapped as C global variables. For example:
  128. <blockquote>
  129. <pre>
  130. set n $Shape_nshapes # Get a static data member
  131. set Shapes_nshapes 13 # Set a static data member
  132. </pre>
  133. </blockquote>
  134. </ul>
  135. <p>
  136. <li>The high-level interface works like a Tk widget
  137. <p>
  138. <ul>
  139. <li>To create a new object, you call a constructor like this:
  140. <blockquote>
  141. <pre>
  142. Circle c 10 # c becomes a name for the Circle object
  143. </pre>
  144. </blockquote>
  145. <p>
  146. <li>To access member data, use cget and configure methods.
  147. For example:
  148. <blockquote>
  149. <pre>
  150. c configure -x 15 ;# Set member data
  151. set x [c cget -x] ;# Get member data
  152. </pre>
  153. </blockquote>
  154. <p>
  155. <li>To invoke a member function, you simply do this
  156. <blockquote>
  157. <pre>
  158. puts "The area is [c area]"
  159. </pre>
  160. </blockquote>
  161. <p>
  162. <li>To invoke a destructor, simply destroy the object name like this:
  163. <blockquote>
  164. <pre>
  165. rename c "" # c goes away
  166. </pre>
  167. </blockquote>
  168. <p>
  169. <li>Static member variables are wrapped as C global variables. For example:
  170. <blockquote>
  171. <pre>
  172. set n $Shape_nshapes # Get a static data member
  173. set Shapes_nshapes 13 # Set a static data member
  174. </pre>
  175. </blockquote>
  176. </ul>
  177. </ul>
  178. <h2>General Comments</h2>
  179. <ul>
  180. <li>The low-level function interface is much faster than the high-level interface.
  181. In fact, all the higher level interface does is call functions in the low-level interface.
  182. <p>
  183. <li>SWIG *does* know how to properly perform upcasting of objects in an inheritance
  184. hierarchy (including multiple inheritance). Therefore it is perfectly safe to pass
  185. an object of a derived class to any function involving a base class.
  186. <p>
  187. <li>A wide variety of C++ features are not currently supported by SWIG. Here is the
  188. short and incomplete list:
  189. <p>
  190. <ul>
  191. <li>Overloaded methods and functions. SWIG wrappers don't know how to resolve name
  192. conflicts so you must give an alternative name to any overloaded method name using the
  193. %name directive like this:
  194. <blockquote>
  195. <pre>
  196. void foo(int a);
  197. %name(foo2) void foo(double a, double b);
  198. </pre>
  199. </blockquote>
  200. <p>
  201. <li>Overloaded operators. Not supported at all. The only workaround for this is
  202. to write a helper function. For example:
  203. <blockquote>
  204. <pre>
  205. %inline %{
  206. Vector *vector_add(Vector *a, Vector *b) {
  207. ... whatever ...
  208. }
  209. %}
  210. </pre>
  211. </blockquote>
  212. <p>
  213. <li>Namespaces. Not supported at all. Won't be supported until SWIG2.0 (if at all).
  214. </ul>
  215. <hr>
  216. </body>
  217. </html>