PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/ruby/class/index.html

#
HTML | 199 lines | 156 code | 43 blank | 0 comment | 0 complexity | abdc42434a37bff22a4fa2eb8ce6b395 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:ruby:class</title>
  4. </head>
  5. <body bgcolor="#ffffff">
  6. <tt>SWIG/Examples/ruby/class/</tt>
  7. <hr>
  8. <H2>Wrapping a simple C++ class</H2>
  9. <p>
  10. This example illustrates C++ class wrapping performed by SWIG.
  11. C++ classes are simply transformed into Ruby classes that provide methods to
  12. access 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++ -ruby example.i
  69. </pre>
  70. </blockquote>
  71. <h2>A sample Ruby script</h2>
  72. Click <a href="runme.rb">here</a> to see a script that calls the C++ functions from Ruby.
  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::Circle.new(10)
  79. </pre>
  80. </blockquote>
  81. <p>
  82. <li>To access member data, a pair of accessor methods are used.
  83. For example:
  84. <blockquote>
  85. <pre>
  86. c.x = 15 # Set member data
  87. x = c.x # Get member data
  88. </pre>
  89. </blockquote>
  90. <p>
  91. <li>To invoke a member function, you simply do this
  92. <blockquote>
  93. <pre>
  94. print "The area is ", c.area, "\n"
  95. </pre>
  96. </blockquote>
  97. <p>
  98. <li>When a instance of Ruby level wrapper class is garbage collected by
  99. Ruby interpreter, the corresponding C++ destructor is automatically invoked.
  100. (Note: destructors are currently not inherited. This might change later.
  101. Until then, use <tt>-make_default</tt>).
  102. <p>
  103. <li>Static member variables are wrapped as Ruby class accessor methods.
  104. For example:
  105. <blockquote>
  106. <pre>
  107. n = Shape.nshapes # Get a static data member
  108. Shapes.nshapes = 13 # Set a static data member
  109. </pre>
  110. </blockquote>
  111. </ul>
  112. <h2>General Comments</h2>
  113. <ul>
  114. <li>Ruby module of SWIG differs from other language modules in wrapping C++
  115. interfaces. They provides lower-level interfaces and optional higher-level
  116. interfaces know as proxy classes. Ruby module needs no such redundancy
  117. due to Ruby's sophisticated extension API.
  118. <p>
  119. <li>SWIG *does* know how to properly perform upcasting of objects in
  120. an inheritance hierarchy except for multiple inheritance.
  121. <p>
  122. <li>A wide variety of C++ features are not currently supported by SWIG. Here is the
  123. short and incomplete list:
  124. <p>
  125. <ul>
  126. <li>Overloaded methods and functions. SWIG wrappers don't know how to resolve name
  127. conflicts so you must give an alternative name to any overloaded method name using the
  128. %name directive like this:
  129. <blockquote>
  130. <pre>
  131. void foo(int a);
  132. %name(foo2) void foo(double a, double b);
  133. </pre>
  134. </blockquote>
  135. <p>
  136. <li>Overloaded operators. Not supported at all. The only workaround for this is
  137. to write a helper function. For example:
  138. <blockquote>
  139. <pre>
  140. %inline %{
  141. Vector *vector_add(Vector *a, Vector *b) {
  142. ... whatever ...
  143. }
  144. %}
  145. </pre>
  146. </blockquote>
  147. <p>
  148. <li>Namespaces. Not supported at all. Won't be supported until SWIG2.0 (if at all).
  149. </ul>
  150. <p>
  151. <li>Dave's snide remark: Like a large bottle of strong Tequilla, it's better to
  152. use C++ in moderation.
  153. </ul>
  154. <hr>
  155. </body>
  156. </html>