Duplicate to class example.
Removed git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@6032 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
de9c2d5040
commit
b2d29d6375
7 changed files with 0 additions and 399 deletions
|
|
@ -1,10 +0,0 @@
|
|||
example.pm
|
||||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
example.dll
|
||||
example.dsw
|
||||
example.ncb
|
||||
example.opt
|
||||
example.plg
|
||||
Release
|
||||
Debug
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
CXXSRCS = example.cxx
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS = -lm
|
||||
SWIGOPT =
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' perl5_cpp
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='myperl' INTERFACE='$(INTERFACE)' perl5_cpp_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile perl5_clean
|
||||
|
||||
check: all
|
||||
|
|
@ -1,28 +0,0 @@
|
|||
/* File : example.c */
|
||||
|
||||
#include "example.h"
|
||||
#define M_PI 3.14159265358979323846
|
||||
|
||||
/* Move the shape to a new location */
|
||||
void Shape::move(double dx, double dy) {
|
||||
x += dx;
|
||||
y += dy;
|
||||
}
|
||||
|
||||
int Shape::nshapes = 0;
|
||||
|
||||
double Circle::area(void) {
|
||||
return M_PI*radius*radius;
|
||||
}
|
||||
|
||||
double Circle::perimeter(void) {
|
||||
return 2*M_PI*radius;
|
||||
}
|
||||
|
||||
double Square::area(void) {
|
||||
return width*width;
|
||||
}
|
||||
|
||||
double Square::perimeter(void) {
|
||||
return 4*width;
|
||||
}
|
||||
|
|
@ -1,41 +0,0 @@
|
|||
/* File : example.h */
|
||||
|
||||
class Shape {
|
||||
public:
|
||||
Shape() {
|
||||
nshapes++;
|
||||
}
|
||||
virtual ~Shape() {
|
||||
nshapes--;
|
||||
};
|
||||
double x, y;
|
||||
void move(double dx, double dy);
|
||||
virtual double area(void) = 0;
|
||||
virtual double perimeter(void) = 0;
|
||||
static int nshapes;
|
||||
};
|
||||
|
||||
class Circle : public Shape {
|
||||
private:
|
||||
double radius;
|
||||
public:
|
||||
Circle(double r) : radius(r) { };
|
||||
~Circle() { };
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
|
||||
class Square : public Shape {
|
||||
private:
|
||||
double width;
|
||||
public:
|
||||
Square(double w) : width(w) { };
|
||||
~Square() { }
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* Let's just grab the original header file here */
|
||||
|
||||
%include "example.h"
|
||||
|
||||
|
|
@ -1,233 +0,0 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>SWIG:Examples:perl5:class</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff">
|
||||
|
||||
|
||||
<tt>SWIG/Examples/perl5/class/</tt>
|
||||
<hr>
|
||||
|
||||
<H2>Wrapping a simple C++ class</H2>
|
||||
|
||||
<tt>$Header$</tt><br>
|
||||
|
||||
<p>
|
||||
This example illustrates the most primitive form of C++ class wrapping performed
|
||||
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>
|
||||
|
||||
Suppose you have some C++ classes described by the following (and admittedly lame)
|
||||
header file:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
/* 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();
|
||||
};
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<h2>The SWIG interface</h2>
|
||||
|
||||
A simple SWIG interface for this can be built by simply grabbing the header file
|
||||
like this:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* Let's just grab the original header file here */
|
||||
%include "example.h"
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
Note: when creating a C++ extension, you must run SWIG with the <tt>-c++</tt> option like this:
|
||||
<blockquote>
|
||||
<pre>
|
||||
% swig -c++ -python example.i
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<h2>A sample Perl script</h2>
|
||||
|
||||
Click <a href="runme.pl">here</a> to see a script that calls the C++ functions from Perl.
|
||||
|
||||
<h2>Key points</h2>
|
||||
|
||||
<ul>
|
||||
<li>To create a new object, you call a constructor like this:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
$c = example::new_Circle(10.0);
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<p>
|
||||
<li>To access member data, a pair of accessor functions are used.
|
||||
For example:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
example::Circle_x_set($c,15); # Set member data
|
||||
$x = example::Shape_x_get($c); # Get member data
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
Note: when accessing member data, the name of the base class or the derived class can be
|
||||
used in the function name as shown above. Of course, it would probably be more
|
||||
proper to just use the base class version such as <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>
|
||||
</blockquote>
|
||||
|
||||
<p>
|
||||
<li>Type checking knows about the inheritance structure of C++. For example:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
example::Shape_area($c); # Works (c is a Shape)
|
||||
example::Circle_area($c); # Works (c is a Circle)
|
||||
example::Square_area($c); # Fails (c is definitely not a Square)
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<p>
|
||||
<li>To invoke a destructor, simply do this
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
example::delete_Shape($c); # Deletes a shape
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<p>
|
||||
<li>Static member variables are wrapped as C global variables. For example:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
$n = $example::Shape_nshapes; # Get a static data member
|
||||
$example::Shapes_nshapes = 13; # Set a static data member
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
</ul>
|
||||
|
||||
<h2>General Comments</h2>
|
||||
|
||||
<ul>
|
||||
<li>This low-level interface is not the only way to handle C++ code. Shadow 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 it is perfectly safe to pass
|
||||
an object of a derived class to any function involving a base class.
|
||||
|
||||
<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).
|
||||
|
||||
<p>
|
||||
<li>Templates. Not supported at all. SWIG throws out anything that looks like a template.
|
||||
You can work around the problem by aliasing a template class behind a typedef however.
|
||||
For example:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
%{
|
||||
typedef vector<int> IntVector;
|
||||
%}
|
||||
|
||||
class IntVector {
|
||||
public:
|
||||
... methods ...
|
||||
};
|
||||
</pre>
|
||||
</blockquote>
|
||||
</ul>
|
||||
<p>
|
||||
<li>There is no guarantee that an extremely complex C++ application will be able to compile
|
||||
as a Python extension. Sorry.
|
||||
|
||||
</ul>
|
||||
|
||||
<hr>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -1,56 +0,0 @@
|
|||
# file: runme.pl
|
||||
|
||||
# This file illustrates the low-level C++ interface
|
||||
# created by SWIG. In this case, all of our C++ classes
|
||||
# get converted into function calls.
|
||||
|
||||
use example;
|
||||
|
||||
# ----- Object creation -----
|
||||
|
||||
print "Creating some objects:\n";
|
||||
$c = new example::Circle(10);
|
||||
print " Created circle $c\n";
|
||||
$s = new example::Square(10);
|
||||
print " Created square $s\n";
|
||||
|
||||
# ----- Access a static member -----
|
||||
|
||||
print "\nA total of $example::Shape::nshapes shapes were created\n";
|
||||
|
||||
# ----- Member data access -----
|
||||
|
||||
# Set the location of the object.
|
||||
# Note: methods in the base class Shape are used since
|
||||
# x and y are defined there.
|
||||
|
||||
$c->{x} = 20;
|
||||
$c->{y} = 30;
|
||||
$s->{x} = -10;
|
||||
$s->{y} = 5;
|
||||
|
||||
print "\nHere is their current position:\n";
|
||||
print " Circle = (",$c->{x},",", $c->{y},")\n";
|
||||
print " Square = (",$s->{x},",", $s->{y},")\n";
|
||||
|
||||
# ----- Call some methods -----
|
||||
|
||||
print "\nHere are some properties of the shapes:\n";
|
||||
foreach $o ($c,$s) {
|
||||
print " $o\n";
|
||||
print " area = ", $o->area(), "\n";
|
||||
print " perimeter = ", $o->perimeter(), "\n";
|
||||
}
|
||||
|
||||
# ----- Delete everything -----
|
||||
|
||||
print "\nGuess I'll clean up now\n";
|
||||
|
||||
# Note: this invokes the virtual destructor
|
||||
|
||||
$c->DESTROY();
|
||||
$s->DESTROY();
|
||||
|
||||
print $example::Shape::nshapes," shapes remain\n";
|
||||
print "Goodbye\n";
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue