PageRenderTime 61ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/java/class/index.html

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