example of how to include the swig runtime code into external user code
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@6753 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
0baa17ec05
commit
eacea2b6f8
5 changed files with 151 additions and 0 deletions
27
SWIG/Examples/python/swigrun/Makefile
Normal file
27
SWIG/Examples/python/swigrun/Makefile
Normal file
|
|
@ -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
|
||||
|
||||
23
SWIG/Examples/python/swigrun/example.cxx
Normal file
23
SWIG/Examples/python/swigrun/example.cxx
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
/* File : example.cxx */
|
||||
|
||||
#include <Python.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
58
SWIG/Examples/python/swigrun/example.h
Normal file
58
SWIG/Examples/python/swigrun/example.h
Normal file
|
|
@ -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);
|
||||
|
||||
15
SWIG/Examples/python/swigrun/example.i
Normal file
15
SWIG/Examples/python/swigrun/example.i
Normal file
|
|
@ -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"
|
||||
|
||||
28
SWIG/Examples/python/swigrun/runme.py
Normal file
28
SWIG/Examples/python/swigrun/runme.py
Normal file
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue