two director examples for python
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@4447 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
c8b971fd33
commit
8db3f9e8df
13 changed files with 360 additions and 0 deletions
22
Examples/python/callback/Makefile
Normal file
22
Examples/python/callback/Makefile
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
CXXSRCS = example.cxx
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS = -lm
|
||||
SWIGOPT =
|
||||
SWIGLIB = SWIG_LIB=/b/mrose/projects/swig/SWIG/Lib
|
||||
|
||||
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
|
||||
4
Examples/python/callback/example.cxx
Normal file
4
Examples/python/callback/example.cxx
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
/* File : example.cxx */
|
||||
|
||||
#include "example.h"
|
||||
|
||||
23
Examples/python/callback/example.h
Normal file
23
Examples/python/callback/example.h
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
/* File : example.h */
|
||||
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
|
||||
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() { if (_callback) delete _callback; _callback = 0; }
|
||||
void setCallback(Callback *cb) { delCallback(); _callback = cb; }
|
||||
void call() { if (_callback) _callback->run(); }
|
||||
};
|
||||
|
||||
15
Examples/python/callback/example.i
Normal file
15
Examples/python/callback/example.i
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
/* File : example.i */
|
||||
%module(directors="1") example
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
%include "typemaps.i"
|
||||
%include "std_vector.i"
|
||||
%include "std_string.i"
|
||||
|
||||
/* turn on director wrapping Callback */
|
||||
%feature("director") Callback;
|
||||
|
||||
%include "example.h"
|
||||
|
||||
21
Examples/python/callback/index.html
Normal file
21
Examples/python/callback/index.html
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>SWIG:Examples:python:callback</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff">
|
||||
|
||||
|
||||
<tt>SWIG/Examples/python/extend/</tt>
|
||||
<hr>
|
||||
|
||||
<H2>Implementing C++ callbacks in Python</H2>
|
||||
|
||||
<tt>$Header$</tt><br>
|
||||
|
||||
<p>
|
||||
This example illustrates how to use directors to implement C++ callbacks in Python.
|
||||
|
||||
<hr>
|
||||
</body>
|
||||
</html>
|
||||
66
Examples/python/callback/runme.py
Normal file
66
Examples/python/callback/runme.py
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
# file: runme.py
|
||||
|
||||
# This file illustrates the cross language polymorphism using directors.
|
||||
|
||||
import example
|
||||
|
||||
|
||||
# CEO class, which overrides Employee::getPosition().
|
||||
|
||||
class PyCallback(example.Callback):
|
||||
def __init__(self):
|
||||
example.Callback.__init__(self)
|
||||
def run(self):
|
||||
print "PyCallback.run()"
|
||||
def __del__(self):
|
||||
print "PyCallback.__del__()"
|
||||
# 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.Callback.__del__(self)
|
||||
|
||||
|
||||
# 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"
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue