diff options
| author | Lorry Tar Creator <lorry-tar-importer@baserock.org> | 2009-08-18 20:56:02 +0000 |
|---|---|---|
| committer | Lorry <lorry@roadtrain.codethink.co.uk> | 2012-09-25 16:59:08 +0000 |
| commit | 9f8a09ed743cedd9547bf0661d518647966ab114 (patch) | |
| tree | 9c7803d3b27a8ec22e91792ac7f7932efa128b20 /Examples/python/swigrun | |
| download | swig-tarball-master.tar.gz | |
Imported from /srv/lorry/lorry-area/swig-tarball/swig-1.3.40.tar.gz.HEADswig-1.3.40master
Diffstat (limited to 'Examples/python/swigrun')
| -rw-r--r-- | Examples/python/swigrun/Makefile | 25 | ||||
| -rw-r--r-- | Examples/python/swigrun/example.cxx | 20 | ||||
| -rw-r--r-- | Examples/python/swigrun/example.h | 58 | ||||
| -rw-r--r-- | Examples/python/swigrun/example.i | 15 | ||||
| -rw-r--r-- | Examples/python/swigrun/runme.py | 28 |
5 files changed, 146 insertions, 0 deletions
diff --git a/Examples/python/swigrun/Makefile b/Examples/python/swigrun/Makefile new file mode 100644 index 0000000..2142be5 --- /dev/null +++ b/Examples/python/swigrun/Makefile @@ -0,0 +1,25 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = example.cxx +TARGET = example +INTERFACE = example.i +LIBS = -lm +SWIGOPT = + +all:: + $(SWIG) -python -external-runtime + $(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 + rm -f swigpyrun.h + +check: all + + $(MAKE) -f $(TOP)/Makefile python_run diff --git a/Examples/python/swigrun/example.cxx b/Examples/python/swigrun/example.cxx new file mode 100644 index 0000000..25906a5 --- /dev/null +++ b/Examples/python/swigrun/example.cxx @@ -0,0 +1,20 @@ +/* File : example.cxx */ + +#include <Python.h> +#include "swigpyrun.h" +#include "example.h" + + +Manager* convert_to_Manager(PyObject *py_obj) +{ + Manager* c_ptr; + swig_type_info *ty = SWIG_TypeQuery("Manager *"); + printf("manager ty %p \n", ty); + if (SWIG_ConvertPtr(py_obj, (void **) &c_ptr, ty, 0) == -1) { + c_ptr = 0; + } else { + Py_XINCREF(py_obj); + } + return c_ptr; +} + diff --git a/Examples/python/swigrun/example.h b/Examples/python/swigrun/example.h new file mode 100644 index 0000000..69e6fe4 --- /dev/null +++ b/Examples/python/swigrun/example.h @@ -0,0 +1,58 @@ +/* File : example.h */ + +#include <cstdio> +#include <iostream> +#include <vector> +#include <string> +#include <cmath> + +class Employee { +private: + std::string name; +public: + Employee(const char* n): name(n) {} + virtual std::string getTitle() { return getPosition() + " " + getName(); } + virtual std::string getName() { return name; } + virtual std::string getPosition() const { return "Employee"; } + virtual ~Employee() { printf("~Employee() @ %p\n", this); } +}; + + +class Manager: public Employee { +public: + Manager(const char* n): Employee(n) {} + virtual std::string getPosition() const { return "Manager"; } +}; + + +class EmployeeList { + std::vector<Employee*> list; +public: + EmployeeList() { + list.push_back(new Employee("Bob")); + list.push_back(new Employee("Jane")); + list.push_back(new Manager("Ted")); + } + void addEmployee(Employee *p) { + list.push_back(p); + std::cout << "New employee added. Current employees are:" << std::endl; + std::vector<Employee*>::iterator i; + for (i=list.begin(); i!=list.end(); i++) { + std::cout << " " << (*i)->getTitle() << std::endl; + } + } + const Employee *get_item(int i) { + return list[i]; + } + ~EmployeeList() { + std::vector<Employee*>::iterator i; + std::cout << "~EmployeeList, deleting " << list.size() << " employees." << std::endl; + for (i=list.begin(); i!=list.end(); i++) { + delete *i; + } + std::cout << "~EmployeeList empty." << std::endl; + } +}; + +Manager* convert_to_Manager(PyObject *obj); + diff --git a/Examples/python/swigrun/example.i b/Examples/python/swigrun/example.i new file mode 100644 index 0000000..c8ec32e --- /dev/null +++ b/Examples/python/swigrun/example.i @@ -0,0 +1,15 @@ +/* File : example.i */ +%module(directors="1") example +%{ +#include "example.h" +%} + +%include "std_vector.i" +%include "std_string.i" + +/* turn on director wrapping for Manager */ +%feature("director") Employee; +%feature("director") Manager; + +%include "example.h" + diff --git a/Examples/python/swigrun/runme.py b/Examples/python/swigrun/runme.py new file mode 100644 index 0000000..abcd964 --- /dev/null +++ b/Examples/python/swigrun/runme.py @@ -0,0 +1,28 @@ +# file: runme.py + +# This file illustrates the cross language polymorphism using directors. + +import example + + +# CEO class, which overrides Employee::getPosition(). + +class CEO(example.Manager): + def __init__(self, name): + example.Manager.__init__(self, name) + def getPosition(self): + return "CEO" + def __del__(self): + print "CEO.__del__(),", self.getName() + # for proxy class extensions that are not "disowned" and + # define a __del__ method, it is very important to call the + # base class __del__. otherwise the c++ objects will never + # be deleted. + example.Manager.__del__(self) + + + + +e = CEO("Alice") +m = example.convert_to_Manager(e) +print m |
