diff options
Diffstat (limited to 'Examples/python/template')
-rw-r--r-- | Examples/python/template/Makefile | 22 | ||||
-rw-r--r-- | Examples/python/template/example.h | 32 | ||||
-rw-r--r-- | Examples/python/template/example.i | 17 | ||||
-rw-r--r-- | Examples/python/template/runme.py | 34 |
4 files changed, 105 insertions, 0 deletions
diff --git a/Examples/python/template/Makefile b/Examples/python/template/Makefile new file mode 100644 index 0000000..ba5c798 --- /dev/null +++ b/Examples/python/template/Makefile @@ -0,0 +1,22 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-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 + $(MAKE) -f $(TOP)/Makefile python_run diff --git a/Examples/python/template/example.h b/Examples/python/template/example.h new file mode 100644 index 0000000..7401df6 --- /dev/null +++ b/Examples/python/template/example.h @@ -0,0 +1,32 @@ +/* File : example.h */ + +// Some template definitions + +template<class T> T max(T a, T b) { return a>b ? a : b; } + +template<class T> class vector { + T *v; + int sz; + public: + vector(int _sz) { + v = new T[_sz]; + sz = _sz; + } + T &get(int index) { + return v[index]; + } + void set(int index, T &val) { + v[index] = val; + } +#ifdef SWIG + %extend { + T getitem(int index) { + return $self->get(index); + } + void setitem(int index, T val) { + $self->set(index,val); + } + } +#endif +}; + diff --git a/Examples/python/template/example.i b/Examples/python/template/example.i new file mode 100644 index 0000000..8f94c4d --- /dev/null +++ b/Examples/python/template/example.i @@ -0,0 +1,17 @@ +/* File : example.i */ +%module example + +%{ +#include "example.h" +%} + +/* Let's just grab the original header file here */ +%include "example.h" + +/* Now instantiate some specific template declarations */ + +%template(maxint) max<int>; +%template(maxdouble) max<double>; +%template(vecint) vector<int>; +%template(vecdouble) vector<double>; + diff --git a/Examples/python/template/runme.py b/Examples/python/template/runme.py new file mode 100644 index 0000000..05940bc --- /dev/null +++ b/Examples/python/template/runme.py @@ -0,0 +1,34 @@ +# file: runme.py + +import example + +# Call some templated functions +print example.maxint(3,7) +print example.maxdouble(3.14,2.18) + +# Create some class + +iv = example.vecint(100) +dv = example.vecdouble(1000) + +for i in range(0,100): + iv.setitem(i,2*i) + +for i in range(0,1000): + dv.setitem(i, 1.0/(i+1)) + +sum = 0 +for i in range(0,100): + sum = sum + iv.getitem(i) + +print sum + +sum = 0.0 +for i in range(0,1000): + sum = sum + dv.getitem(i) +print sum + +del iv +del dv + + |