The great merge

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@4141 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Dave Beazley 2002-11-30 22:01:28 +00:00
commit 12a43edc2d
1508 changed files with 125983 additions and 44037 deletions

View file

@ -0,0 +1,10 @@
example.py
*_wrap.c
*_wrap.cxx
*.dll
*.dsw
*.ncb
*.opt
*.plg
Release
Debug

View file

@ -0,0 +1,21 @@
TOP = ../..
SWIG = $(TOP)/../swig
CXXSRCS =
TARGET = example
INTERFACE = example.i
LIBS = -lm
SWIGOPT =
all::
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' python_cpp
static::
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
SWIGOPT='$(SWIGOPT)' TARGET='mypython' INTERFACE='$(INTERFACE)' python_cpp_static
clean::
$(MAKE) -f $(TOP)/Makefile python_clean
rm -f $(TARGET).py
check: all

View file

@ -0,0 +1,51 @@
/* File : example.h */
// A simple exception
class EmptyError { };
class FullError {
public:
int maxsize;
FullError(int m) : maxsize(m) { }
};
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() throw(EmptyError) {
T x;
if (nitems == 0) throw EmptyError();
x = items[(last + maxsize - nitems) % maxsize];
nitems--;
return x;
}
int length() {
return nitems;
}
};

View file

@ -0,0 +1,83 @@
/* This is a rather sophisticated example that illustrates exception handling,
templates, and shadow classes.
(i) The %exception directive is used to attach exception handlers
to specific methods.
(ii) Exception classes are automatically converted to shadow 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.
*/
%exception *::enqueue {
try {
$action
} catch(FullError e) {
FullError *ecopy = new FullError(e);
PyObject *err = SWIG_NewPointerObj(ecopy, SWIGTYPE_p_FullError, 1);
PyErr_SetObject((PyObject *) SWIGTYPE_p_FullError->clientdata, err);
return NULL;
}
}
/* 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 shadow 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 shadow 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.
*/
%exception *::dequeue {
try {
$action
} catch(EmptyError e) {
EmptyError *ecopy = new EmptyError(e);
PyObject *err = SWIG_NewPointerObj(ecopy, SWIGTYPE_p_EmptyError, 1);
PyErr_SetObject((PyObject *) SWIGTYPE_p_EmptyError->clientdata, err);
return NULL;
}
}
/* Grab the original header file */
%include "example.h"
/* Instantiate a few templates */
%template(intQueue) Queue<int>;
%template(doubleQueue) Queue<double>;

View 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