From 2824b0cbb66e715490e1ef13250bd675d87b32d9 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Wed, 2 Jun 2010 20:53:17 +0000 Subject: rel-2.0.0 git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/tags/rel-2.0.0@12089 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- trunk/Examples/python/callback/Makefile | 22 +++ trunk/Examples/python/callback/example.cxx | 4 + trunk/Examples/python/callback/example.h | 22 +++ trunk/Examples/python/callback/example.i | 13 ++ trunk/Examples/python/callback/index.html | 19 +++ trunk/Examples/python/callback/runme.py | 56 +++++++ trunk/Examples/python/check.list | 29 ++++ trunk/Examples/python/class/Makefile | 21 +++ trunk/Examples/python/class/example.cxx | 28 ++++ trunk/Examples/python/class/example.dsp | 152 +++++++++++++++++ trunk/Examples/python/class/example.h | 39 +++++ trunk/Examples/python/class/example.i | 10 ++ trunk/Examples/python/class/index.html | 216 +++++++++++++++++++++++++ trunk/Examples/python/class/runme.py | 51 ++++++ trunk/Examples/python/constants/Makefile | 20 +++ trunk/Examples/python/constants/example.i | 27 ++++ trunk/Examples/python/constants/index.html | 67 ++++++++ trunk/Examples/python/constants/runme.py | 27 ++++ trunk/Examples/python/contract/Makefile | 20 +++ trunk/Examples/python/contract/example.c | 23 +++ trunk/Examples/python/contract/example.dsp | 148 +++++++++++++++++ trunk/Examples/python/contract/example.i | 21 +++ trunk/Examples/python/contract/runme.py | 30 ++++ trunk/Examples/python/docstrings/Makefile | 24 +++ trunk/Examples/python/docstrings/example.cxx | 4 + trunk/Examples/python/docstrings/example.h | 4 + trunk/Examples/python/docstrings/example.i | 14 ++ trunk/Examples/python/docstrings/runme.py | 6 + trunk/Examples/python/enum/Makefile | 21 +++ trunk/Examples/python/enum/example.cxx | 37 +++++ trunk/Examples/python/enum/example.h | 13 ++ trunk/Examples/python/enum/example.i | 11 ++ trunk/Examples/python/enum/index.html | 35 ++++ trunk/Examples/python/enum/runme.py | 31 ++++ trunk/Examples/python/exception/Makefile | 21 +++ trunk/Examples/python/exception/example.h | 53 ++++++ trunk/Examples/python/exception/example.i | 12 ++ trunk/Examples/python/exception/runme.py | 36 +++++ trunk/Examples/python/exceptproxy/Makefile | 22 +++ trunk/Examples/python/exceptproxy/example.h | 54 +++++++ trunk/Examples/python/exceptproxy/example.i | 114 +++++++++++++ trunk/Examples/python/exceptproxy/runme.py | 45 ++++++ trunk/Examples/python/extend/Makefile | 22 +++ trunk/Examples/python/extend/example.cxx | 4 + trunk/Examples/python/extend/example.h | 56 +++++++ trunk/Examples/python/extend/example.i | 15 ++ trunk/Examples/python/extend/index.html | 19 +++ trunk/Examples/python/extend/runme.py | 81 ++++++++++ trunk/Examples/python/funcptr/Makefile | 20 +++ trunk/Examples/python/funcptr/example.c | 19 +++ trunk/Examples/python/funcptr/example.h | 9 ++ trunk/Examples/python/funcptr/example.i | 16 ++ trunk/Examples/python/funcptr/index.html | 90 +++++++++++ trunk/Examples/python/funcptr/runme.py | 20 +++ trunk/Examples/python/funcptr2/Makefile | 20 +++ trunk/Examples/python/funcptr2/example.c | 19 +++ trunk/Examples/python/funcptr2/example.h | 9 ++ trunk/Examples/python/funcptr2/example.i | 18 +++ trunk/Examples/python/funcptr2/runme.py | 24 +++ trunk/Examples/python/functor/Makefile | 22 +++ trunk/Examples/python/functor/example.i | 29 ++++ trunk/Examples/python/functor/runme.py | 17 ++ trunk/Examples/python/import/Makefile | 22 +++ trunk/Examples/python/import/README | 39 +++++ trunk/Examples/python/import/bar.dsp | 144 +++++++++++++++++ trunk/Examples/python/import/bar.h | 22 +++ trunk/Examples/python/import/bar.i | 9 ++ trunk/Examples/python/import/base.dsp | 144 +++++++++++++++++ trunk/Examples/python/import/base.h | 18 +++ trunk/Examples/python/import/base.i | 6 + trunk/Examples/python/import/example.dsw | 65 ++++++++ trunk/Examples/python/import/foo.dsp | 144 +++++++++++++++++ trunk/Examples/python/import/foo.h | 21 +++ trunk/Examples/python/import/foo.i | 8 + trunk/Examples/python/import/runme.py | 111 +++++++++++++ trunk/Examples/python/import/spam.dsp | 144 +++++++++++++++++ trunk/Examples/python/import/spam.h | 24 +++ trunk/Examples/python/import/spam.i | 9 ++ trunk/Examples/python/import_template/Makefile | 22 +++ trunk/Examples/python/import_template/README | 30 ++++ trunk/Examples/python/import_template/bar.h | 22 +++ trunk/Examples/python/import_template/bar.i | 11 ++ trunk/Examples/python/import_template/base.h | 18 +++ trunk/Examples/python/import_template/base.i | 7 + trunk/Examples/python/import_template/foo.h | 21 +++ trunk/Examples/python/import_template/foo.i | 10 ++ trunk/Examples/python/import_template/runme.py | 111 +++++++++++++ trunk/Examples/python/import_template/spam.h | 24 +++ trunk/Examples/python/import_template/spam.i | 10 ++ trunk/Examples/python/index.html | 112 +++++++++++++ trunk/Examples/python/java/Example.java | 29 ++++ trunk/Examples/python/java/Makefile | 25 +++ trunk/Examples/python/java/example.i | 9 ++ trunk/Examples/python/java/runme.py | 16 ++ trunk/Examples/python/libffi/Makefile | 20 +++ trunk/Examples/python/libffi/example.i | 176 ++++++++++++++++++++ trunk/Examples/python/multimap/Makefile | 20 +++ trunk/Examples/python/multimap/example.c | 53 ++++++ trunk/Examples/python/multimap/example.dsp | 148 +++++++++++++++++ trunk/Examples/python/multimap/example.i | 121 ++++++++++++++ trunk/Examples/python/multimap/runme.py | 27 ++++ trunk/Examples/python/operator/Makefile | 22 +++ trunk/Examples/python/operator/example.h | 36 +++++ trunk/Examples/python/operator/example.i | 28 ++++ trunk/Examples/python/operator/runme.py | 21 +++ trunk/Examples/python/pointer/Makefile | 20 +++ trunk/Examples/python/pointer/example.c | 16 ++ trunk/Examples/python/pointer/example.i | 30 ++++ trunk/Examples/python/pointer/index.html | 171 ++++++++++++++++++++ trunk/Examples/python/pointer/runme.py | 44 +++++ trunk/Examples/python/reference/Makefile | 21 +++ trunk/Examples/python/reference/example.cxx | 46 ++++++ trunk/Examples/python/reference/example.h | 26 +++ trunk/Examples/python/reference/example.i | 48 ++++++ trunk/Examples/python/reference/index.html | 147 +++++++++++++++++ trunk/Examples/python/reference/runme.py | 63 ++++++++ trunk/Examples/python/simple/Makefile | 20 +++ trunk/Examples/python/simple/example.c | 18 +++ trunk/Examples/python/simple/example.dsp | 148 +++++++++++++++++ trunk/Examples/python/simple/example.i | 7 + trunk/Examples/python/simple/index.html | 97 +++++++++++ trunk/Examples/python/simple/runme.py | 30 ++++ trunk/Examples/python/smartptr/Makefile | 22 +++ trunk/Examples/python/smartptr/example.cxx | 31 ++++ trunk/Examples/python/smartptr/example.h | 39 +++++ trunk/Examples/python/smartptr/example.i | 20 +++ trunk/Examples/python/smartptr/runme.py | 55 +++++++ trunk/Examples/python/smartptr/smartptr.h | 13 ++ trunk/Examples/python/std_map/Makefile | 25 +++ trunk/Examples/python/std_map/example.h | 17 ++ trunk/Examples/python/std_map/example.i | 27 ++++ trunk/Examples/python/std_map/runme.py | 82 ++++++++++ trunk/Examples/python/std_vector/Makefile | 22 +++ trunk/Examples/python/std_vector/example.h | 25 +++ trunk/Examples/python/std_vector/example.i | 17 ++ trunk/Examples/python/std_vector/runme.py | 36 +++++ trunk/Examples/python/swigrun/Makefile | 25 +++ trunk/Examples/python/swigrun/example.cxx | 20 +++ trunk/Examples/python/swigrun/example.h | 58 +++++++ trunk/Examples/python/swigrun/example.i | 15 ++ trunk/Examples/python/swigrun/runme.py | 28 ++++ trunk/Examples/python/template/Makefile | 22 +++ trunk/Examples/python/template/example.h | 32 ++++ trunk/Examples/python/template/example.i | 17 ++ trunk/Examples/python/template/runme.py | 34 ++++ trunk/Examples/python/varargs/Makefile | 20 +++ trunk/Examples/python/varargs/example.i | 65 ++++++++ trunk/Examples/python/varargs/runme.py | 34 ++++ trunk/Examples/python/variables/Makefile | 20 +++ trunk/Examples/python/variables/example.c | 91 +++++++++++ trunk/Examples/python/variables/example.h | 6 + trunk/Examples/python/variables/example.i | 51 ++++++ trunk/Examples/python/variables/index.html | 100 ++++++++++++ trunk/Examples/python/variables/runme.py | 75 +++++++++ 154 files changed, 6326 insertions(+) create mode 100644 trunk/Examples/python/callback/Makefile create mode 100644 trunk/Examples/python/callback/example.cxx create mode 100644 trunk/Examples/python/callback/example.h create mode 100644 trunk/Examples/python/callback/example.i create mode 100644 trunk/Examples/python/callback/index.html create mode 100644 trunk/Examples/python/callback/runme.py create mode 100644 trunk/Examples/python/check.list create mode 100644 trunk/Examples/python/class/Makefile create mode 100644 trunk/Examples/python/class/example.cxx create mode 100644 trunk/Examples/python/class/example.dsp create mode 100644 trunk/Examples/python/class/example.h create mode 100644 trunk/Examples/python/class/example.i create mode 100644 trunk/Examples/python/class/index.html create mode 100644 trunk/Examples/python/class/runme.py create mode 100644 trunk/Examples/python/constants/Makefile create mode 100644 trunk/Examples/python/constants/example.i create mode 100644 trunk/Examples/python/constants/index.html create mode 100644 trunk/Examples/python/constants/runme.py create mode 100644 trunk/Examples/python/contract/Makefile create mode 100644 trunk/Examples/python/contract/example.c create mode 100644 trunk/Examples/python/contract/example.dsp create mode 100644 trunk/Examples/python/contract/example.i create mode 100644 trunk/Examples/python/contract/runme.py create mode 100644 trunk/Examples/python/docstrings/Makefile create mode 100644 trunk/Examples/python/docstrings/example.cxx create mode 100644 trunk/Examples/python/docstrings/example.h create mode 100644 trunk/Examples/python/docstrings/example.i create mode 100644 trunk/Examples/python/docstrings/runme.py create mode 100644 trunk/Examples/python/enum/Makefile create mode 100644 trunk/Examples/python/enum/example.cxx create mode 100644 trunk/Examples/python/enum/example.h create mode 100644 trunk/Examples/python/enum/example.i create mode 100644 trunk/Examples/python/enum/index.html create mode 100644 trunk/Examples/python/enum/runme.py create mode 100644 trunk/Examples/python/exception/Makefile create mode 100644 trunk/Examples/python/exception/example.h create mode 100644 trunk/Examples/python/exception/example.i create mode 100644 trunk/Examples/python/exception/runme.py create mode 100644 trunk/Examples/python/exceptproxy/Makefile create mode 100644 trunk/Examples/python/exceptproxy/example.h create mode 100644 trunk/Examples/python/exceptproxy/example.i create mode 100644 trunk/Examples/python/exceptproxy/runme.py create mode 100644 trunk/Examples/python/extend/Makefile create mode 100644 trunk/Examples/python/extend/example.cxx create mode 100644 trunk/Examples/python/extend/example.h create mode 100644 trunk/Examples/python/extend/example.i create mode 100644 trunk/Examples/python/extend/index.html create mode 100644 trunk/Examples/python/extend/runme.py create mode 100644 trunk/Examples/python/funcptr/Makefile create mode 100644 trunk/Examples/python/funcptr/example.c create mode 100644 trunk/Examples/python/funcptr/example.h create mode 100644 trunk/Examples/python/funcptr/example.i create mode 100644 trunk/Examples/python/funcptr/index.html create mode 100644 trunk/Examples/python/funcptr/runme.py create mode 100644 trunk/Examples/python/funcptr2/Makefile create mode 100644 trunk/Examples/python/funcptr2/example.c create mode 100644 trunk/Examples/python/funcptr2/example.h create mode 100644 trunk/Examples/python/funcptr2/example.i create mode 100644 trunk/Examples/python/funcptr2/runme.py create mode 100644 trunk/Examples/python/functor/Makefile create mode 100644 trunk/Examples/python/functor/example.i create mode 100644 trunk/Examples/python/functor/runme.py create mode 100644 trunk/Examples/python/import/Makefile create mode 100644 trunk/Examples/python/import/README create mode 100644 trunk/Examples/python/import/bar.dsp create mode 100644 trunk/Examples/python/import/bar.h create mode 100644 trunk/Examples/python/import/bar.i create mode 100644 trunk/Examples/python/import/base.dsp create mode 100644 trunk/Examples/python/import/base.h create mode 100644 trunk/Examples/python/import/base.i create mode 100644 trunk/Examples/python/import/example.dsw create mode 100644 trunk/Examples/python/import/foo.dsp create mode 100644 trunk/Examples/python/import/foo.h create mode 100644 trunk/Examples/python/import/foo.i create mode 100644 trunk/Examples/python/import/runme.py create mode 100644 trunk/Examples/python/import/spam.dsp create mode 100644 trunk/Examples/python/import/spam.h create mode 100644 trunk/Examples/python/import/spam.i create mode 100644 trunk/Examples/python/import_template/Makefile create mode 100644 trunk/Examples/python/import_template/README create mode 100644 trunk/Examples/python/import_template/bar.h create mode 100644 trunk/Examples/python/import_template/bar.i create mode 100644 trunk/Examples/python/import_template/base.h create mode 100644 trunk/Examples/python/import_template/base.i create mode 100644 trunk/Examples/python/import_template/foo.h create mode 100644 trunk/Examples/python/import_template/foo.i create mode 100644 trunk/Examples/python/import_template/runme.py create mode 100644 trunk/Examples/python/import_template/spam.h create mode 100644 trunk/Examples/python/import_template/spam.i create mode 100644 trunk/Examples/python/index.html create mode 100644 trunk/Examples/python/java/Example.java create mode 100644 trunk/Examples/python/java/Makefile create mode 100644 trunk/Examples/python/java/example.i create mode 100644 trunk/Examples/python/java/runme.py create mode 100644 trunk/Examples/python/libffi/Makefile create mode 100644 trunk/Examples/python/libffi/example.i create mode 100644 trunk/Examples/python/multimap/Makefile create mode 100644 trunk/Examples/python/multimap/example.c create mode 100644 trunk/Examples/python/multimap/example.dsp create mode 100644 trunk/Examples/python/multimap/example.i create mode 100644 trunk/Examples/python/multimap/runme.py create mode 100644 trunk/Examples/python/operator/Makefile create mode 100644 trunk/Examples/python/operator/example.h create mode 100644 trunk/Examples/python/operator/example.i create mode 100644 trunk/Examples/python/operator/runme.py create mode 100644 trunk/Examples/python/pointer/Makefile create mode 100644 trunk/Examples/python/pointer/example.c create mode 100644 trunk/Examples/python/pointer/example.i create mode 100644 trunk/Examples/python/pointer/index.html create mode 100644 trunk/Examples/python/pointer/runme.py create mode 100644 trunk/Examples/python/reference/Makefile create mode 100644 trunk/Examples/python/reference/example.cxx create mode 100644 trunk/Examples/python/reference/example.h create mode 100644 trunk/Examples/python/reference/example.i create mode 100644 trunk/Examples/python/reference/index.html create mode 100644 trunk/Examples/python/reference/runme.py create mode 100644 trunk/Examples/python/simple/Makefile create mode 100644 trunk/Examples/python/simple/example.c create mode 100644 trunk/Examples/python/simple/example.dsp create mode 100644 trunk/Examples/python/simple/example.i create mode 100644 trunk/Examples/python/simple/index.html create mode 100644 trunk/Examples/python/simple/runme.py create mode 100644 trunk/Examples/python/smartptr/Makefile create mode 100644 trunk/Examples/python/smartptr/example.cxx create mode 100644 trunk/Examples/python/smartptr/example.h create mode 100644 trunk/Examples/python/smartptr/example.i create mode 100644 trunk/Examples/python/smartptr/runme.py create mode 100644 trunk/Examples/python/smartptr/smartptr.h create mode 100644 trunk/Examples/python/std_map/Makefile create mode 100644 trunk/Examples/python/std_map/example.h create mode 100644 trunk/Examples/python/std_map/example.i create mode 100644 trunk/Examples/python/std_map/runme.py create mode 100644 trunk/Examples/python/std_vector/Makefile create mode 100644 trunk/Examples/python/std_vector/example.h create mode 100644 trunk/Examples/python/std_vector/example.i create mode 100644 trunk/Examples/python/std_vector/runme.py create mode 100644 trunk/Examples/python/swigrun/Makefile create mode 100644 trunk/Examples/python/swigrun/example.cxx create mode 100644 trunk/Examples/python/swigrun/example.h create mode 100644 trunk/Examples/python/swigrun/example.i create mode 100644 trunk/Examples/python/swigrun/runme.py create mode 100644 trunk/Examples/python/template/Makefile create mode 100644 trunk/Examples/python/template/example.h create mode 100644 trunk/Examples/python/template/example.i create mode 100644 trunk/Examples/python/template/runme.py create mode 100644 trunk/Examples/python/varargs/Makefile create mode 100644 trunk/Examples/python/varargs/example.i create mode 100644 trunk/Examples/python/varargs/runme.py create mode 100644 trunk/Examples/python/variables/Makefile create mode 100644 trunk/Examples/python/variables/example.c create mode 100644 trunk/Examples/python/variables/example.h create mode 100644 trunk/Examples/python/variables/example.i create mode 100644 trunk/Examples/python/variables/index.html create mode 100644 trunk/Examples/python/variables/runme.py (limited to 'trunk/Examples/python') diff --git a/trunk/Examples/python/callback/Makefile b/trunk/Examples/python/callback/Makefile new file mode 100644 index 000000000..a29276e58 --- /dev/null +++ b/trunk/Examples/python/callback/Makefile @@ -0,0 +1,22 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = example.cxx +TARGET = example +INTERFACE = example.i +LIBS = -lm +SWIGOPT = + +all:: + $(MAKE) -f $(TOP)/Makefile $(SWIGLIB) CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' python_cpp + +static:: + $(MAKE) -f $(TOP)/Makefile $(SWIGLIB) 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 + $(MAKE) -f $(TOP)/Makefile python_run diff --git a/trunk/Examples/python/callback/example.cxx b/trunk/Examples/python/callback/example.cxx new file mode 100644 index 000000000..450d75608 --- /dev/null +++ b/trunk/Examples/python/callback/example.cxx @@ -0,0 +1,4 @@ +/* File : example.cxx */ + +#include "example.h" + diff --git a/trunk/Examples/python/callback/example.h b/trunk/Examples/python/callback/example.h new file mode 100644 index 000000000..2a0194999 --- /dev/null +++ b/trunk/Examples/python/callback/example.h @@ -0,0 +1,22 @@ +/* File : example.h */ + +#include + +class Callback { +public: + virtual ~Callback() { std::cout << "Callback::~Callback()" << std:: endl; } + virtual void run() { std::cout << "Callback::run()" << std::endl; } +}; + + +class Caller { +private: + Callback *_callback; +public: + Caller(): _callback(0) {} + ~Caller() { delCallback(); } + void delCallback() { delete _callback; _callback = 0; } + void setCallback(Callback *cb) { delCallback(); _callback = cb; } + void call() { if (_callback) _callback->run(); } +}; + diff --git a/trunk/Examples/python/callback/example.i b/trunk/Examples/python/callback/example.i new file mode 100644 index 000000000..90beda01a --- /dev/null +++ b/trunk/Examples/python/callback/example.i @@ -0,0 +1,13 @@ +/* File : example.i */ +%module(directors="1") example +%{ +#include "example.h" +%} + +%include "std_string.i" + +/* turn on director wrapping Callback */ +%feature("director") Callback; + +%include "example.h" + diff --git a/trunk/Examples/python/callback/index.html b/trunk/Examples/python/callback/index.html new file mode 100644 index 000000000..e2017eca3 --- /dev/null +++ b/trunk/Examples/python/callback/index.html @@ -0,0 +1,19 @@ + + +SWIG:Examples:python:callback + + + + + +SWIG/Examples/python/extend/ +
+ +

Implementing C++ callbacks in Python

+ +

+This example illustrates how to use directors to implement C++ callbacks in Python. + +


+ + diff --git a/trunk/Examples/python/callback/runme.py b/trunk/Examples/python/callback/runme.py new file mode 100644 index 000000000..026e9520b --- /dev/null +++ b/trunk/Examples/python/callback/runme.py @@ -0,0 +1,56 @@ +# file: runme.py + +# This file illustrates the cross language polymorphism using directors. + +import example + + +class PyCallback(example.Callback): + def __init__(self): + example.Callback.__init__(self) + def run(self): + print "PyCallback.run()" + +# Create an Caller instance + +caller = example.Caller() + +# Add a simple C++ callback (caller owns the callback, so +# we disown it first by clearing the .thisown flag). + +print "Adding and calling a normal C++ callback" +print "----------------------------------------" + +callback = example.Callback() +callback.thisown = 0 +caller.setCallback(callback) +caller.call() +caller.delCallback(); + +print +print "Adding and calling a Python callback" +print "------------------------------------" + +# Add a Python callback (caller owns the callback, so we +# disown it first by calling __disown__). + +caller.setCallback(PyCallback().__disown__()) +caller.call() +caller.delCallback() + +print +print "Adding and calling another Python callback" +print "------------------------------------------" + +# Lets do the same but use the weak reference this time. + +callback = PyCallback().__disown__() +caller.setCallback(callback) +caller.call() +caller.delCallback() + +# All done. + +print +print "python exit" + diff --git a/trunk/Examples/python/check.list b/trunk/Examples/python/check.list new file mode 100644 index 000000000..7cfe437f0 --- /dev/null +++ b/trunk/Examples/python/check.list @@ -0,0 +1,29 @@ +# see top-level Makefile.in +callback +class +constants +contract +docstrings +enum +exception +exceptproxy +extend +funcptr +funcptr2 +functor +import +import_template +java +#libffi +multimap +operator +pointer +reference +simple +smartptr +std_vector +std_map +swigrun +template +varargs +variables diff --git a/trunk/Examples/python/class/Makefile b/trunk/Examples/python/class/Makefile new file mode 100644 index 000000000..74625b992 --- /dev/null +++ b/trunk/Examples/python/class/Makefile @@ -0,0 +1,21 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = example.cxx +TARGET = example +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:: + $(MAKE) -f $(TOP)/Makefile python_clean + rm -f $(TARGET).py + +check: all + $(MAKE) -f $(TOP)/Makefile python_run diff --git a/trunk/Examples/python/class/example.cxx b/trunk/Examples/python/class/example.cxx new file mode 100644 index 000000000..1e8e203dd --- /dev/null +++ b/trunk/Examples/python/class/example.cxx @@ -0,0 +1,28 @@ +/* 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; +} diff --git a/trunk/Examples/python/class/example.dsp b/trunk/Examples/python/class/example.dsp new file mode 100644 index 000000000..fd7bf8c06 --- /dev/null +++ b/trunk/Examples/python/class/example.dsp @@ -0,0 +1,152 @@ +# Microsoft Developer Studio Project File - Name="example" - Package Owner=<4> +# Microsoft Developer Studio Generated Build File, Format Version 6.00 +# ** DO NOT EDIT ** + +# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102 + +CFG=example - Win32 Release +!MESSAGE This is not a valid makefile. To build this project using NMAKE, +!MESSAGE use the Export Makefile command and run +!MESSAGE +!MESSAGE NMAKE /f "example.mak". +!MESSAGE +!MESSAGE You can specify a configuration when running NMAKE +!MESSAGE by defining the macro CFG on the command line. For example: +!MESSAGE +!MESSAGE NMAKE /f "example.mak" CFG="example - Win32 Release" +!MESSAGE +!MESSAGE Possible choices for configuration are: +!MESSAGE +!MESSAGE "example - Win32 Debug" (based on "Win32 (x86) Dynamic-Link Library") +!MESSAGE "example - Win32 Release" (based on "Win32 (x86) Dynamic-Link Library") +!MESSAGE + +# Begin Project +# PROP AllowPerConfigDependencies 0 +# PROP Scc_ProjName "" +# PROP Scc_LocalPath "" +CPP=cl.exe +MTL=midl.exe +RSC=rc.exe + +!IF "$(CFG)" == "example - Win32 Debug" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 1 +# PROP BASE Output_Dir "Debug" +# PROP BASE Intermediate_Dir "Debug" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 1 +# PROP Output_Dir "Debug" +# PROP Intermediate_Dir "Debug" +# PROP Ignore_Export_Lib 0 +# PROP Target_Dir "" +# ADD BASE CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /YX /FD /GZ /c +# ADD CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /I "$(PYTHON_INCLUDE)" /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /YX /FD /GZ /c +# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32 +# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32 +# ADD BASE RSC /l 0x809 /d "_DEBUG" +# ADD RSC /l 0x809 /d "_DEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib /nologo /dll /debug /machine:I386 /pdbtype:sept +# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib "$(PYTHON_LIB)" /nologo /dll /debug /machine:I386 /out:"_example.pyd" /pdbtype:sept + +!ELSEIF "$(CFG)" == "example - Win32 Release" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 0 +# PROP BASE Output_Dir "Release" +# PROP BASE Intermediate_Dir "Release" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 0 +# PROP Output_Dir "Release" +# PROP Intermediate_Dir "Release" +# PROP Ignore_Export_Lib 0 +# PROP Target_Dir "" +# ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /YX /FD /c +# ADD CPP /nologo /MT /W3 /GX /O2 /I "$(PYTHON_INCLUDE)" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /YX /FD /c +# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32 +# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32 +# ADD BASE RSC /l 0x809 /d "NDEBUG" +# ADD RSC /l 0x809 /d "NDEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib /nologo /dll /machine:I386 +# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib "$(PYTHON_LIB)" /nologo /dll /machine:I386 /out:"_example.pyd" + +!ENDIF + +# Begin Target + +# Name "example - Win32 Debug" +# Name "example - Win32 Release" +# Begin Group "Source Files" + +# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" +# Begin Source File + +SOURCE=.\example.cxx +# End Source File +# Begin Source File + +SOURCE=.\example_wrap.cxx +# End Source File +# End Group +# Begin Group "Header Files" + +# PROP Default_Filter "h;hpp;hxx;hm;inl" +# Begin Source File + +SOURCE=.\example.h +# End Source File +# End Group +# Begin Group "Resource Files" + +# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe" +# End Group +# Begin Source File + +SOURCE=.\example.i + +!IF "$(CFG)" == "example - Win32 Debug" + +# Begin Custom Build +InputPath=.\example.i +InputName=example + +"$(InputName)_wrap.cxx" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)" + echo In order to function correctly, please ensure the following environment variables are correctly set: + echo PYTHON_INCLUDE: %PYTHON_INCLUDE% + echo PYTHON_LIB: %PYTHON_LIB% + echo on + ..\..\..\swig.exe -c++ -python $(InputPath) + +# End Custom Build + +!ELSEIF "$(CFG)" == "example - Win32 Release" + +# Begin Custom Build +InputPath=.\example.i +InputName=example + +"$(InputName)_wrap.cxx" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)" + echo In order to function correctly, please ensure the following environment variables are correctly set: + echo PYTHON_INCLUDE: %PYTHON_INCLUDE% + echo PYTHON_LIB: %PYTHON_LIB% + echo on + ..\..\..\swig.exe -c++ -python $(InputPath) + +# End Custom Build + +!ENDIF + +# End Source File +# End Target +# End Project diff --git a/trunk/Examples/python/class/example.h b/trunk/Examples/python/class/example.h new file mode 100644 index 000000000..46d901361 --- /dev/null +++ b/trunk/Examples/python/class/example.h @@ -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(void) = 0; + virtual double perimeter(void) = 0; + static int nshapes; +}; + +class Circle : public Shape { +private: + double radius; +public: + Circle(double r) : radius(r) { }; + virtual double area(void); + virtual double perimeter(void); +}; + +class Square : public Shape { +private: + double width; +public: + Square(double w) : width(w) { }; + virtual double area(void); + virtual double perimeter(void); +}; + + + + + diff --git a/trunk/Examples/python/class/example.i b/trunk/Examples/python/class/example.i new file mode 100644 index 000000000..75700b305 --- /dev/null +++ b/trunk/Examples/python/class/example.i @@ -0,0 +1,10 @@ +/* File : example.i */ +%module example + +%{ +#include "example.h" +%} + +/* Let's just grab the original header file here */ +%include "example.h" + diff --git a/trunk/Examples/python/class/index.html b/trunk/Examples/python/class/index.html new file mode 100644 index 000000000..12c5eded1 --- /dev/null +++ b/trunk/Examples/python/class/index.html @@ -0,0 +1,216 @@ + + +SWIG:Examples:python:class + + + + + +SWIG/Examples/python/class/ +
+ +

Wrapping a simple C++ class

+ +

+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. + +

The C++ Code

+ +Suppose you have some C++ classes described by the following (and admittedly lame) +header file: + +
+
+/* 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();
+};
+
+
+ +

The SWIG interface

+ +A simple SWIG interface for this can be built by simply grabbing the header file +like this: + +
+
+/* File : example.i */
+%module example
+
+%{
+#include "example.h"
+%}
+
+/* Let's just grab the original header file here */
+%include "example.h"
+
+
+ +Note: when creating a C++ extension, you must run SWIG with the -c++ option like this: +
+
+% swig -c++ -python example.i
+
+
+ +

A sample Python script

+ +Click here to see a script that calls the C++ functions from Python. + +

Key points

+ + + +

General Comments

+ +