shadow->proxy terminology update
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@10400 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
33e41c1070
commit
e0889b6121
10 changed files with 1 additions and 172 deletions
|
|
@ -1,6 +1,6 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
CXXSRCS = example.cxx
|
||||
CXXSRCS =
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS = -lm
|
||||
54
Examples/python/exceptproxy/example.h
Normal file
54
Examples/python/exceptproxy/example.h
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
/* File : example.h */
|
||||
|
||||
// A simple exception
|
||||
class EmptyError { };
|
||||
class FullError {
|
||||
public:
|
||||
int maxsize;
|
||||
FullError(int m) : maxsize(m) { }
|
||||
};
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(disable: 4290) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
|
||||
#endif
|
||||
|
||||
template<typename T> class Queue {
|
||||
int maxsize;
|
||||
T *items;
|
||||
int nitems;
|
||||
int last;
|
||||
public:
|
||||
Queue(int size) {
|
||||
maxsize = size;
|
||||
items = new T[size];
|
||||
nitems = 0;
|
||||
last = 0;
|
||||
}
|
||||
~Queue() {
|
||||
delete [] items;
|
||||
}
|
||||
void enqueue(T x) throw(FullError) {
|
||||
if (nitems == maxsize) {
|
||||
throw FullError(maxsize);
|
||||
}
|
||||
items[last] = x;
|
||||
last = (last + 1) % maxsize;
|
||||
nitems++;
|
||||
}
|
||||
T dequeue() {
|
||||
T x;
|
||||
if (nitems == 0) throw EmptyError();
|
||||
x = items[(last + maxsize - nitems) % maxsize];
|
||||
nitems--;
|
||||
return x;
|
||||
}
|
||||
int length() {
|
||||
return nitems;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(default: 4290) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
|
||||
#endif
|
||||
|
||||
114
Examples/python/exceptproxy/example.i
Normal file
114
Examples/python/exceptproxy/example.i
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
/* This is a rather sophisticated example that illustrates exception handling,
|
||||
templates, and proxy classes.
|
||||
|
||||
(i) The %exception directive is used to attach exception handlers
|
||||
to specific methods.
|
||||
|
||||
(ii) Exception classes are automatically converted to proxy class
|
||||
objects.
|
||||
|
||||
(iii) The %template directive is used to expand the templates
|
||||
*/
|
||||
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* Define some exception handlers for specific methods. In
|
||||
the header file, the enqueue method throws FullError and
|
||||
the dequeue method throws EmptyError. Since we don't
|
||||
want to define an exception handler for everything, we
|
||||
simply write a handler each method individually.
|
||||
|
||||
Note: the *::enqueue syntax means that we simply define
|
||||
the handler for any class with this method defined.
|
||||
*/
|
||||
|
||||
/*
|
||||
First we need to 'disable' the default swig throw mechanism for the
|
||||
FullError class. We do this by rethrowing the exception.
|
||||
|
||||
Note that this is necessary since the class appears in a throw
|
||||
declaration:
|
||||
|
||||
|
||||
void enqueue(T x) throw(FullError);
|
||||
|
||||
hence, swig recognizes it as an exception class and it will generate
|
||||
the necessary code to catch it and rethrow it to the python side.
|
||||
|
||||
*/
|
||||
%typemap(throws) FullError "(void)$1; throw;";
|
||||
|
||||
|
||||
%exception *::enqueue {
|
||||
try {
|
||||
$action
|
||||
} catch(FullError& e) {
|
||||
FullError *ecopy = new FullError(e);
|
||||
PyObject *err = SWIG_NewPointerObj(ecopy, SWIGTYPE_p_FullError, 1);
|
||||
PyErr_SetObject(SWIG_Python_ExceptionType(SWIGTYPE_p_FullError), err);
|
||||
SWIG_fail;
|
||||
}
|
||||
}
|
||||
|
||||
/* Some notes about the code above:
|
||||
|
||||
(0) $action gets replaced with the actual method call.
|
||||
|
||||
(1) We are going to return a copy of the exception object (FullError)
|
||||
to pass back to the Python interpreter. This is why the copy
|
||||
constructor is being called.
|
||||
|
||||
(2) The SWIG_NewPointerObj() call automatically wraps the exception object
|
||||
into a proxy class. The SWIGTYPE_p_FullError is the type-descriptor
|
||||
used for type checking. The "1" indicates that Python will have
|
||||
ownership of the resulting object.
|
||||
|
||||
(3) The PyErr_SetObject call sets the Python exception. However,
|
||||
the SWIGTYPE_p_FullError->clientdata reference may not be
|
||||
obvious. This is actually the Python proxy class object
|
||||
for FullError. Recall that in Python, exceptions are defined
|
||||
as classes. Therefore, this works perfectly as the argument to
|
||||
PyErr_SetObject()! A neat trick perhaps.
|
||||
*/
|
||||
|
||||
/*
|
||||
Now, the EmpytError doesn't appear in a throw declaration, and hence
|
||||
we need to 'mark' it as an exception class. In python, classes that
|
||||
are used as exception are 'special', and need to be wrapped as
|
||||
'classic' ones.
|
||||
|
||||
This is a python issue, and if you don't mark the class, you will
|
||||
see 'interesting' behaviours at the python side.
|
||||
|
||||
|
||||
*/
|
||||
%exceptionclass EmptyError;
|
||||
%exceptionclass FullError;
|
||||
|
||||
%exception *::dequeue {
|
||||
try {
|
||||
$action
|
||||
} catch(EmptyError& e) {
|
||||
EmptyError *ecopy = new EmptyError(e);
|
||||
PyObject *err = SWIG_NewPointerObj(ecopy, SWIGTYPE_p_EmptyError, 1);
|
||||
PyErr_SetObject(SWIG_Python_ExceptionType(SWIGTYPE_p_EmptyError), err);
|
||||
SWIG_fail;
|
||||
}
|
||||
}
|
||||
|
||||
/* Grab the original header file */
|
||||
%include "example.h"
|
||||
|
||||
/* Instantiate a few templates */
|
||||
|
||||
%template(intQueue) Queue<int>;
|
||||
%template(doubleQueue) Queue<double>;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
45
Examples/python/exceptproxy/runme.py
Normal file
45
Examples/python/exceptproxy/runme.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
# file: runme.py
|
||||
import example
|
||||
|
||||
q = example.intQueue(10)
|
||||
|
||||
print "Inserting items into intQueue"
|
||||
|
||||
try:
|
||||
for i in range(0,100):
|
||||
q.enqueue(i)
|
||||
except example.FullError,e:
|
||||
print "Maxsize is", e.maxsize
|
||||
|
||||
print "Removing items"
|
||||
|
||||
try:
|
||||
while 1:
|
||||
q.dequeue()
|
||||
except example.EmptyError,e:
|
||||
pass
|
||||
|
||||
|
||||
q = example.doubleQueue(1000)
|
||||
|
||||
print "Inserting items into doubleQueue"
|
||||
|
||||
try:
|
||||
for i in range(0,10000):
|
||||
q.enqueue(i*1.5)
|
||||
except example.FullError,e:
|
||||
print "Maxsize is", e.maxsize
|
||||
|
||||
print "Removing items"
|
||||
|
||||
try:
|
||||
while 1:
|
||||
q.dequeue()
|
||||
except example.EmptyError,e:
|
||||
pass
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -1,31 +0,0 @@
|
|||
/* File : example.c */
|
||||
|
||||
#include "example.h"
|
||||
#include <math.h>
|
||||
#ifndef M_PI
|
||||
# define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
|
@ -1,39 +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() = 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();
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* Let's just grab the original header file here */
|
||||
%include "example.h"
|
||||
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>SWIG:Examples:python:proxy</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff">
|
||||
|
||||
|
||||
<tt>SWIG/Examples/python/proxy/</tt>
|
||||
<hr>
|
||||
|
||||
<H2>Wrapping a simple C++ class</H2>
|
||||
|
||||
<p>
|
||||
This example illustrates the wrapping of some C++ classes by proxy classes.
|
||||
|
||||
<hr>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -1,51 +0,0 @@
|
|||
# file: runme.py
|
||||
|
||||
# This file illustrates the proxy 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…
Reference in a new issue