New example
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@778 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
2b0da81001
commit
01795d2075
8 changed files with 521 additions and 0 deletions
19
Examples/tcl/class/Makefile
Normal file
19
Examples/tcl/class/Makefile
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
TOP = ../..
|
||||||
|
SWIG = $(TOP)/../swig
|
||||||
|
CXXSRCS = example.cxx
|
||||||
|
TARGET = example
|
||||||
|
INTERFACE = example.i
|
||||||
|
LIBS = -lm
|
||||||
|
|
||||||
|
all::
|
||||||
|
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||||
|
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' tcl_cpp
|
||||||
|
|
||||||
|
static::
|
||||||
|
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||||
|
TARGET='mytclsh' INTERFACE='$(INTERFACE)' tclsh_cpp_static
|
||||||
|
|
||||||
|
clean::
|
||||||
|
rm -f *_wrap* *.o *~ *.so mytclsh *.pyc .~* core
|
||||||
|
|
||||||
|
check: all
|
||||||
28
Examples/tcl/class/example.cxx
Normal file
28
Examples/tcl/class/example.cxx
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
/* File : example.c */
|
||||||
|
|
||||||
|
#include "example.h"
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
/* 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() {
|
||||||
|
return M_PI*radius*radius;
|
||||||
|
}
|
||||||
|
|
||||||
|
double Circle::perimeter() {
|
||||||
|
return 2*M_PI*radius;
|
||||||
|
}
|
||||||
|
|
||||||
|
double Square::area() {
|
||||||
|
return width*width;
|
||||||
|
}
|
||||||
|
|
||||||
|
double Square::perimeter() {
|
||||||
|
return 4*width;
|
||||||
|
}
|
||||||
39
Examples/tcl/class/example.h
Normal file
39
Examples/tcl/class/example.h
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
/* 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();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
11
Examples/tcl/class/example.i
Normal file
11
Examples/tcl/class/example.i
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
/* File : example.i */
|
||||||
|
%module example
|
||||||
|
|
||||||
|
%{
|
||||||
|
#include "example.h"
|
||||||
|
%}
|
||||||
|
|
||||||
|
/* Let's just grab the original header file here */
|
||||||
|
|
||||||
|
%include "example.h"
|
||||||
|
|
||||||
73
Examples/tcl/class/example1.tcl
Normal file
73
Examples/tcl/class/example1.tcl
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
# file: example1.tcl
|
||||||
|
|
||||||
|
# This file illustrates the low-level C++ interface
|
||||||
|
# created by SWIG. In this case, all of our C++ classes
|
||||||
|
# get converted into function calls.
|
||||||
|
|
||||||
|
catch { load ./example.so example}
|
||||||
|
catch { load ./example.dll example} ;# Windows
|
||||||
|
|
||||||
|
# ----- Object creation -----
|
||||||
|
|
||||||
|
puts "Creating some objects:"
|
||||||
|
set c [new_Circle 10]
|
||||||
|
puts " Created circle $c"
|
||||||
|
set s [new_Square 10]
|
||||||
|
puts " Created square $s"
|
||||||
|
|
||||||
|
# ----- Access a static member -----
|
||||||
|
|
||||||
|
puts "\nA total of $Shape_nshapes shapes were created"
|
||||||
|
|
||||||
|
# ----- Member data access -----
|
||||||
|
|
||||||
|
# Set the location of the object
|
||||||
|
|
||||||
|
# Notice how we can do this using functions specific to
|
||||||
|
# the 'Circle' class.
|
||||||
|
Circle_x_set $c 20
|
||||||
|
Circle_y_set $c 30
|
||||||
|
|
||||||
|
# Now use the same functions in the base class
|
||||||
|
Shape_x_set $s -10
|
||||||
|
Shape_y_set $s 5
|
||||||
|
|
||||||
|
puts "\nHere is their current position:"
|
||||||
|
puts " Circle = ([Shape_x_get $c], [Shape_y_get $c])"
|
||||||
|
puts " Square = ([Shape_x_get $s], [Shape_y_get $s])"
|
||||||
|
|
||||||
|
# ----- Call some methods -----
|
||||||
|
|
||||||
|
puts "\nHere are some properties of the shapes:"
|
||||||
|
foreach o "$c $s" {
|
||||||
|
puts " $o"
|
||||||
|
puts " area = [Shape_area $o]"
|
||||||
|
puts " perimeter = [Shape_perimeter $o]"
|
||||||
|
}
|
||||||
|
# Notice how the Shape_area() and Shape_perimeter() functions really
|
||||||
|
# invoke the appropriate virtual method on each object.
|
||||||
|
|
||||||
|
# ----- Try to cause a type error -----
|
||||||
|
|
||||||
|
puts "\nI'm going to try and break the type system"
|
||||||
|
|
||||||
|
if { [catch {
|
||||||
|
# Bad script!
|
||||||
|
Square_area $c # Try to invoke Square method on a Circle
|
||||||
|
puts " Bad bad SWIG!"
|
||||||
|
|
||||||
|
}]} {
|
||||||
|
puts " Well, it didn't work. Good SWIG."
|
||||||
|
}
|
||||||
|
|
||||||
|
# ----- Delete everything -----
|
||||||
|
|
||||||
|
puts "\nGuess I'll clean up now"
|
||||||
|
|
||||||
|
# Note: this invokes the virtual destructor
|
||||||
|
delete_Shape $c
|
||||||
|
delete_Shape $s
|
||||||
|
|
||||||
|
puts "$Shape_nshapes shapes remain"
|
||||||
|
puts "Goodbye"
|
||||||
|
|
||||||
51
Examples/tcl/class/example2.tcl
Normal file
51
Examples/tcl/class/example2.tcl
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
# file: example1.tcl
|
||||||
|
|
||||||
|
# This file illustrates the high level C++ interface.
|
||||||
|
# In this case C++ classes work kind of like Tk widgets
|
||||||
|
|
||||||
|
catch { load ./example.so example}
|
||||||
|
catch { load ./example.dll example} ;# Windows
|
||||||
|
|
||||||
|
# ----- Object creation -----
|
||||||
|
|
||||||
|
puts "Creating some objects:"
|
||||||
|
Circle c 10
|
||||||
|
puts " Created circle [c cget -this]"
|
||||||
|
Circle s 10
|
||||||
|
puts " Created square [s cget -this]"
|
||||||
|
|
||||||
|
# ----- Access a static member -----
|
||||||
|
|
||||||
|
puts "\nA total of $Shape_nshapes shapes were created"
|
||||||
|
|
||||||
|
# ----- Member data access -----
|
||||||
|
|
||||||
|
# Set the location of the object
|
||||||
|
|
||||||
|
c configure -x 20 -y 30
|
||||||
|
s configure -x -10 -y 5
|
||||||
|
|
||||||
|
puts "\nHere is their current position:"
|
||||||
|
puts " Circle = ([c cget -x], [c cget -y])"
|
||||||
|
puts " Square = ([s cget -x], [s cget -y])"
|
||||||
|
|
||||||
|
# ----- Call some methods -----
|
||||||
|
|
||||||
|
puts "\nHere are some properties of the shapes:"
|
||||||
|
foreach o "c s" {
|
||||||
|
puts " [$o cget -this]"
|
||||||
|
puts " area = [$o area]"
|
||||||
|
puts " perimeter = [$o perimeter]"
|
||||||
|
}
|
||||||
|
|
||||||
|
# ----- Delete everything -----
|
||||||
|
|
||||||
|
puts "\nGuess I'll clean up now"
|
||||||
|
|
||||||
|
# Note: this invokes the virtual destructor
|
||||||
|
rename c ""
|
||||||
|
rename s ""
|
||||||
|
|
||||||
|
puts "$Shape_nshapes shapes remain"
|
||||||
|
puts "Goodbye"
|
||||||
|
|
||||||
299
Examples/tcl/class/index.html
Normal file
299
Examples/tcl/class/index.html
Normal file
|
|
@ -0,0 +1,299 @@
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>SWIG:Examples:tcl:class</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body bgcolor="#ffffff">
|
||||||
|
|
||||||
|
|
||||||
|
<tt>SWIG/Examples/tcl/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++ -tcl example.i
|
||||||
|
</pre>
|
||||||
|
</blockquote>
|
||||||
|
|
||||||
|
<h2>Some sample Tcl scripts</h2>
|
||||||
|
|
||||||
|
SWIG performs two forms of C++ wrapping-- a low level interface and a high level widget-like interface.
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
Click <a href="example1.tcl">here</a> to see a script that calls the C++ functions using the
|
||||||
|
low-level interface.
|
||||||
|
<li>
|
||||||
|
Click <a href="example2.tcl">here</a> to see a the same script written with the high-level
|
||||||
|
interface.
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<h2>Key points</h2>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>The low-level C++ interface works like this:
|
||||||
|
<p>
|
||||||
|
<ul>
|
||||||
|
<li>To create a new object, you call a constructor like this:
|
||||||
|
|
||||||
|
<blockquote>
|
||||||
|
<pre>
|
||||||
|
set c [new_Circle 10.0]
|
||||||
|
</pre>
|
||||||
|
</blockquote>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<li>To access member data, a pair of accessor functions are used.
|
||||||
|
For example:
|
||||||
|
|
||||||
|
<blockquote>
|
||||||
|
<pre>
|
||||||
|
Circle_x_set $c 15 ;# Set member data
|
||||||
|
set x [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>
|
||||||
|
puts "The area is [Shape_area $c]"
|
||||||
|
</pre>
|
||||||
|
</blockquote>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<li>Type checking knows about the inheritance structure of C++. For example:
|
||||||
|
|
||||||
|
<blockquote>
|
||||||
|
<pre>
|
||||||
|
Shape_area $c # Works (c is a Shape)
|
||||||
|
Circle_area $c # Works (c is a Circle)
|
||||||
|
Square_area $c # Fails (c is definitely not a Square)
|
||||||
|
</pre>
|
||||||
|
</blockquote>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<li>To invoke a destructor, simply do this
|
||||||
|
|
||||||
|
<blockquote>
|
||||||
|
<pre>
|
||||||
|
delete_Shape $c # Deletes a shape
|
||||||
|
</pre>
|
||||||
|
</blockquote>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<li>Static member variables are wrapped as C global variables. For example:
|
||||||
|
|
||||||
|
<blockquote>
|
||||||
|
<pre>
|
||||||
|
set n $Shape_nshapes # Get a static data member
|
||||||
|
set Shapes_nshapes 13 # Set a static data member
|
||||||
|
</pre>
|
||||||
|
</blockquote>
|
||||||
|
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<li>The high-level interface works like a Tk widget
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<ul>
|
||||||
|
<li>To create a new object, you call a constructor like this:
|
||||||
|
|
||||||
|
<blockquote>
|
||||||
|
<pre>
|
||||||
|
Circle c 10 # c becomes a name for the Circle object
|
||||||
|
</pre>
|
||||||
|
</blockquote>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<li>To access member data, use cget and configure methods.
|
||||||
|
For example:
|
||||||
|
|
||||||
|
<blockquote>
|
||||||
|
<pre>
|
||||||
|
c configure -x 15 ;# Set member data
|
||||||
|
set x [c cget -x] ;# Get member data
|
||||||
|
</pre>
|
||||||
|
</blockquote>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<li>To invoke a member function, you simply do this
|
||||||
|
|
||||||
|
<blockquote>
|
||||||
|
<pre>
|
||||||
|
puts "The area is [c area]"
|
||||||
|
</pre>
|
||||||
|
</blockquote>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<li>To invoke a destructor, simply destroy the object name like this:
|
||||||
|
|
||||||
|
<blockquote>
|
||||||
|
<pre>
|
||||||
|
rename c "" # c goes away
|
||||||
|
</pre>
|
||||||
|
</blockquote>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<li>Static member variables are wrapped as C global variables. For example:
|
||||||
|
|
||||||
|
<blockquote>
|
||||||
|
<pre>
|
||||||
|
set n $Shape_nshapes # Get a static data member
|
||||||
|
set Shapes_nshapes 13 # Set a static data member
|
||||||
|
</pre>
|
||||||
|
</blockquote>
|
||||||
|
|
||||||
|
</ul>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<h2>General Comments</h2>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<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.
|
||||||
|
|
||||||
|
<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>
|
||||||
|
|
@ -18,6 +18,7 @@ be used to wrap a C function and a global variable.
|
||||||
certain C declarations are turned into constants.
|
certain C declarations are turned into constants.
|
||||||
<li><a href="variables/index.html">variables</a>. How SWIG can be used to wrap C global variables.
|
<li><a href="variables/index.html">variables</a>. How SWIG can be used to wrap C global variables.
|
||||||
<li><a href="value/index.html">value</a>. How to pass and return structures by value.
|
<li><a href="value/index.html">value</a>. How to pass and return structures by value.
|
||||||
|
<li><a href="class/index.html">class</a>. How wrap a simple C++ class.
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<h2>Compilation Issues</h2>
|
<h2>Compilation Issues</h2>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue