From 2824b0cbb66e715490e1ef13250bd675d87b32d9 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Wed, 2 Jun 2010 20:53:17 +0000 Subject: rel-2.0.0 git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/tags/rel-2.0.0@12089 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- trunk/Examples/php/callback/Makefile | 22 +++++++++++++++ trunk/Examples/php/callback/example.cxx | 4 +++ trunk/Examples/php/callback/example.h | 22 +++++++++++++++ trunk/Examples/php/callback/example.i | 13 +++++++++ trunk/Examples/php/callback/index.html | 19 +++++++++++++ trunk/Examples/php/callback/runme.php | 47 +++++++++++++++++++++++++++++++++ 6 files changed, 127 insertions(+) create mode 100644 trunk/Examples/php/callback/Makefile create mode 100644 trunk/Examples/php/callback/example.cxx create mode 100644 trunk/Examples/php/callback/example.h create mode 100644 trunk/Examples/php/callback/example.i create mode 100644 trunk/Examples/php/callback/index.html create mode 100644 trunk/Examples/php/callback/runme.php (limited to 'trunk/Examples/php/callback') diff --git a/trunk/Examples/php/callback/Makefile b/trunk/Examples/php/callback/Makefile new file mode 100644 index 000000000..42597202b --- /dev/null +++ b/trunk/Examples/php/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)' php_cpp + +static:: + $(MAKE) -f $(TOP)/Makefile $(SWIGLIB) CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='myphp' INTERFACE='$(INTERFACE)' php_cpp_static + +clean:: + $(MAKE) -f $(TOP)/Makefile php_clean + rm -f $(TARGET).php + +check: all + $(MAKE) -f $(TOP)/Makefile php_run diff --git a/trunk/Examples/php/callback/example.cxx b/trunk/Examples/php/callback/example.cxx new file mode 100644 index 000000000..450d75608 --- /dev/null +++ b/trunk/Examples/php/callback/example.cxx @@ -0,0 +1,4 @@ +/* File : example.cxx */ + +#include "example.h" + diff --git a/trunk/Examples/php/callback/example.h b/trunk/Examples/php/callback/example.h new file mode 100644 index 000000000..2a0194999 --- /dev/null +++ b/trunk/Examples/php/callback/example.h @@ -0,0 +1,22 @@ +/* File : example.h */ + +#include + +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/trunk/Examples/php/callback/example.i b/trunk/Examples/php/callback/example.i new file mode 100644 index 000000000..90beda01a --- /dev/null +++ b/trunk/Examples/php/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/trunk/Examples/php/callback/index.html b/trunk/Examples/php/callback/index.html new file mode 100644 index 000000000..2aa720e24 --- /dev/null +++ b/trunk/Examples/php/callback/index.html @@ -0,0 +1,19 @@ + + +SWIG:Examples:php:callback + + + + + +SWIG/Examples/php/callback/ +
+ +

Implementing C++ callbacks in PHP

+ +

+This example illustrates how to use directors to implement C++ callbacks in PHP. + +


+ + diff --git a/trunk/Examples/php/callback/runme.php b/trunk/Examples/php/callback/runme.php new file mode 100644 index 000000000..2be71994f --- /dev/null +++ b/trunk/Examples/php/callback/runme.php @@ -0,0 +1,47 @@ +thisown = 0; +$caller->setCallback($callback); +$caller->call(); +$caller->delCallback(); + +print "\n"; +print "Adding and calling a PHP callback\n"; +print "------------------------------------\n"; + +# Add a PHP callback. + +$callback = new PhpCallback(); +$callback->thisown = 0; +$caller->setCallback($callback); +$caller->call(); +$caller->delCallback(); + +# All done. + +print "php exit\n"; + +?> -- cgit v1.2.1