summaryrefslogtreecommitdiff
path: root/Examples/python/callback
diff options
context:
space:
mode:
authorLorry Tar Creator <lorry-tar-importer@baserock.org>2009-08-18 20:56:02 +0000
committerLorry <lorry@roadtrain.codethink.co.uk>2012-09-25 16:59:08 +0000
commit9f8a09ed743cedd9547bf0661d518647966ab114 (patch)
tree9c7803d3b27a8ec22e91792ac7f7932efa128b20 /Examples/python/callback
downloadswig-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/callback')
-rw-r--r--Examples/python/callback/Makefile22
-rw-r--r--Examples/python/callback/example.cxx4
-rw-r--r--Examples/python/callback/example.h22
-rw-r--r--Examples/python/callback/example.i13
-rw-r--r--Examples/python/callback/index.html19
-rw-r--r--Examples/python/callback/runme.py56
6 files changed, 136 insertions, 0 deletions
diff --git a/Examples/python/callback/Makefile b/Examples/python/callback/Makefile
new file mode 100644
index 0000000..a29276e
--- /dev/null
+++ b/Examples/python/callback/Makefile
@@ -0,0 +1,22 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+CXXSRCS = example.cxx
+TARGET = example
+INTERFACE = example.i
+LIBS = -lm
+SWIGOPT =
+
+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
+ $(MAKE) -f $(TOP)/Makefile python_run
diff --git a/Examples/python/callback/example.cxx b/Examples/python/callback/example.cxx
new file mode 100644
index 0000000..450d756
--- /dev/null
+++ b/Examples/python/callback/example.cxx
@@ -0,0 +1,4 @@
+/* File : example.cxx */
+
+#include "example.h"
+
diff --git a/Examples/python/callback/example.h b/Examples/python/callback/example.h
new file mode 100644
index 0000000..2a01949
--- /dev/null
+++ b/Examples/python/callback/example.h
@@ -0,0 +1,22 @@
+/* File : example.h */
+
+#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() { delete _callback; _callback = 0; }
+ void setCallback(Callback *cb) { delCallback(); _callback = cb; }
+ void call() { if (_callback) _callback->run(); }
+};
+
diff --git a/Examples/python/callback/example.i b/Examples/python/callback/example.i
new file mode 100644
index 0000000..90beda0
--- /dev/null
+++ b/Examples/python/callback/example.i
@@ -0,0 +1,13 @@
+/* File : example.i */
+%module(directors="1") example
+%{
+#include "example.h"
+%}
+
+%include "std_string.i"
+
+/* turn on director wrapping Callback */
+%feature("director") Callback;
+
+%include "example.h"
+
diff --git a/Examples/python/callback/index.html b/Examples/python/callback/index.html
new file mode 100644
index 0000000..e2017ec
--- /dev/null
+++ b/Examples/python/callback/index.html
@@ -0,0 +1,19 @@
+<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>
+
+<p>
+This example illustrates how to use directors to implement C++ callbacks in Python.
+
+<hr>
+</body>
+</html>
diff --git a/Examples/python/callback/runme.py b/Examples/python/callback/runme.py
new file mode 100644
index 0000000..026e952
--- /dev/null
+++ b/Examples/python/callback/runme.py
@@ -0,0 +1,56 @@
+# file: runme.py
+
+# This file illustrates the cross language polymorphism using directors.
+
+import example
+
+
+class PyCallback(example.Callback):
+ def __init__(self):
+ example.Callback.__init__(self)
+ def run(self):
+ print "PyCallback.run()"
+
+# 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"
+