Improve the class example for several languages.
Fix numerous inaccuracies in index.html (where it exists) and eliminate unnecessary differences between the example code being wrapped.
This commit is contained in:
parent
2f3bf144c6
commit
34c97ffdbd
23 changed files with 154 additions and 412 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
/* File : example.c */
|
/* File : example.cxx */
|
||||||
|
|
||||||
#include "example.h"
|
#include "example.h"
|
||||||
#define M_PI 3.14159265358979323846
|
#define M_PI 3.14159265358979323846
|
||||||
|
|
@ -11,18 +11,18 @@ void Shape::move(double dx, double dy) {
|
||||||
|
|
||||||
int Shape::nshapes = 0;
|
int Shape::nshapes = 0;
|
||||||
|
|
||||||
double Circle::area(void) {
|
double Circle::area() {
|
||||||
return M_PI*radius*radius;
|
return M_PI*radius*radius;
|
||||||
}
|
}
|
||||||
|
|
||||||
double Circle::perimeter(void) {
|
double Circle::perimeter() {
|
||||||
return 2*M_PI*radius;
|
return 2*M_PI*radius;
|
||||||
}
|
}
|
||||||
|
|
||||||
double Square::area(void) {
|
double Square::area() {
|
||||||
return width*width;
|
return width*width;
|
||||||
}
|
}
|
||||||
|
|
||||||
double Square::perimeter(void) {
|
double Square::perimeter() {
|
||||||
return 4*width;
|
return 4*width;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,11 +7,11 @@ public:
|
||||||
}
|
}
|
||||||
virtual ~Shape() {
|
virtual ~Shape() {
|
||||||
nshapes--;
|
nshapes--;
|
||||||
};
|
}
|
||||||
double x, y;
|
double x, y;
|
||||||
void move(double dx, double dy);
|
void move(double dx, double dy);
|
||||||
virtual double area(void) = 0;
|
virtual double area() = 0;
|
||||||
virtual double perimeter(void) = 0;
|
virtual double perimeter() = 0;
|
||||||
static int nshapes;
|
static int nshapes;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -19,21 +19,16 @@ class Circle : public Shape {
|
||||||
private:
|
private:
|
||||||
double radius;
|
double radius;
|
||||||
public:
|
public:
|
||||||
Circle(double r) : radius(r) { };
|
Circle(double r) : radius(r) { }
|
||||||
virtual double area(void);
|
virtual double area();
|
||||||
virtual double perimeter(void);
|
virtual double perimeter();
|
||||||
};
|
};
|
||||||
|
|
||||||
class Square : public Shape {
|
class Square : public Shape {
|
||||||
private:
|
private:
|
||||||
double width;
|
double width;
|
||||||
public:
|
public:
|
||||||
Square(double w) : width(w) { };
|
Square(double w) : width(w) { }
|
||||||
virtual double area(void);
|
virtual double area();
|
||||||
virtual double perimeter(void);
|
virtual double perimeter();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,8 +32,8 @@ public:
|
||||||
}
|
}
|
||||||
virtual ~Shape() {
|
virtual ~Shape() {
|
||||||
nshapes--;
|
nshapes--;
|
||||||
};
|
}
|
||||||
double x, y;
|
double x, y;
|
||||||
void move(double dx, double dy);
|
void move(double dx, double dy);
|
||||||
virtual double area() = 0;
|
virtual double area() = 0;
|
||||||
virtual double perimeter() = 0;
|
virtual double perimeter() = 0;
|
||||||
|
|
@ -44,7 +44,7 @@ class Circle : public Shape {
|
||||||
private:
|
private:
|
||||||
double radius;
|
double radius;
|
||||||
public:
|
public:
|
||||||
Circle(double r) : radius(r) { };
|
Circle(double r) : radius(r) { }
|
||||||
virtual double area();
|
virtual double area();
|
||||||
virtual double perimeter();
|
virtual double perimeter();
|
||||||
};
|
};
|
||||||
|
|
@ -53,7 +53,7 @@ class Square : public Shape {
|
||||||
private:
|
private:
|
||||||
double width;
|
double width;
|
||||||
public:
|
public:
|
||||||
Square(double w) : width(w) { };
|
Square(double w) : width(w) { }
|
||||||
virtual double area();
|
virtual double area();
|
||||||
virtual double perimeter();
|
virtual double perimeter();
|
||||||
};
|
};
|
||||||
|
|
@ -146,50 +146,15 @@ Shape.setNshapes(13); // Set a static data member
|
||||||
|
|
||||||
<ul>
|
<ul>
|
||||||
<li>This high-level interface using proxy classes is not the only way to handle C++ code.
|
<li>This high-level interface using proxy 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
|
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.
|
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 proxy classes far better fits the java programming paradigm.
|
The abstraction of the underlying pointer by the java proxy classes far better fits the java programming paradigm.
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<li>SWIG *does* know how to properly perform upcasting of objects in an inheritance
|
<li>SWIG <b>does</b> 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
|
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.
|
is not implemented. Java classes can implement more than one interface so there is scope for improvement in the future.
|
||||||
|
|
||||||
<p>
|
|
||||||
<li>A wide variety of C++ features are not currently supported by SWIG. Here is the
|
|
||||||
short and incomplete list:
|
|
||||||
|
|
||||||
<p>
|
|
||||||
<ul>
|
|
||||||
<li>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:
|
|
||||||
|
|
||||||
<blockquote>
|
|
||||||
<pre>
|
|
||||||
void foo(int a);
|
|
||||||
%name(foo2) void foo(double a, double b);
|
|
||||||
</pre>
|
|
||||||
</blockquote>
|
|
||||||
|
|
||||||
<p>
|
|
||||||
<li>Overloaded operators. Not supported at all. The only workaround for this is
|
|
||||||
to write a helper function. For example:
|
|
||||||
|
|
||||||
<blockquote>
|
|
||||||
<pre>
|
|
||||||
%inline %{
|
|
||||||
Vector *vector_add(Vector *a, Vector *b) {
|
|
||||||
... whatever ...
|
|
||||||
}
|
|
||||||
%}
|
|
||||||
</pre>
|
|
||||||
</blockquote>
|
|
||||||
|
|
||||||
<p>
|
|
||||||
<li>Namespaces. Not supported at all. Won't be supported until SWIG2.0 (if at all).
|
|
||||||
|
|
||||||
</ul>
|
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<hr>
|
<hr>
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
/* File : example.c */
|
/* File : example.cxx */
|
||||||
|
|
||||||
#include "example.h"
|
#include "example.h"
|
||||||
#define M_PI 3.14159265358979323846
|
#define M_PI 3.14159265358979323846
|
||||||
|
|
@ -11,18 +11,18 @@ void Shape::move(double dx, double dy) {
|
||||||
|
|
||||||
int Shape::nshapes = 0;
|
int Shape::nshapes = 0;
|
||||||
|
|
||||||
double Circle::area(void) {
|
double Circle::area() {
|
||||||
return M_PI*radius*radius;
|
return M_PI*radius*radius;
|
||||||
}
|
}
|
||||||
|
|
||||||
double Circle::perimeter(void) {
|
double Circle::perimeter() {
|
||||||
return 2*M_PI*radius;
|
return 2*M_PI*radius;
|
||||||
}
|
}
|
||||||
|
|
||||||
double Square::area(void) {
|
double Square::area() {
|
||||||
return width*width;
|
return width*width;
|
||||||
}
|
}
|
||||||
|
|
||||||
double Square::perimeter(void) {
|
double Square::perimeter() {
|
||||||
return 4*width;
|
return 4*width;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,11 +7,11 @@ public:
|
||||||
}
|
}
|
||||||
virtual ~Shape() {
|
virtual ~Shape() {
|
||||||
nshapes--;
|
nshapes--;
|
||||||
};
|
}
|
||||||
double x, y;
|
double x, y;
|
||||||
void move(double dx, double dy);
|
void move(double dx, double dy);
|
||||||
virtual double area(void) = 0;
|
virtual double area() = 0;
|
||||||
virtual double perimeter(void) = 0;
|
virtual double perimeter() = 0;
|
||||||
static int nshapes;
|
static int nshapes;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -19,29 +19,24 @@ class Circle : public Shape {
|
||||||
private:
|
private:
|
||||||
double radius;
|
double radius;
|
||||||
public:
|
public:
|
||||||
Circle(double r) : radius(r) { };
|
Circle(double r) : radius(r) { }
|
||||||
virtual double area(void);
|
virtual double area();
|
||||||
virtual double perimeter(void);
|
virtual double perimeter();
|
||||||
};
|
};
|
||||||
|
|
||||||
class Square : public Shape {
|
class Square : public Shape {
|
||||||
private:
|
private:
|
||||||
double width;
|
double width;
|
||||||
public:
|
public:
|
||||||
Square(double w) : width(w) { };
|
Square(double w) : width(w) { }
|
||||||
virtual double area(void);
|
virtual double area();
|
||||||
virtual double perimeter(void);
|
virtual double perimeter();
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef Square TSquare;
|
typedef Square TSquare;
|
||||||
class CFoo
|
class CFoo
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static Square MakeSquare(void) {return Square(4.0);};
|
static Square MakeSquare(void) {return Square(4.0);}
|
||||||
static TSquare MakeTSquare(void) {return Square(4.0);};
|
static TSquare MakeTSquare(void) {return Square(4.0);}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,5 @@
|
||||||
%}
|
%}
|
||||||
|
|
||||||
/* Let's just grab the original header file here */
|
/* Let's just grab the original header file here */
|
||||||
|
|
||||||
%include "example.h"
|
%include "example.h"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,8 +32,8 @@ public:
|
||||||
}
|
}
|
||||||
virtual ~Shape() {
|
virtual ~Shape() {
|
||||||
nshapes--;
|
nshapes--;
|
||||||
};
|
}
|
||||||
double x, y;
|
double x, y;
|
||||||
void move(double dx, double dy);
|
void move(double dx, double dy);
|
||||||
virtual double area() = 0;
|
virtual double area() = 0;
|
||||||
virtual double perimeter() = 0;
|
virtual double perimeter() = 0;
|
||||||
|
|
@ -44,7 +44,7 @@ class Circle : public Shape {
|
||||||
private:
|
private:
|
||||||
double radius;
|
double radius;
|
||||||
public:
|
public:
|
||||||
Circle(double r) : radius(r) { };
|
Circle(double r) : radius(r) { }
|
||||||
virtual double area();
|
virtual double area();
|
||||||
virtual double perimeter();
|
virtual double perimeter();
|
||||||
};
|
};
|
||||||
|
|
@ -53,7 +53,7 @@ class Square : public Shape {
|
||||||
private:
|
private:
|
||||||
double width;
|
double width;
|
||||||
public:
|
public:
|
||||||
Square(double w) : width(w) { };
|
Square(double w) : width(w) { }
|
||||||
virtual double area();
|
virtual double area();
|
||||||
virtual double perimeter();
|
virtual double perimeter();
|
||||||
};
|
};
|
||||||
|
|
@ -82,7 +82,7 @@ like this:
|
||||||
Note: when creating a C++ extension, you must run SWIG with the <tt>-c++</tt> option like this:
|
Note: when creating a C++ extension, you must run SWIG with the <tt>-c++</tt> option like this:
|
||||||
<blockquote>
|
<blockquote>
|
||||||
<pre>
|
<pre>
|
||||||
% swig -c++ -python example.i
|
% swig -c++ -perl example.i
|
||||||
</pre>
|
</pre>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
|
|
||||||
|
|
@ -97,60 +97,45 @@ Click <a href="runme.pl">here</a> to see a script that calls the C++ functions f
|
||||||
|
|
||||||
<blockquote>
|
<blockquote>
|
||||||
<pre>
|
<pre>
|
||||||
$c = example::new_Circle(10.0);
|
$c = new example::Circle(10.0);
|
||||||
</pre>
|
</pre>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<li>To access member data, a pair of accessor functions are used.
|
<li>You can access member data like so:
|
||||||
For example:
|
|
||||||
|
|
||||||
<blockquote>
|
<blockquote>
|
||||||
<pre>
|
<pre>
|
||||||
example::Shape_x_set($c,15); # Set member data
|
$c->{x} = 15; # Set member data
|
||||||
$x = example::Shape_x_get($c); # Get member data
|
$x = $c->{x}; # Get member data
|
||||||
</pre>
|
|
||||||
</blockquote>
|
|
||||||
|
|
||||||
Note: when accessing member data, the name of the class in which
|
|
||||||
the data member is defined is used. For example <tt>Shape_x_get()</tt>.
|
|
||||||
|
|
||||||
<p>
|
|
||||||
<li>To invoke a member function, you simply do this
|
|
||||||
|
|
||||||
<blockquote>
|
|
||||||
<pre>
|
|
||||||
print "The area is ", example::Shape_area($c);
|
|
||||||
</pre>
|
</pre>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<li>Type checking knows about the inheritance structure of C++. For example:
|
<li>To invoke a member function, you simply do this:
|
||||||
|
|
||||||
<blockquote>
|
<blockquote>
|
||||||
<pre>
|
<pre>
|
||||||
example::Shape_area($c); # Works (c is a Shape)
|
print "The area is ", $c->area();
|
||||||
example::Circle_area($c); # Works (c is a Circle)
|
|
||||||
example::Square_area($c); # Fails (c is definitely not a Square)
|
|
||||||
</pre>
|
</pre>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<li>To invoke a destructor, simply do this
|
<li>To invoke a destructor, simply do this:
|
||||||
|
|
||||||
<blockquote>
|
<blockquote>
|
||||||
<pre>
|
<pre>
|
||||||
example::delete_Shape($c); # Deletes a shape
|
$c->DESTROY(); # Deletes a shape
|
||||||
</pre>
|
</pre>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<li>Static member variables are wrapped as C global variables. For example:
|
<li>Static member variables are wrapped like so:
|
||||||
|
|
||||||
<blockquote>
|
<blockquote>
|
||||||
<pre>
|
<pre>
|
||||||
$n = $example::Shape_nshapes; # Get a static data member
|
$n = $example::Shape::nshapes; # Get a static data member
|
||||||
$example::Shapes_nshapes = 13; # Set a static data member
|
$example::Shapes::nshapes = 13; # Set a static data member
|
||||||
</pre>
|
</pre>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
|
|
||||||
|
|
@ -159,47 +144,11 @@ $example::Shapes_nshapes = 13; # Set a static data member
|
||||||
<h2>General Comments</h2>
|
<h2>General Comments</h2>
|
||||||
|
|
||||||
<ul>
|
<ul>
|
||||||
<li>This low-level interface is not the only way to handle C++ code. Proxy classes
|
<li>SWIG <b>does</b> know how to properly perform upcasting of objects in an inheritance
|
||||||
provide a much higher-level interface.
|
|
||||||
|
|
||||||
<p>
|
|
||||||
<li>SWIG *does* know how to properly perform upcasting of objects in an inheritance
|
|
||||||
hierarchy (including multiple inheritance). Therefore it is perfectly safe to pass
|
hierarchy (including multiple inheritance). Therefore it is perfectly safe to pass
|
||||||
an object of a derived class to any function involving a base class.
|
an object of a derived class to any function involving a base class.
|
||||||
|
|
||||||
<p>
|
<li>C++ Namespaces - %nspace isn't yet supported for Perl.
|
||||||
<li>A wide variety of C++ features are not currently supported by SWIG. Here is the
|
|
||||||
short and incomplete list:
|
|
||||||
|
|
||||||
<p>
|
|
||||||
<ul>
|
|
||||||
<li>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:
|
|
||||||
|
|
||||||
<blockquote>
|
|
||||||
<pre>
|
|
||||||
void foo(int a);
|
|
||||||
%name(foo2) void foo(double a, double b);
|
|
||||||
</pre>
|
|
||||||
</blockquote>
|
|
||||||
|
|
||||||
<p>
|
|
||||||
<li>Overloaded operators. Not supported at all. The only workaround for this is
|
|
||||||
to write a helper function. For example:
|
|
||||||
|
|
||||||
<blockquote>
|
|
||||||
<pre>
|
|
||||||
%inline %{
|
|
||||||
Vector *vector_add(Vector *a, Vector *b) {
|
|
||||||
... whatever ...
|
|
||||||
}
|
|
||||||
%}
|
|
||||||
</pre>
|
|
||||||
</blockquote>
|
|
||||||
|
|
||||||
<p>
|
|
||||||
<li>Namespaces. Not supported at all. Won't be supported until SWIG2.0 (if at all).
|
|
||||||
|
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ foreach $o ($c,$s) {
|
||||||
print " $o\n";
|
print " $o\n";
|
||||||
print " area = ", $o->area(), "\n";
|
print " area = ", $o->area(), "\n";
|
||||||
print " perimeter = ", $o->perimeter(), "\n";
|
print " perimeter = ", $o->perimeter(), "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
# ----- Delete everything -----
|
# ----- Delete everything -----
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,7 @@
|
||||||
/* File : example.c */
|
/* File : example.cxx */
|
||||||
|
|
||||||
#include "example.h"
|
#include "example.h"
|
||||||
#include <math.h>
|
#define M_PI 3.14159265358979323846
|
||||||
#ifndef M_PI
|
|
||||||
# define M_PI 3.14159265358979323846
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int Shape::get_nshapes() {
|
|
||||||
return nshapes;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Move the shape to a new location */
|
/* Move the shape to a new location */
|
||||||
void Shape::move(double dx, double dy) {
|
void Shape::move(double dx, double dy) {
|
||||||
|
|
@ -18,22 +11,18 @@ void Shape::move(double dx, double dy) {
|
||||||
|
|
||||||
int Shape::nshapes = 0;
|
int Shape::nshapes = 0;
|
||||||
|
|
||||||
void Circle::set_radius( double r ) {
|
double Circle::area() {
|
||||||
radius = r;
|
|
||||||
}
|
|
||||||
|
|
||||||
double Circle::area(void) {
|
|
||||||
return M_PI*radius*radius;
|
return M_PI*radius*radius;
|
||||||
}
|
}
|
||||||
|
|
||||||
double Circle::perimeter(void) {
|
double Circle::perimeter() {
|
||||||
return 2*M_PI*radius;
|
return 2*M_PI*radius;
|
||||||
}
|
}
|
||||||
|
|
||||||
double Square::area(void) {
|
double Square::area() {
|
||||||
return width*width;
|
return width*width;
|
||||||
}
|
}
|
||||||
|
|
||||||
double Square::perimeter(void) {
|
double Square::perimeter() {
|
||||||
return 4*width;
|
return 4*width;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,10 +10,9 @@ public:
|
||||||
}
|
}
|
||||||
double x, y;
|
double x, y;
|
||||||
void move(double dx, double dy);
|
void move(double dx, double dy);
|
||||||
virtual double area(void) = 0;
|
virtual double area() = 0;
|
||||||
virtual double perimeter(void) = 0;
|
virtual double perimeter() = 0;
|
||||||
static int nshapes;
|
static int nshapes;
|
||||||
static int get_nshapes();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class Circle : public Shape {
|
class Circle : public Shape {
|
||||||
|
|
@ -21,10 +20,8 @@ private:
|
||||||
double radius;
|
double radius;
|
||||||
public:
|
public:
|
||||||
Circle(double r) : radius(r) { }
|
Circle(double r) : radius(r) { }
|
||||||
~Circle() { }
|
virtual double area();
|
||||||
void set_radius( double r );
|
virtual double perimeter();
|
||||||
virtual double area(void);
|
|
||||||
virtual double perimeter(void);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class Square : public Shape {
|
class Square : public Shape {
|
||||||
|
|
@ -32,7 +29,6 @@ private:
|
||||||
double width;
|
double width;
|
||||||
public:
|
public:
|
||||||
Square(double w) : width(w) { }
|
Square(double w) : width(w) { }
|
||||||
~Square() { }
|
virtual double area();
|
||||||
virtual double area(void);
|
virtual double perimeter();
|
||||||
virtual double perimeter(void);
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ print " Created square\n";
|
||||||
|
|
||||||
# ----- Access a static member -----
|
# ----- Access a static member -----
|
||||||
|
|
||||||
print "\nA total of " . Shape::get_nshapes() . " shapes were created\n";
|
print "\nA total of " . Shape::nshapes() . " shapes were created\n";
|
||||||
|
|
||||||
# ----- Member data access -----
|
# ----- Member data access -----
|
||||||
|
|
||||||
|
|
@ -54,7 +54,7 @@ $s = NULL;
|
||||||
# the square.
|
# the square.
|
||||||
$o = NULL;
|
$o = NULL;
|
||||||
|
|
||||||
print Shape::get_nshapes() . " shapes remain\n";
|
print Shape::nshapes() . " shapes remain\n";
|
||||||
print "Goodbye\n";
|
print "Goodbye\n";
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
/* File : example.c */
|
/* File : example.cxx */
|
||||||
|
|
||||||
#include "example.h"
|
#include "example.h"
|
||||||
#define M_PI 3.14159265358979323846
|
#define M_PI 3.14159265358979323846
|
||||||
|
|
@ -11,18 +11,18 @@ void Shape::move(double dx, double dy) {
|
||||||
|
|
||||||
int Shape::nshapes = 0;
|
int Shape::nshapes = 0;
|
||||||
|
|
||||||
double Circle::area(void) {
|
double Circle::area() {
|
||||||
return M_PI*radius*radius;
|
return M_PI*radius*radius;
|
||||||
}
|
}
|
||||||
|
|
||||||
double Circle::perimeter(void) {
|
double Circle::perimeter() {
|
||||||
return 2*M_PI*radius;
|
return 2*M_PI*radius;
|
||||||
}
|
}
|
||||||
|
|
||||||
double Square::area(void) {
|
double Square::area() {
|
||||||
return width*width;
|
return width*width;
|
||||||
}
|
}
|
||||||
|
|
||||||
double Square::perimeter(void) {
|
double Square::perimeter() {
|
||||||
return 4*width;
|
return 4*width;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,11 +7,11 @@ public:
|
||||||
}
|
}
|
||||||
virtual ~Shape() {
|
virtual ~Shape() {
|
||||||
nshapes--;
|
nshapes--;
|
||||||
};
|
}
|
||||||
double x, y;
|
double x, y;
|
||||||
void move(double dx, double dy);
|
void move(double dx, double dy);
|
||||||
virtual double area(void) = 0;
|
virtual double area() = 0;
|
||||||
virtual double perimeter(void) = 0;
|
virtual double perimeter() = 0;
|
||||||
static int nshapes;
|
static int nshapes;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -19,21 +19,16 @@ class Circle : public Shape {
|
||||||
private:
|
private:
|
||||||
double radius;
|
double radius;
|
||||||
public:
|
public:
|
||||||
Circle(double r) : radius(r) { };
|
Circle(double r) : radius(r) { }
|
||||||
virtual double area(void);
|
virtual double area();
|
||||||
virtual double perimeter(void);
|
virtual double perimeter();
|
||||||
};
|
};
|
||||||
|
|
||||||
class Square : public Shape {
|
class Square : public Shape {
|
||||||
private:
|
private:
|
||||||
double width;
|
double width;
|
||||||
public:
|
public:
|
||||||
Square(double w) : width(w) { };
|
Square(double w) : width(w) { }
|
||||||
virtual double area(void);
|
virtual double area();
|
||||||
virtual double perimeter(void);
|
virtual double perimeter();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,9 +12,7 @@
|
||||||
<H2>Wrapping a simple C++ class</H2>
|
<H2>Wrapping a simple C++ class</H2>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
This example illustrates the most primitive form of C++ class wrapping performed
|
This example illustrates wrapping a simple C++ class to give a Python class.
|
||||||
by SWIG. In this case, C++ classes are simply transformed into a collection of
|
|
||||||
C-style functions that provide access to class members.
|
|
||||||
|
|
||||||
<h2>The C++ Code</h2>
|
<h2>The C++ Code</h2>
|
||||||
|
|
||||||
|
|
@ -32,8 +30,8 @@ public:
|
||||||
}
|
}
|
||||||
virtual ~Shape() {
|
virtual ~Shape() {
|
||||||
nshapes--;
|
nshapes--;
|
||||||
};
|
}
|
||||||
double x, y;
|
double x, y;
|
||||||
void move(double dx, double dy);
|
void move(double dx, double dy);
|
||||||
virtual double area() = 0;
|
virtual double area() = 0;
|
||||||
virtual double perimeter() = 0;
|
virtual double perimeter() = 0;
|
||||||
|
|
@ -44,7 +42,7 @@ class Circle : public Shape {
|
||||||
private:
|
private:
|
||||||
double radius;
|
double radius;
|
||||||
public:
|
public:
|
||||||
Circle(double r) : radius(r) { };
|
Circle(double r) : radius(r) { }
|
||||||
virtual double area();
|
virtual double area();
|
||||||
virtual double perimeter();
|
virtual double perimeter();
|
||||||
};
|
};
|
||||||
|
|
@ -53,7 +51,7 @@ class Square : public Shape {
|
||||||
private:
|
private:
|
||||||
double width;
|
double width;
|
||||||
public:
|
public:
|
||||||
Square(double w) : width(w) { };
|
Square(double w) : width(w) { }
|
||||||
virtual double area();
|
virtual double area();
|
||||||
virtual double perimeter();
|
virtual double perimeter();
|
||||||
};
|
};
|
||||||
|
|
@ -102,51 +100,34 @@ c = example.new_Circle(10.0)
|
||||||
</blockquote>
|
</blockquote>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<li>To access member data, a pair of accessor functions are used.
|
<li>Member variables of the C++ class are wrapped as attributes of the Python class.
|
||||||
For example:
|
For example:
|
||||||
|
|
||||||
<blockquote>
|
<blockquote>
|
||||||
<pre>
|
<pre>
|
||||||
example.Shape_x_set(c,15) # Set member data
|
c.x = 15 # Set member data
|
||||||
x = example.Shape_x_get(c) # Get member data
|
x = c.x # Get member data
|
||||||
</pre>
|
|
||||||
</blockquote>
|
|
||||||
|
|
||||||
Note: when accessing member data, the name of the class in which
|
|
||||||
the member data was must be used. In this case, <tt>Shape_x_get()</tt>
|
|
||||||
and <tt>Shape_x_set()</tt> are used since 'x' was defined in Shape.
|
|
||||||
|
|
||||||
<p>
|
|
||||||
<li>To invoke a member function, you simply do this
|
|
||||||
|
|
||||||
<blockquote>
|
|
||||||
<pre>
|
|
||||||
print "The area is ", example.Shape_area(c)
|
|
||||||
</pre>
|
</pre>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<li>Type checking knows about the inheritance structure of C++. For example:
|
<li>Member function are invoked as you would expect:
|
||||||
|
|
||||||
<blockquote>
|
<blockquote>
|
||||||
<pre>
|
<pre>
|
||||||
example.Shape_area(c) # Works (c is a Shape)
|
print "The area is ", c.area()
|
||||||
example.Circle_area(c) # Works (c is a Circle)
|
|
||||||
example.Square_area(c) # Fails (c is definitely not a Square)
|
|
||||||
</pre>
|
</pre>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<li>To invoke a destructor, simply do this
|
<li>To invoke a destructor, simply call <code>del</code> on the object:
|
||||||
|
|
||||||
<blockquote>
|
<blockquote>
|
||||||
<pre>
|
<pre>
|
||||||
example.delete_Shape(c) # Deletes a shape
|
del c # Deletes a shape
|
||||||
</pre>
|
</pre>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
|
|
||||||
(Note: destructors are currently not inherited. This might change later).
|
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<li>Static member variables are wrapped as C global variables. For example:
|
<li>Static member variables are wrapped as C global variables. For example:
|
||||||
|
|
||||||
|
|
@ -162,52 +143,12 @@ example.cvar.Shapes_nshapes = 13 # Set a static data member
|
||||||
<h2>General Comments</h2>
|
<h2>General Comments</h2>
|
||||||
|
|
||||||
<ul>
|
<ul>
|
||||||
<li>This low-level interface is not the only way to handle C++ code.
|
<li>SWIG <b>does</b> know how to properly perform upcasting of objects in
|
||||||
Proxy classes provide a much higher-level interface.
|
|
||||||
|
|
||||||
<p>
|
|
||||||
<li>SWIG *does* know how to properly perform upcasting of objects in
|
|
||||||
an inheritance hierarchy (including multiple inheritance). Therefore
|
an inheritance hierarchy (including multiple inheritance). Therefore
|
||||||
it is perfectly safe to pass an object of a derived class to any
|
it is perfectly safe to pass an object of a derived class to any
|
||||||
function involving a base class.
|
function involving a base class.
|
||||||
|
|
||||||
<p>
|
<li>C++ Namespaces - %nspace isn't yet supported for Python.
|
||||||
<li>A wide variety of C++ features are not currently supported by SWIG. Here is the
|
|
||||||
short and incomplete list:
|
|
||||||
|
|
||||||
<p>
|
|
||||||
<ul>
|
|
||||||
<li>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:
|
|
||||||
|
|
||||||
<blockquote>
|
|
||||||
<pre>
|
|
||||||
void foo(int a);
|
|
||||||
%name(foo2) void foo(double a, double b);
|
|
||||||
</pre>
|
|
||||||
</blockquote>
|
|
||||||
|
|
||||||
<p>
|
|
||||||
<li>Overloaded operators. Not supported at all. The only workaround for this is
|
|
||||||
to write a helper function. For example:
|
|
||||||
|
|
||||||
<blockquote>
|
|
||||||
<pre>
|
|
||||||
%inline %{
|
|
||||||
Vector *vector_add(Vector *a, Vector *b) {
|
|
||||||
... whatever ...
|
|
||||||
}
|
|
||||||
%}
|
|
||||||
</pre>
|
|
||||||
</blockquote>
|
|
||||||
|
|
||||||
<p>
|
|
||||||
<li>Namespaces. Not supported at all. Won't be supported until SWIG2.0 (if at all).
|
|
||||||
|
|
||||||
<p>
|
|
||||||
<li>Dave's snide remark: Like a large bottle of strong Tequilla, it's better to
|
|
||||||
use C++ in moderation.
|
|
||||||
|
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,8 @@ for o in [c,s]:
|
||||||
print " ", o
|
print " ", o
|
||||||
print " area = ", o.area()
|
print " area = ", o.area()
|
||||||
print " perimeter = ", o.perimeter()
|
print " perimeter = ", o.perimeter()
|
||||||
|
# prevent o from holding a reference to the last object looked at
|
||||||
|
o = None
|
||||||
|
|
||||||
print "\nGuess I'll clean up now"
|
print "\nGuess I'll clean up now"
|
||||||
|
|
||||||
|
|
@ -45,7 +47,5 @@ print "\nGuess I'll clean up now"
|
||||||
del c
|
del c
|
||||||
del s
|
del s
|
||||||
|
|
||||||
s = 3
|
|
||||||
print example.cvar.Shape_nshapes,"shapes remain"
|
print example.cvar.Shape_nshapes,"shapes remain"
|
||||||
print "Goodbye"
|
print "Goodbye"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
/* File : example.c */
|
/* File : example.cxx */
|
||||||
|
|
||||||
#include "example.h"
|
#include "example.h"
|
||||||
#define M_PI 3.14159265358979323846
|
#define M_PI 3.14159265358979323846
|
||||||
|
|
@ -11,18 +11,18 @@ void Shape::move(double dx, double dy) {
|
||||||
|
|
||||||
int Shape::nshapes = 0;
|
int Shape::nshapes = 0;
|
||||||
|
|
||||||
double Circle::area(void) {
|
double Circle::area() {
|
||||||
return M_PI*radius*radius;
|
return M_PI*radius*radius;
|
||||||
}
|
}
|
||||||
|
|
||||||
double Circle::perimeter(void) {
|
double Circle::perimeter() {
|
||||||
return 2*M_PI*radius;
|
return 2*M_PI*radius;
|
||||||
}
|
}
|
||||||
|
|
||||||
double Square::area(void) {
|
double Square::area() {
|
||||||
return width*width;
|
return width*width;
|
||||||
}
|
}
|
||||||
|
|
||||||
double Square::perimeter(void) {
|
double Square::perimeter() {
|
||||||
return 4*width;
|
return 4*width;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,11 +7,11 @@ public:
|
||||||
}
|
}
|
||||||
virtual ~Shape() {
|
virtual ~Shape() {
|
||||||
nshapes--;
|
nshapes--;
|
||||||
};
|
}
|
||||||
double x, y;
|
double x, y;
|
||||||
void move(double dx, double dy);
|
void move(double dx, double dy);
|
||||||
virtual double area(void) = 0;
|
virtual double area() = 0;
|
||||||
virtual double perimeter(void) = 0;
|
virtual double perimeter() = 0;
|
||||||
static int nshapes;
|
static int nshapes;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -19,21 +19,16 @@ class Circle : public Shape {
|
||||||
private:
|
private:
|
||||||
double radius;
|
double radius;
|
||||||
public:
|
public:
|
||||||
Circle(double r) : radius(r) { };
|
Circle(double r) : radius(r) { }
|
||||||
virtual double area(void);
|
virtual double area();
|
||||||
virtual double perimeter(void);
|
virtual double perimeter();
|
||||||
};
|
};
|
||||||
|
|
||||||
class Square : public Shape {
|
class Square : public Shape {
|
||||||
private:
|
private:
|
||||||
double width;
|
double width;
|
||||||
public:
|
public:
|
||||||
Square(double w) : width(w) { };
|
Square(double w) : width(w) { }
|
||||||
virtual double area(void);
|
virtual double area();
|
||||||
virtual double perimeter(void);
|
virtual double perimeter();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,9 +12,7 @@
|
||||||
<H2>Wrapping a simple C++ class</H2>
|
<H2>Wrapping a simple C++ class</H2>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
This example illustrates C++ class wrapping performed by SWIG.
|
This example illustrates wrapping a simple C++ class to give a Python class.
|
||||||
C++ classes are simply transformed into Ruby classes that provide methods to
|
|
||||||
access class members.
|
|
||||||
|
|
||||||
<h2>The C++ Code</h2>
|
<h2>The C++ Code</h2>
|
||||||
|
|
||||||
|
|
@ -32,8 +30,8 @@ public:
|
||||||
}
|
}
|
||||||
virtual ~Shape() {
|
virtual ~Shape() {
|
||||||
nshapes--;
|
nshapes--;
|
||||||
};
|
}
|
||||||
double x, y;
|
double x, y;
|
||||||
void move(double dx, double dy);
|
void move(double dx, double dy);
|
||||||
virtual double area() = 0;
|
virtual double area() = 0;
|
||||||
virtual double perimeter() = 0;
|
virtual double perimeter() = 0;
|
||||||
|
|
@ -44,7 +42,7 @@ class Circle : public Shape {
|
||||||
private:
|
private:
|
||||||
double radius;
|
double radius;
|
||||||
public:
|
public:
|
||||||
Circle(double r) : radius(r) { };
|
Circle(double r) : radius(r) { }
|
||||||
virtual double area();
|
virtual double area();
|
||||||
virtual double perimeter();
|
virtual double perimeter();
|
||||||
};
|
};
|
||||||
|
|
@ -53,7 +51,7 @@ class Square : public Shape {
|
||||||
private:
|
private:
|
||||||
double width;
|
double width;
|
||||||
public:
|
public:
|
||||||
Square(double w) : width(w) { };
|
Square(double w) : width(w) { }
|
||||||
virtual double area();
|
virtual double area();
|
||||||
virtual double perimeter();
|
virtual double perimeter();
|
||||||
};
|
};
|
||||||
|
|
@ -122,10 +120,8 @@ print "The area is ", c.area, "\n"
|
||||||
</blockquote>
|
</blockquote>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<li>When a instance of Ruby level wrapper class is garbage collected by
|
<li>When a instance of Ruby level wrapper class is garbage collected by the
|
||||||
Ruby interpreter, the corresponding C++ destructor is automatically invoked.
|
Ruby interpreter, the corresponding C++ destructor is automatically invoked.
|
||||||
(Note: destructors are currently not inherited. This might change later.
|
|
||||||
Until then, use <tt>-make_default</tt>).
|
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<li>Static member variables are wrapped as Ruby class accessor methods.
|
<li>Static member variables are wrapped as Ruby class accessor methods.
|
||||||
|
|
@ -144,53 +140,14 @@ Shapes.nshapes = 13 # Set a static data member
|
||||||
|
|
||||||
<ul>
|
<ul>
|
||||||
<li>Ruby module of SWIG differs from other language modules in wrapping C++
|
<li>Ruby module of SWIG differs from other language modules in wrapping C++
|
||||||
interfaces. They provides lower-level interfaces and optional higher-level
|
interfaces. They provide lower-level interfaces and optional higher-level
|
||||||
interfaces know as proxy classes. Ruby module needs no such redundancy
|
interfaces know as proxy classes. Ruby module needs no such redundancy
|
||||||
due to Ruby's sophisticated extension API.
|
due to Ruby's sophisticated extension API.
|
||||||
|
|
||||||
<p>
|
<li>SWIG <b>does</b> know how to properly perform upcasting of objects in
|
||||||
<li>SWIG *does* know how to properly perform upcasting of objects in
|
|
||||||
an inheritance hierarchy except for multiple inheritance.
|
an inheritance hierarchy except for multiple inheritance.
|
||||||
|
|
||||||
<p>
|
<li>C++ Namespaces - %nspace isn't yet supported for Python.
|
||||||
<li>A wide variety of C++ features are not currently supported by SWIG. Here is the
|
|
||||||
short and incomplete list:
|
|
||||||
|
|
||||||
<p>
|
|
||||||
<ul>
|
|
||||||
<li>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:
|
|
||||||
|
|
||||||
<blockquote>
|
|
||||||
<pre>
|
|
||||||
void foo(int a);
|
|
||||||
%name(foo2) void foo(double a, double b);
|
|
||||||
</pre>
|
|
||||||
</blockquote>
|
|
||||||
|
|
||||||
<p>
|
|
||||||
<li>Overloaded operators. Not supported at all. The only workaround for this is
|
|
||||||
to write a helper function. For example:
|
|
||||||
|
|
||||||
<blockquote>
|
|
||||||
<pre>
|
|
||||||
%inline %{
|
|
||||||
Vector *vector_add(Vector *a, Vector *b) {
|
|
||||||
... whatever ...
|
|
||||||
}
|
|
||||||
%}
|
|
||||||
</pre>
|
|
||||||
</blockquote>
|
|
||||||
|
|
||||||
<p>
|
|
||||||
<li>Namespaces. Not supported at all. Won't be supported until SWIG2.0 (if at all).
|
|
||||||
|
|
||||||
</ul>
|
|
||||||
<p>
|
|
||||||
|
|
||||||
<li>Dave's snide remark: Like a large bottle of strong Tequilla, it's better to
|
|
||||||
use C++ in moderation.
|
|
||||||
|
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -45,5 +45,9 @@ end
|
||||||
# Notice how the Shape#area() and Shape#perimeter() functions really
|
# Notice how the Shape#area() and Shape#perimeter() functions really
|
||||||
# invoke the appropriate virtual method on each object.
|
# invoke the appropriate virtual method on each object.
|
||||||
|
|
||||||
|
# Remove references to the object and force a garbage collection run.
|
||||||
|
c = s = o = nil
|
||||||
|
GC.start()
|
||||||
|
|
||||||
print "\n", Example::Shape.nshapes," shapes remain\n"
|
print "\n", Example::Shape.nshapes," shapes remain\n"
|
||||||
print "Goodbye\n"
|
print "Goodbye\n"
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
/* File : example.c */
|
/* File : example.cxx */
|
||||||
|
|
||||||
#include "example.h"
|
#include "example.h"
|
||||||
#define M_PI 3.14159265358979323846
|
#define M_PI 3.14159265358979323846
|
||||||
|
|
@ -11,18 +11,18 @@ void Shape::move(double dx, double dy) {
|
||||||
|
|
||||||
int Shape::nshapes = 0;
|
int Shape::nshapes = 0;
|
||||||
|
|
||||||
double Circle::area(void) {
|
double Circle::area() {
|
||||||
return M_PI*radius*radius;
|
return M_PI*radius*radius;
|
||||||
}
|
}
|
||||||
|
|
||||||
double Circle::perimeter(void) {
|
double Circle::perimeter() {
|
||||||
return 2*M_PI*radius;
|
return 2*M_PI*radius;
|
||||||
}
|
}
|
||||||
|
|
||||||
double Square::area(void) {
|
double Square::area() {
|
||||||
return width*width;
|
return width*width;
|
||||||
}
|
}
|
||||||
|
|
||||||
double Square::perimeter(void) {
|
double Square::perimeter() {
|
||||||
return 4*width;
|
return 4*width;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,11 +7,11 @@ public:
|
||||||
}
|
}
|
||||||
virtual ~Shape() {
|
virtual ~Shape() {
|
||||||
nshapes--;
|
nshapes--;
|
||||||
};
|
}
|
||||||
double x, y;
|
double x, y;
|
||||||
void move(double dx, double dy);
|
void move(double dx, double dy);
|
||||||
virtual double area(void) = 0;
|
virtual double area() = 0;
|
||||||
virtual double perimeter(void) = 0;
|
virtual double perimeter() = 0;
|
||||||
static int nshapes;
|
static int nshapes;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -19,21 +19,16 @@ class Circle : public Shape {
|
||||||
private:
|
private:
|
||||||
double radius;
|
double radius;
|
||||||
public:
|
public:
|
||||||
Circle(double r) : radius(r) { };
|
Circle(double r) : radius(r) { }
|
||||||
virtual double area(void);
|
virtual double area();
|
||||||
virtual double perimeter(void);
|
virtual double perimeter();
|
||||||
};
|
};
|
||||||
|
|
||||||
class Square : public Shape {
|
class Square : public Shape {
|
||||||
private:
|
private:
|
||||||
double width;
|
double width;
|
||||||
public:
|
public:
|
||||||
Square(double w) : width(w) { };
|
Square(double w) : width(w) { }
|
||||||
virtual double area(void);
|
virtual double area();
|
||||||
virtual double perimeter(void);
|
virtual double perimeter();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,5 @@
|
||||||
%}
|
%}
|
||||||
|
|
||||||
/* Let's just grab the original header file here */
|
/* Let's just grab the original header file here */
|
||||||
|
|
||||||
%include "example.h"
|
%include "example.h"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,8 +32,8 @@ public:
|
||||||
}
|
}
|
||||||
virtual ~Shape() {
|
virtual ~Shape() {
|
||||||
nshapes--;
|
nshapes--;
|
||||||
};
|
}
|
||||||
double x, y;
|
double x, y;
|
||||||
void move(double dx, double dy);
|
void move(double dx, double dy);
|
||||||
virtual double area() = 0;
|
virtual double area() = 0;
|
||||||
virtual double perimeter() = 0;
|
virtual double perimeter() = 0;
|
||||||
|
|
@ -44,7 +44,7 @@ class Circle : public Shape {
|
||||||
private:
|
private:
|
||||||
double radius;
|
double radius;
|
||||||
public:
|
public:
|
||||||
Circle(double r) : radius(r) { };
|
Circle(double r) : radius(r) { }
|
||||||
virtual double area();
|
virtual double area();
|
||||||
virtual double perimeter();
|
virtual double perimeter();
|
||||||
};
|
};
|
||||||
|
|
@ -53,7 +53,7 @@ class Square : public Shape {
|
||||||
private:
|
private:
|
||||||
double width;
|
double width;
|
||||||
public:
|
public:
|
||||||
Square(double w) : width(w) { };
|
Square(double w) : width(w) { }
|
||||||
virtual double area();
|
virtual double area();
|
||||||
virtual double perimeter();
|
virtual double perimeter();
|
||||||
};
|
};
|
||||||
|
|
@ -91,10 +91,10 @@ Note: when creating a C++ extension, you must run SWIG with the <tt>-c++</tt> op
|
||||||
SWIG performs two forms of C++ wrapping-- a low level interface and a high level widget-like interface.
|
SWIG performs two forms of C++ wrapping-- a low level interface and a high level widget-like interface.
|
||||||
<ul>
|
<ul>
|
||||||
<li>
|
<li>
|
||||||
Click <a href="example1.tcl">here</a> to see a script that calls the C++ functions using the
|
Click <a href="runme.tcl">here</a> to see a script that calls the C++ functions using the
|
||||||
low-level interface.
|
low-level interface.
|
||||||
<li>
|
<li>
|
||||||
Click <a href="example2.tcl">here</a> to see a the same script written with the high-level
|
Click <a href="runme2.tcl">here</a> to see the same script written with the high-level
|
||||||
interface.
|
interface.
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
|
@ -225,47 +225,15 @@ set Shapes_nshapes 13 # Set a static data member
|
||||||
<h2>General Comments</h2>
|
<h2>General Comments</h2>
|
||||||
|
|
||||||
<ul>
|
<ul>
|
||||||
|
|
||||||
<li>The low-level function interface is much faster than the high-level interface.
|
<li>The low-level function interface is much faster than the high-level interface.
|
||||||
In fact, all the higher level interface does is call functions in the low-level interface.
|
In fact, all the higher level interface does is call functions in the low-level interface.
|
||||||
|
|
||||||
<p>
|
|
||||||
<li>SWIG *does* know how to properly perform upcasting of objects in an inheritance
|
<li>SWIG *does* know how to properly perform upcasting of objects in an inheritance
|
||||||
hierarchy (including multiple inheritance). Therefore it is perfectly safe to pass
|
hierarchy (including multiple inheritance). Therefore it is perfectly safe to pass
|
||||||
an object of a derived class to any function involving a base class.
|
an object of a derived class to any function involving a base class.
|
||||||
|
|
||||||
<p>
|
<li>C++ Namespaces - %nspace isn't yet supported for Python.
|
||||||
<li>A wide variety of C++ features are not currently supported by SWIG. Here is the
|
|
||||||
short and incomplete list:
|
|
||||||
|
|
||||||
<p>
|
|
||||||
<ul>
|
|
||||||
<li>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:
|
|
||||||
|
|
||||||
<blockquote>
|
|
||||||
<pre>
|
|
||||||
void foo(int a);
|
|
||||||
%name(foo2) void foo(double a, double b);
|
|
||||||
</pre>
|
|
||||||
</blockquote>
|
|
||||||
|
|
||||||
<p>
|
|
||||||
<li>Overloaded operators. Not supported at all. The only workaround for this is
|
|
||||||
to write a helper function. For example:
|
|
||||||
|
|
||||||
<blockquote>
|
|
||||||
<pre>
|
|
||||||
%inline %{
|
|
||||||
Vector *vector_add(Vector *a, Vector *b) {
|
|
||||||
... whatever ...
|
|
||||||
}
|
|
||||||
%}
|
|
||||||
</pre>
|
|
||||||
</blockquote>
|
|
||||||
|
|
||||||
<p>
|
|
||||||
<li>Namespaces. Not supported at all. Won't be supported until SWIG2.0 (if at all).
|
|
||||||
|
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue