PageRenderTime 61ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/rel-1-3-15/SWIG/Examples/java/class/index.html

#
HTML | 199 lines | 156 code | 43 blank | 0 comment | 0 complexity | a1d06e69421ab67bb198db6af3273b06 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:java:class</title>
  4. </head>
  5. <body bgcolor="#ffffff">
  6. <tt>SWIG/Examples/java/class/</tt>
  7. <hr>
  8. <H2>Wrapping a simple C++ class</H2>
  9. <tt>$Header$</tt><br>
  10. <p>
  11. This example illustrates the high level form of C++ class wrapping performed
  12. by SWIG. In this case, a C++ class has a proxy Java class, which
  13. provides access to C++ 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++ -java example.i
  70. </pre>
  71. </blockquote>
  72. <h2>A sample Java program</h2>
  73. Click <a href="main.java">here</a> to see a Java program that calls the C++ functions from Java.
  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. Circle c = new Circle(10);
  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. c.setX(15); // Set member data
  88. x = c.getX(); // Get member data
  89. </pre>
  90. </blockquote>
  91. <p>
  92. <li>To invoke a member function, you simply do this
  93. <blockquote>
  94. <pre>
  95. System.out.println( "The area is " + c.area() );
  96. </pre>
  97. </blockquote>
  98. <p>
  99. <li>To invoke a destructor, simply do this
  100. <blockquote>
  101. <pre>
  102. c.delete(); // Deletes a shape
  103. </pre>
  104. </blockquote>
  105. <p>
  106. <li>Static member variables are wrapped with java static get and set access functions. For example:
  107. <blockquote>
  108. <pre>
  109. n = Shape.getNshapes(); // Get a static data member
  110. Shape.setNshapes(13); // Set a static data member
  111. </pre>
  112. </blockquote>
  113. </ul>
  114. <h2>General Comments</h2>
  115. <ul>
  116. <li>This high-level interface using shadow classes is not the only way to handle C++ code.
  117. A low level interface using c functions to access member variables and member functions is the alternative SWIG
  118. approach. This entails passing around the c pointer or c++ 'this' pointer and as such it is not difficult to crash the JVM.
  119. The abstraction of the underlying pointer by the java shadow classes far better fits the java programming paradigm.
  120. <p>
  121. <li>SWIG *does* know how to properly perform upcasting of objects in an inheritance
  122. hierarchy (including multiple inheritance). However Java classes can only derive from one base class so multiple inheritance
  123. is not implemented. Java classes can implement more than one interface so there is scope for improvement in the future.
  124. <p>
  125. <li>A wide variety of C++ features are not currently supported by SWIG. Here is the
  126. short and incomplete list:
  127. <p>
  128. <ul>
  129. <li>Overloaded methods and functions. SWIG wrappers don't know how to resolve name
  130. conflicts so you must give an alternative name to any overloaded method name using the
  131. %name directive like this:
  132. <blockquote>
  133. <pre>
  134. void foo(int a);
  135. %name(foo2) void foo(double a, double b);
  136. </pre>
  137. </blockquote>
  138. <p>
  139. <li>Overloaded operators. Not supported at all. The only workaround for this is
  140. to write a helper function. For example:
  141. <blockquote>
  142. <pre>
  143. %inline %{
  144. Vector *vector_add(Vector *a, Vector *b) {
  145. ... whatever ...
  146. }
  147. %}
  148. </pre>
  149. </blockquote>
  150. <p>
  151. <li>Namespaces. Not supported at all. Won't be supported until SWIG2.0 (if at all).
  152. </ul>
  153. </ul>
  154. <hr>
  155. </body>
  156. </html>