Google

SWIG/Examples/java/class/

Wrapping a simple C++ class

$Header: /cvs/projects/SWIG/Examples/java/class/Attic/index.html,v 1.1.2.2.2.3 2002/08/08 19:28:59 cheetah Exp $

This example illustrates the high level form of C++ class wrapping performed by SWIG. In this case, a C++ class has a proxy Java class, which provides access to C++ class members.

The C++ Code

Suppose you have some C++ classes described by the following (and admittedly lame) header file:
/* File : example.h */

class Shape {
public:
  Shape() {
    nshapes++;
  }
  virtual ~Shape() {
    nshapes--;
  };
  double  x, y;   
  void    move(double dx, double dy);
  virtual double area() = 0;
  virtual double perimeter() = 0;
  static  int nshapes;
};

class Circle : public Shape {
private:
  double radius;
public:
  Circle(double r) : radius(r) { };
  virtual double area();
  virtual double perimeter();
};

class Square : public Shape {
private:
  double width;
public:
  Square(double w) : width(w) { };
  virtual double area();
  virtual double perimeter();
};

The SWIG interface

A simple SWIG interface for this can be built by simply grabbing the header file like this:
/* File : example.i */
%module example

%{
#include "example.h"
%}

/* Let's just grab the original header file here */
%include "example.h"
Note: when creating a C++ extension, you must run SWIG with the -c++ option like this:
% swig -c++ -java example.i

A sample Java program

Click here to see a Java program that calls the C++ functions from Java.

Key points

  • To create a new object, you call a constructor like this:
    Circle c = new Circle(10);
    

  • To access member data, a pair of accessor functions are used. For example:
    c.setX(15);        // Set member data
    x = c.getX();      // Get member data
    

  • To invoke a member function, you simply do this
    System.out.println( "The area is " + c.area() );
    

  • To invoke a destructor, simply do this
    c.delete();     // Deletes a shape
    

  • Static member variables are wrapped with java static get and set access functions. For example:
    n = Shape.getNshapes();     // Get a static data member
    Shape.setNshapes(13);       // Set a static data member
    

General Comments

  • This high-level interface using shadow classes is not the only way to handle C++ code. A low level interface using c functions to access member variables and member functions is the alternative SWIG approach. This entails passing around the c pointer or c++ 'this' pointer and as such it is not difficult to crash the JVM. The abstraction of the underlying pointer by the java shadow classes far better fits the java programming paradigm.

  • SWIG *does* know how to properly perform upcasting of objects in an inheritance hierarchy (including multiple inheritance). However Java classes can only derive from one base class so multiple inheritance is not implemented. Java classes can implement more than one interface so there is scope for improvement in the future.

  • A wide variety of C++ features are not currently supported by SWIG. Here is the short and incomplete list:

    • Overloaded methods and functions. SWIG wrappers don't know how to resolve name conflicts so you must give an alternative name to any overloaded method name using the %name directive like this:
      void foo(int a);  
      %name(foo2) void foo(double a, double b);
      

    • Overloaded operators. Not supported at all. The only workaround for this is to write a helper function. For example:
      %inline %{
          Vector *vector_add(Vector *a, Vector *b) {
                ... whatever ...
          }
      %}
      

    • Namespaces. Not supported at all. Won't be supported until SWIG2.0 (if at all).