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:
Olly Betts 2014-02-23 18:05:50 +13:00
commit 34c97ffdbd
23 changed files with 154 additions and 412 deletions

View file

@ -1,4 +1,4 @@
/* File : example.c */
/* File : example.cxx */
#include "example.h"
#define M_PI 3.14159265358979323846
@ -11,18 +11,18 @@ void Shape::move(double dx, double dy) {
int Shape::nshapes = 0;
double Circle::area(void) {
double Circle::area() {
return M_PI*radius*radius;
}
double Circle::perimeter(void) {
double Circle::perimeter() {
return 2*M_PI*radius;
}
double Square::area(void) {
double Square::area() {
return width*width;
}
double Square::perimeter(void) {
double Square::perimeter() {
return 4*width;
}

View file

@ -7,11 +7,11 @@ public:
}
virtual ~Shape() {
nshapes--;
};
double x, y;
}
double x, y;
void move(double dx, double dy);
virtual double area(void) = 0;
virtual double perimeter(void) = 0;
virtual double area() = 0;
virtual double perimeter() = 0;
static int nshapes;
};
@ -19,21 +19,16 @@ class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) { };
virtual double area(void);
virtual double perimeter(void);
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(void);
virtual double perimeter(void);
Square(double w) : width(w) { }
virtual double area();
virtual double perimeter();
};

View file

@ -12,9 +12,7 @@
<H2>Wrapping a simple C++ class</H2>
<p>
This example illustrates C++ class wrapping performed by SWIG.
C++ classes are simply transformed into Ruby classes that provide methods to
access class members.
This example illustrates wrapping a simple C++ class to give a Python class.
<h2>The C++ Code</h2>
@ -32,8 +30,8 @@ public:
}
virtual ~Shape() {
nshapes--;
};
double x, y;
}
double x, y;
void move(double dx, double dy);
virtual double area() = 0;
virtual double perimeter() = 0;
@ -44,7 +42,7 @@ class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) { };
Circle(double r) : radius(r) { }
virtual double area();
virtual double perimeter();
};
@ -53,7 +51,7 @@ class Square : public Shape {
private:
double width;
public:
Square(double w) : width(w) { };
Square(double w) : width(w) { }
virtual double area();
virtual double perimeter();
};
@ -122,10 +120,8 @@ print "The area is ", c.area, "\n"
</blockquote>
<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.
(Note: destructors are currently not inherited. This might change later.
Until then, use <tt>-make_default</tt>).
<p>
<li>Static member variables are wrapped as Ruby class accessor methods.
@ -144,53 +140,14 @@ Shapes.nshapes = 13 # Set a static data member
<ul>
<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
due to Ruby's sophisticated extension API.
<p>
<li>SWIG *does* know how to properly perform upcasting of objects in
<li>SWIG <b>does</b> know how to properly perform upcasting of objects in
an inheritance hierarchy except for multiple inheritance.
<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>
<p>
<li>Dave's snide remark: Like a large bottle of strong Tequilla, it's better to
use C++ in moderation.
<li>C++ Namespaces - %nspace isn't yet supported for Python.
</ul>

View file

@ -45,5 +45,9 @@ end
# Notice how the Shape#area() and Shape#perimeter() functions really
# 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 "Goodbye\n"