diff --git a/SWIG/Examples/python/swigrun/Makefile b/SWIG/Examples/python/swigrun/Makefile new file mode 100644 index 000000000..3c0ff3026 --- /dev/null +++ b/SWIG/Examples/python/swigrun/Makefile @@ -0,0 +1,27 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = example.cxx +TARGET = example +INTERFACE = example.i +LIBS = -lm +SWIGOPT = + +all:: + $(SWIG) -co swigrun.swg + $(SWIG) -o pyrun.swg -co python/pyrun.swg + $(SWIG) -o pyrunalias.swg -co python/pyrunalias.swg + $(MAKE) -f $(TOP)/Makefile $(SWIGLIB) CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' python_cpp + rm -f swigrun.swg pyrun.swg pyrunalias.swg + +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 swigrun.swg pyrun.swg pyrunalias.swg + +check: all + diff --git a/SWIG/Examples/python/swigrun/example.cxx b/SWIG/Examples/python/swigrun/example.cxx new file mode 100644 index 000000000..780c53f58 --- /dev/null +++ b/SWIG/Examples/python/swigrun/example.cxx @@ -0,0 +1,23 @@ +/* File : example.cxx */ + +#include +#include "swigrun.swg" +#include "pyrun.swg" +#include "pyrunalias.swg" + +#include "example.h" + + +Manager* convert_to_Manager(PyObject *py_obj) +{ + Manager* c_ptr; + swig_type_info *ty = SWIG_TypeQuery("Manager *"); + printf("manager ty %x \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/SWIG/Examples/python/swigrun/example.h b/SWIG/Examples/python/swigrun/example.h new file mode 100644 index 000000000..69e6fe4de --- /dev/null +++ b/SWIG/Examples/python/swigrun/example.h @@ -0,0 +1,58 @@ +/* File : example.h */ + +#include +#include +#include +#include +#include + +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 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::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::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/SWIG/Examples/python/swigrun/example.i b/SWIG/Examples/python/swigrun/example.i new file mode 100644 index 000000000..c8ec32e09 --- /dev/null +++ b/SWIG/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/SWIG/Examples/python/swigrun/runme.py b/SWIG/Examples/python/swigrun/runme.py new file mode 100644 index 000000000..cfffe63f6 --- /dev/null +++ b/SWIG/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 shadow 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