new example
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@869 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
ff2943a35d
commit
4baedcf650
6 changed files with 168 additions and 0 deletions
19
Examples/python/shadow/Makefile
Normal file
19
Examples/python/shadow/Makefile
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
TOP = ../..
|
||||||
|
SWIG = $(TOP)/../swig -shadow
|
||||||
|
CXXSRCS = example.cxx
|
||||||
|
TARGET = examplec
|
||||||
|
INTERFACE = example.i
|
||||||
|
LIBS = -lm
|
||||||
|
|
||||||
|
all::
|
||||||
|
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||||
|
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' python_cpp
|
||||||
|
|
||||||
|
static::
|
||||||
|
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||||
|
TARGET='mypython' INTERFACE='$(INTERFACE)' python_cpp_static
|
||||||
|
|
||||||
|
clean::
|
||||||
|
rm -f *_wrap* *.o *~ *.so mypython *.pyc .~* core
|
||||||
|
|
||||||
|
check: all
|
||||||
28
Examples/python/shadow/example.cxx
Normal file
28
Examples/python/shadow/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/python/shadow/example.h
Normal file
39
Examples/python/shadow/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();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
10
Examples/python/shadow/example.i
Normal file
10
Examples/python/shadow/example.i
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
/* File : example.i */
|
||||||
|
%module example
|
||||||
|
|
||||||
|
%{
|
||||||
|
#include "example.h"
|
||||||
|
%}
|
||||||
|
|
||||||
|
/* Let's just grab the original header file here */
|
||||||
|
%include "example.h"
|
||||||
|
|
||||||
21
Examples/python/shadow/index.html
Normal file
21
Examples/python/shadow/index.html
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>SWIG:Examples:python:shadow</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body bgcolor="#ffffff">
|
||||||
|
|
||||||
|
|
||||||
|
<tt>SWIG/Examples/python/shadow/</tt>
|
||||||
|
<hr>
|
||||||
|
|
||||||
|
<H2>Wrapping a simple C++ class</H2>
|
||||||
|
|
||||||
|
<tt>$Header$</tt><br>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
This example illustrates the wrapping of some C++ classes by shadow classes.
|
||||||
|
|
||||||
|
<hr>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
51
Examples/python/shadow/runme.py
Normal file
51
Examples/python/shadow/runme.py
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
# file: runme.py
|
||||||
|
|
||||||
|
# This file illustrates the shadow-class C++ interface generated
|
||||||
|
# by SWIG.
|
||||||
|
|
||||||
|
import example
|
||||||
|
|
||||||
|
# ----- Object creation -----
|
||||||
|
|
||||||
|
print "Creating some objects:"
|
||||||
|
c = example.Circle(10)
|
||||||
|
print " Created circle", c
|
||||||
|
s = example.Square(10)
|
||||||
|
print " Created square", s
|
||||||
|
|
||||||
|
# ----- Access a static member -----
|
||||||
|
|
||||||
|
print "\nA total of", example.cvar.Shape_nshapes,"shapes were created"
|
||||||
|
|
||||||
|
# ----- Member data access -----
|
||||||
|
|
||||||
|
# Set the location of the object
|
||||||
|
|
||||||
|
c.x = 20
|
||||||
|
c.y = 30
|
||||||
|
|
||||||
|
s.x = -10
|
||||||
|
s.y = 5
|
||||||
|
|
||||||
|
print "\nHere is their current position:"
|
||||||
|
print " Circle = (%f, %f)" % (c.x,c.y)
|
||||||
|
print " Square = (%f, %f)" % (s.x,s.y)
|
||||||
|
|
||||||
|
# ----- Call some methods -----
|
||||||
|
|
||||||
|
print "\nHere are some properties of the shapes:"
|
||||||
|
for o in [c,s]:
|
||||||
|
print " ", o
|
||||||
|
print " area = ", o.area()
|
||||||
|
print " perimeter = ", o.perimeter()
|
||||||
|
|
||||||
|
print "\nGuess I'll clean up now"
|
||||||
|
|
||||||
|
# Note: this invokes the virtual destructor
|
||||||
|
del c
|
||||||
|
del s
|
||||||
|
|
||||||
|
s = 3
|
||||||
|
print example.cvar.Shape_nshapes,"shapes remain"
|
||||||
|
print "Goodbye"
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue