git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@11434 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
8ec7035eee
commit
0249eea389
53 changed files with 2838 additions and 66 deletions
22
Examples/php/callback/Makefile
Normal file
22
Examples/php/callback/Makefile
Normal file
|
|
@ -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
|
||||
4
Examples/php/callback/example.cxx
Normal file
4
Examples/php/callback/example.cxx
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
/* File : example.cxx */
|
||||
|
||||
#include "example.h"
|
||||
|
||||
22
Examples/php/callback/example.h
Normal file
22
Examples/php/callback/example.h
Normal file
|
|
@ -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(); }
|
||||
};
|
||||
|
||||
13
Examples/php/callback/example.i
Normal file
13
Examples/php/callback/example.i
Normal file
|
|
@ -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"
|
||||
|
||||
19
Examples/php/callback/index.html
Normal file
19
Examples/php/callback/index.html
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>SWIG:Examples:php:callback</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff">
|
||||
|
||||
|
||||
<tt>SWIG/Examples/php/callback/</tt>
|
||||
<hr>
|
||||
|
||||
<H2>Implementing C++ callbacks in PHP</H2>
|
||||
|
||||
<p>
|
||||
This example illustrates how to use directors to implement C++ callbacks in PHP.
|
||||
|
||||
<hr>
|
||||
</body>
|
||||
</html>
|
||||
47
Examples/php/callback/runme.php
Normal file
47
Examples/php/callback/runme.php
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
# This file illustrates the cross language polymorphism using directors.
|
||||
|
||||
require("example.php");
|
||||
|
||||
# Class, which overwrites Callback::run().
|
||||
|
||||
class PhpCallback extends Callback {
|
||||
function run() {
|
||||
print "PhpCallback.run()\n";
|
||||
}
|
||||
};
|
||||
|
||||
# Create an Caller instance
|
||||
|
||||
$caller = new 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\n";
|
||||
print "----------------------------------------\n";
|
||||
|
||||
$callback = new Callback();
|
||||
$callback->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";
|
||||
|
||||
?>
|
||||
Loading…
Add table
Add a link
Reference in a new issue