Updated examples to function correctly with new php4 module. Added
some supplemental examples for cpointer, overloading and references using proxies. git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7391 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
e2379c2b28
commit
522ab32693
44 changed files with 506 additions and 131 deletions
|
|
@ -1,30 +1,12 @@
|
|||
CRUD=acinclude.m4 aclocal.m4 config.cache config.h config.h.in config.log \
|
||||
config.m4 config.nice config.status config.sub configure configure.in \
|
||||
config_vars.mk config.guess CREDITS dynlib.m4 *.so *.lo *.o *.slo \
|
||||
install-sh libs.mk libtool ltmain.sh Makefile Makefile.in missing \
|
||||
mkinstalldirs php_example.h php_example.la *_wrap.c* example.php \
|
||||
build modules .deps .libs conftest conftest.c
|
||||
CRUD=*.so *.o php_example.h *_wrap.c* example.php
|
||||
|
||||
all: check
|
||||
|
||||
check: BUILD.sh
|
||||
check:
|
||||
./BUILD.sh
|
||||
# SWIG=$(SWIG) ./BUILD.sh
|
||||
|
||||
# This one is fun! How do we know what shouldn't be there?
|
||||
clean:
|
||||
rm -fr $(CRUD)
|
||||
rm -f $(CRUD)
|
||||
|
||||
BUILD.sh:
|
||||
echo "-------------------------------------------------"
|
||||
echo `pwd`"/BUILD.sh missing!"
|
||||
echo "I need BUILD.sh file to show me how to invoke swig"
|
||||
echo "It usually looks like this:"
|
||||
echo
|
||||
echo "swig -php -phpfull -c++ <extra options> example.i"
|
||||
echo "# and then the rest is always the same..."
|
||||
echo "phpize && ./configure && make && make install"
|
||||
echo
|
||||
echo
|
||||
exit 1
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
#! /bin/sh -e
|
||||
|
||||
${SWIG:=swig} -php4 -phpfull -c++ -noproxy -withcxx example.cxx example.i
|
||||
phpize && ./configure && make clean && make
|
||||
${SWIG:=swig} -php4 -make -c++ -noproxy -withcxx example.cxx example.i
|
||||
make
|
||||
php -d extension_dir=. runme.php4
|
||||
|
|
|
|||
|
|
@ -1,7 +1,14 @@
|
|||
/* File : example.c */
|
||||
|
||||
#include "example.h"
|
||||
#define M_PI 3.14159265358979323846
|
||||
#include <math.h>
|
||||
#ifndef M_PI
|
||||
# define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
int Shape::get_nshapes() {
|
||||
return nshapes;
|
||||
}
|
||||
|
||||
/* Move the shape to a new location */
|
||||
void Shape::move(double dx, double dy) {
|
||||
|
|
@ -11,6 +18,10 @@ void Shape::move(double dx, double dy) {
|
|||
|
||||
int Shape::nshapes = 0;
|
||||
|
||||
void Circle::set_radius( double r ) {
|
||||
radius = r;
|
||||
}
|
||||
|
||||
double Circle::area(void) {
|
||||
return M_PI*radius*radius;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ public:
|
|||
virtual double area(void) = 0;
|
||||
virtual double perimeter(void) = 0;
|
||||
static int nshapes;
|
||||
static int get_nshapes();
|
||||
};
|
||||
|
||||
class Circle : public Shape {
|
||||
|
|
@ -20,6 +21,8 @@ private:
|
|||
double radius;
|
||||
public:
|
||||
Circle(double r) : radius(r) { };
|
||||
~Circle() { };
|
||||
void set_radius( double r );
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
|
|
@ -29,6 +32,7 @@ private:
|
|||
double width;
|
||||
public:
|
||||
Square(double w) : width(w) { };
|
||||
~Square() { }
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -35,22 +35,28 @@ print " Square = (" . Shape_x_get($s) . "," . Shape_y_get($s) . ")\n";
|
|||
|
||||
# ----- Call some methods -----
|
||||
|
||||
# Notice how the Shape_area() and Shape_perimeter() functions really
|
||||
# invoke the appropriate virtual method on each object.
|
||||
print "\nHere are some properties of the shapes:\n";
|
||||
foreach (array($c,$s) as $o) {
|
||||
print " $o\n";
|
||||
print " area = " . Shape_area($o) . "\n";
|
||||
print " perimeter = " . Shape_perimeter($o) . "\n";
|
||||
}
|
||||
# Notice how the Shape_area() and Shape_perimeter() functions really
|
||||
# invoke the appropriate virtual method on each object.
|
||||
|
||||
# ----- Delete everything -----
|
||||
|
||||
print "\nGuess I'll clean up now\n";
|
||||
|
||||
# Note: this invokes the virtual destructor
|
||||
delete_Shape($c);
|
||||
delete_Shape($s);
|
||||
#delete_Shape($c);
|
||||
#delete_Shape($s);
|
||||
$c = NULL;
|
||||
$s = NULL;
|
||||
|
||||
# and don't forget the $o from the for loop above. It still refers to
|
||||
# the square.
|
||||
$o = NULL;
|
||||
|
||||
print nshapes() . " shapes remain\n";
|
||||
print "Goodbye\n";
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
#! /bin/sh -e
|
||||
|
||||
${SWIG:=swig} -php4 -phpfull -c++ -noproxy example.i
|
||||
phpize && ./configure && make clean && make
|
||||
${SWIG:=swig} -php4 -make -noproxy example.i
|
||||
make
|
||||
php -d extension_dir=. runme.php4
|
||||
|
|
|
|||
5
Examples/php4/cpointer/BUILD.sh
Executable file
5
Examples/php4/cpointer/BUILD.sh
Executable file
|
|
@ -0,0 +1,5 @@
|
|||
#! /bin/sh -e
|
||||
|
||||
${SWIG:=swig} -php4 -make -noproxy -withc example.c example.i
|
||||
make
|
||||
php -d extension_dir=. runme.php4
|
||||
16
Examples/php4/cpointer/example.c
Normal file
16
Examples/php4/cpointer/example.c
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
/* File : example.c */
|
||||
|
||||
void add(double *x, double *y, double *result) {
|
||||
*result = *x + *y;
|
||||
}
|
||||
|
||||
void sub(int *x, int *y, int *result) {
|
||||
*result = *x - *y;
|
||||
}
|
||||
|
||||
int divide(int n, int d, int *r) {
|
||||
int q;
|
||||
q = n/d;
|
||||
*r = n - q*d;
|
||||
return q;
|
||||
}
|
||||
26
Examples/php4/cpointer/example.i
Normal file
26
Examples/php4/cpointer/example.i
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
/* This example illustrates a couple of different techniques
|
||||
for manipulating C pointers */
|
||||
|
||||
/* First we'll use the pointer library */
|
||||
extern void add(int *x, int *y, int *result);
|
||||
|
||||
%include cpointer.i
|
||||
%pointer_functions(int, intp);
|
||||
|
||||
/* Next we'll use some typemaps */
|
||||
|
||||
%include typemaps.i
|
||||
extern void sub(int *INPUT, int *INPUT, int *OUTPUT);
|
||||
|
||||
/* Next we'll use typemaps and the %apply directive */
|
||||
|
||||
//%apply int *OUTPUT { int *r };
|
||||
//extern int divide(int n, int d, int *r);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
45
Examples/php4/cpointer/runme.php4
Normal file
45
Examples/php4/cpointer/runme.php4
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
require "example.php";
|
||||
|
||||
# First create some objects using the pointer library.
|
||||
|
||||
print "Testing the pointer library\n";
|
||||
|
||||
$a = new_intp();
|
||||
$b = new_intp();
|
||||
$c = new_intp();
|
||||
intp_assign($a,37);
|
||||
intp_assign($b,42);
|
||||
|
||||
print " a = $a\n";
|
||||
print " b = $b\n";
|
||||
print " c = $c\n";
|
||||
|
||||
# Call the add() function wuth some pointers
|
||||
add($a,$b,$c);
|
||||
|
||||
# Now get the result
|
||||
$r = intp_value($c);
|
||||
|
||||
print " 38 + 42 = $r\n";
|
||||
|
||||
# Clean up the pointers
|
||||
delete_intp($a);
|
||||
delete_intp($b);
|
||||
delete_intp($c);
|
||||
|
||||
# Now try the typemap library
|
||||
# This should be much easier. Now how it is no longer
|
||||
# necessary to manufacture pointers.
|
||||
|
||||
print "Trying the typemap library\n";
|
||||
$r = sub(37,42);
|
||||
print " 37 - 42 = $r\n";
|
||||
|
||||
# Now try the version with multiple return values
|
||||
# print "Testing multiple return values\n";
|
||||
# ($q,$r) = divide(42,37);
|
||||
# print " 42/37 = $q remainder $r\n";
|
||||
|
||||
?>
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
#! /bin/sh -e
|
||||
|
||||
${SWIG:=swig} -php4 -phpfull -c++ -noproxy -withcxx example.cxx example.i
|
||||
phpize && ./configure && make clean && make
|
||||
${SWIG:=swig} -php4 -make -c++ -noproxy -withcxx example.cxx example.i
|
||||
make
|
||||
php -d extension_dir=. runme.php4
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
#! /bin/sh -e
|
||||
|
||||
${SWIG:=swig} -php4 -phpfull -noproxy -withc example.c example.i
|
||||
phpize && ./configure && make clean && make
|
||||
${SWIG:=swig} -php4 -make -c -noproxy -withc example.c example.i
|
||||
make
|
||||
php -d extension_dir=. runme.php4
|
||||
|
|
|
|||
|
|
@ -5,5 +5,3 @@ extern int add(int,int);
|
|||
extern int sub(int,int);
|
||||
extern int mul(int,int);
|
||||
|
||||
extern int (*funcvar)(int,int);
|
||||
|
||||
|
|
|
|||
5
Examples/php4/overloading/BUILD.sh
Executable file
5
Examples/php4/overloading/BUILD.sh
Executable file
|
|
@ -0,0 +1,5 @@
|
|||
#! /bin/sh -e
|
||||
|
||||
${SWIG:=swig} -php4 -make -c++ -withcxx example.cxx example.i
|
||||
make
|
||||
php -d extension_dir=. runme.php4
|
||||
51
Examples/php4/overloading/example.cxx
Normal file
51
Examples/php4/overloading/example.cxx
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
/* File : example.c */
|
||||
|
||||
#include "example.h"
|
||||
#include <math.h>
|
||||
#ifndef M_PI
|
||||
# define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
int Shape::get_nshapes() {
|
||||
return nshapes;
|
||||
}
|
||||
|
||||
/* Move the shape to a new location */
|
||||
void Shape::move(double dx, double dy) {
|
||||
x += dx;
|
||||
y += dy;
|
||||
}
|
||||
|
||||
int Shape::nshapes = 0;
|
||||
|
||||
double Circle::area(void) {
|
||||
return M_PI*radius*radius;
|
||||
}
|
||||
|
||||
double Circle::perimeter(void) {
|
||||
return 2*M_PI*radius;
|
||||
}
|
||||
|
||||
double Square::area(void) {
|
||||
return width*width;
|
||||
}
|
||||
|
||||
double Square::perimeter(void) {
|
||||
return 4*width;
|
||||
}
|
||||
|
||||
char *overloaded(int i) {
|
||||
return "Overloaded with int";
|
||||
}
|
||||
|
||||
char *overloaded(double d) {
|
||||
return "Overloaded with double";
|
||||
}
|
||||
|
||||
char *overloaded( const Circle& ) {
|
||||
return "Overloaded with Circle";
|
||||
}
|
||||
|
||||
char *overloaded( const Shape& ) {
|
||||
return "Overloaded with Shape";
|
||||
}
|
||||
45
Examples/php4/overloading/example.h
Normal file
45
Examples/php4/overloading/example.h
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
/* File : example.h */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
class Shape {
|
||||
public:
|
||||
Shape() {
|
||||
nshapes++;
|
||||
}
|
||||
virtual ~Shape() {
|
||||
nshapes--;
|
||||
};
|
||||
double x, y;
|
||||
void move(double dx, double dy);
|
||||
virtual double area(void) = 0;
|
||||
virtual double perimeter(void) = 0;
|
||||
static int nshapes;
|
||||
static int get_nshapes();
|
||||
};
|
||||
|
||||
class Circle : public Shape {
|
||||
private:
|
||||
double radius;
|
||||
public:
|
||||
Circle(double r) : radius(r) { };
|
||||
~Circle() { };
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
|
||||
class Square : public Shape {
|
||||
private:
|
||||
double width;
|
||||
public:
|
||||
Square(double w) : width(w) { };
|
||||
~Square() { }
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
|
||||
char *overloaded( int i );
|
||||
char *overloaded( double d );
|
||||
char *overloaded( const Circle& );
|
||||
char *overloaded( const Shape& );
|
||||
|
||||
8
Examples/php4/overloading/example.i
Normal file
8
Examples/php4/overloading/example.i
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
%include "example.h"
|
||||
59
Examples/php4/overloading/runme.php4
Normal file
59
Examples/php4/overloading/runme.php4
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
# This file illustrates the low-level C++ interface
|
||||
# created by SWIG. In this case, all of our C++ classes
|
||||
# get converted into function calls.
|
||||
|
||||
include("example.php");
|
||||
|
||||
# ----- Object creation -----
|
||||
|
||||
print "Creating some objects:\n";
|
||||
$c = new Circle(10);
|
||||
print " Created circle $c\n";
|
||||
$s = new Square(10);
|
||||
print " Created square $s\n";
|
||||
|
||||
# ----- Access a static member -----
|
||||
|
||||
print "\nA total of " . Shape::nshapes() . " shapes were created\n";
|
||||
|
||||
# ----- Member data access -----
|
||||
|
||||
# Set the location of the object.
|
||||
# Note: methods in the base class Shape are used since
|
||||
# x and y are defined there.
|
||||
|
||||
$c->x = 20;
|
||||
$c->y = 30;
|
||||
$s->x = -10;
|
||||
$s->y = 5;
|
||||
|
||||
print "\nHere is their current position:\n";
|
||||
print " Circle = (" . $c->x . "," . $c->y . ")\n";
|
||||
print " Square = (" . $s->x . "," . $s->y . ")\n";
|
||||
|
||||
# ----- Call some methods -----
|
||||
|
||||
print "\nHere are some properties of the shapes:\n";
|
||||
foreach (array(1,2.1,$c,$s) as $o) {
|
||||
print " ".get_class($o)." $o\n";
|
||||
print " overloaded= " . overloaded($o) . "\n";
|
||||
}
|
||||
|
||||
# Need to unset($o) or else we hang on to a reference to the Square object.
|
||||
unset($o);
|
||||
|
||||
# ----- Delete everything -----
|
||||
|
||||
print "\nGuess I'll clean up now\n";
|
||||
|
||||
# Note: this invokes the virtual destructor
|
||||
unset($c);
|
||||
$s = 42;
|
||||
|
||||
print Shape::nshapes() . " shapes remain\n";
|
||||
|
||||
print "Goodbye\n";
|
||||
|
||||
?>
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
#! /bin/sh -e
|
||||
|
||||
${SWIG:=swig} -php4 -phpfull -noproxy -withc example.c example.i
|
||||
phpize && ./configure && make clean && make
|
||||
${SWIG:=swig} -php4 -make -noproxy -withc example.c example.i
|
||||
make
|
||||
php -d extension_dir=. runme.php4
|
||||
|
|
|
|||
|
|
@ -1,20 +1,12 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
extern void add(int *, int *, int *);
|
||||
extern void sub(int *, int *, int *);
|
||||
extern int divide(int, int, int *);
|
||||
%}
|
||||
|
||||
/* This example illustrates a couple of different techniques
|
||||
for manipulating C pointers */
|
||||
|
||||
%include phppointers.i
|
||||
/* First we'll use the pointer library */
|
||||
extern void add(int *x, int *y, int *result);
|
||||
|
||||
%include cpointer.i
|
||||
%pointer_functions(int, intp);
|
||||
extern void add(double *x, double *y, double *result);
|
||||
|
||||
/* Next we'll use some typemaps */
|
||||
|
||||
|
|
|
|||
|
|
@ -6,28 +6,18 @@
|
|||
|
||||
print "Testing the pointer library\n";
|
||||
|
||||
$a = new_intp();
|
||||
$b = new_intp();
|
||||
$c = new_intp();
|
||||
intp_assign($a,37);
|
||||
intp_assign($b,42);
|
||||
$a = 37.145;
|
||||
$b = 42.555;
|
||||
$c = ""; // $c must be defined and not null.
|
||||
|
||||
print " a = $a\n";
|
||||
print " b = $b\n";
|
||||
print " c = $c\n";
|
||||
|
||||
# Call the add() function wuth some pointers
|
||||
add($a,$b,$c);
|
||||
add(&$a,&$b,&$c);
|
||||
|
||||
# Now get the result
|
||||
$r = intp_value($c);
|
||||
|
||||
print " 37 + 42 = $r\n";
|
||||
|
||||
# Clean up the pointers
|
||||
delete_intp($a);
|
||||
delete_intp($b);
|
||||
delete_intp($c);
|
||||
print " $a + $b = $c\n";
|
||||
|
||||
# Now try the typemap library
|
||||
# This should be much easier. Now how it is no longer
|
||||
|
|
|
|||
5
Examples/php4/reference/BUILD-proxy.sh
Executable file
5
Examples/php4/reference/BUILD-proxy.sh
Executable file
|
|
@ -0,0 +1,5 @@
|
|||
#! /bin/sh -e
|
||||
|
||||
${SWIG:=swig} -php4 -make -c++ -withcxx example.cxx example.i
|
||||
make
|
||||
php -d extension_dir=. runme-proxy.php4
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
#! /bin/sh -e
|
||||
|
||||
${SWIG:=swig} -php4 -phpfull -c++ -noproxy -withcxx example.cxx example.i
|
||||
phpize && ./configure && make clean && make
|
||||
${SWIG:=swig} -php4 -make -c++ -noproxy -withcxx example.cxx example.i
|
||||
make
|
||||
php -d extension_dir=. runme.php4
|
||||
|
|
|
|||
|
|
@ -21,15 +21,16 @@ char *Vector::print() {
|
|||
VectorArray::VectorArray(int size) {
|
||||
items = new Vector[size];
|
||||
maxsize = size;
|
||||
printf("Vectorarray new: self=%p\n",this);
|
||||
printf("VectorArray new: self=%p\n",this);
|
||||
}
|
||||
|
||||
VectorArray::~VectorArray() {
|
||||
printf("VectorArray delete: self=%p\n",this);
|
||||
delete [] items;
|
||||
}
|
||||
|
||||
Vector &VectorArray::operator[](int index) {
|
||||
printf("Vectorarray: read[%d] self=%p\n",index,this);
|
||||
printf("VectorArray: read[%d] self=%p\n",index,this);
|
||||
if ((index < 0) || (index >= maxsize)) {
|
||||
printf("Panic! Array index out of bounds.\n");
|
||||
exit(1);
|
||||
|
|
@ -38,7 +39,7 @@ printf("Vectorarray: read[%d] self=%p\n",index,this);
|
|||
}
|
||||
|
||||
int VectorArray::size() {
|
||||
printf("Vectorarray: size %d self=%p\n",maxsize,this);
|
||||
printf("VectorArray: size %d self=%p\n",maxsize,this);
|
||||
return maxsize;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ public:
|
|||
/* This wrapper provides an alternative to the [] operator */
|
||||
%extend {
|
||||
Vector &get(int index) {
|
||||
printf("%p %d\n",self,index);
|
||||
printf("VectorArray extended get: %p %d\n",self,index);
|
||||
return (*self)[index];
|
||||
}
|
||||
void set(int index, Vector &a) {
|
||||
|
|
|
|||
79
Examples/php4/reference/runme-proxy.php4
Normal file
79
Examples/php4/reference/runme-proxy.php4
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
<?php
|
||||
|
||||
# This file illustrates the manipulation of C++ references in Php.
|
||||
# This uses the low-level interface. Shadow classes work differently.
|
||||
|
||||
require "example.php";
|
||||
|
||||
# ----- Object creation -----
|
||||
|
||||
print "Creating some objects:\n";
|
||||
$a = new Vector(3,4,5);
|
||||
$b = new Vector(10,11,12);
|
||||
|
||||
print " Created a: $a " . $a->print() . "\n";
|
||||
print " Created b: $b " . $b->print() . "\n";
|
||||
|
||||
# ----- Call an overloaded operator -----
|
||||
|
||||
# This calls the wrapper we placed around
|
||||
#
|
||||
# operator+(const Vector &a, const Vector &)
|
||||
#
|
||||
# It returns a new allocated object.
|
||||
|
||||
print "Adding a+b\n";
|
||||
$c = addv($a,$b);
|
||||
print " a+b =". $c->print()."\n";
|
||||
|
||||
# Note: Unless we free the result, a memory leak will occur
|
||||
$c = 0;
|
||||
|
||||
# ----- Create a vector array -----
|
||||
|
||||
# Note: Using the high-level interface here
|
||||
print "Creating an array of vectors\n";
|
||||
$va = new VectorArray(10);
|
||||
|
||||
print " va: $va size=".$va->size()."\n";
|
||||
|
||||
# ----- Set some values in the array -----
|
||||
|
||||
# These operators copy the value of $a and $b to the vector array
|
||||
$va->set(0,$a);
|
||||
$va->set(1,$b);
|
||||
|
||||
$va->get(0);
|
||||
# This will work, but it will cause a memory leak!
|
||||
|
||||
$va->set(2,addv($a,$b));
|
||||
|
||||
# The non-leaky way to do it
|
||||
|
||||
$c = addv($a,$b);
|
||||
$va->set(3,$c);
|
||||
$c = NULL;
|
||||
|
||||
# Get some values from the array
|
||||
|
||||
print "Getting some array values\n";
|
||||
for ($i = 0; $i < 5; $i++) {
|
||||
print "do $i\n";
|
||||
$v = $va->get($i);
|
||||
print " va($i) = ". $v->print(). "\n";
|
||||
}
|
||||
|
||||
# Watch under resource meter to check on this
|
||||
#print "Making sure we don't leak memory.\n";
|
||||
#for ($i = 0; $i < 1000000; $i++) {
|
||||
# $c = VectorArray_get($va,$i % 10);
|
||||
#}
|
||||
|
||||
# ----- Clean up -----
|
||||
print "Cleaning up\n";
|
||||
# wants fixing FIXME
|
||||
#delete_VectorArray($va);
|
||||
#delete_Vector($a);
|
||||
#delete_Vector($b);
|
||||
|
||||
?>
|
||||
|
|
@ -27,7 +27,7 @@ $c = addv($a,$b);
|
|||
print " a+b =". Vector_print($c)."\n";
|
||||
|
||||
# Note: Unless we free the result, a memory leak will occur
|
||||
delete_Vector($c);
|
||||
$c = None;
|
||||
|
||||
# ----- Create a vector array -----
|
||||
|
||||
|
|
@ -52,7 +52,7 @@ VectorArray_set($va,2,addv($a,$b));
|
|||
|
||||
$c = addv($a,$b);
|
||||
VectorArray_set($va,3,$c);
|
||||
delete_Vector($c);
|
||||
$c = None;
|
||||
|
||||
# Get some values from the array
|
||||
|
||||
|
|
@ -71,8 +71,8 @@ print "do $i\n";
|
|||
# ----- Clean up -----
|
||||
print "Cleaning up\n";
|
||||
# wants fixing FIXME
|
||||
#delete_VectorArray($va);
|
||||
delete_Vector($a);
|
||||
delete_Vector($b);
|
||||
$va = None;
|
||||
$a = None;
|
||||
$b = None;
|
||||
|
||||
?>
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
#! /bin/sh -e
|
||||
|
||||
${SWIG:=swig} -php4 -phpfull -c++ -withcxx example.cxx example.i
|
||||
phpize && ./configure && make clean && make
|
||||
${SWIG:=swig} -xmlout e.xml -php4 -make -c++ -withcxx example.cxx example.i
|
||||
make
|
||||
php -d extension_dir=. runme.php4
|
||||
|
|
|
|||
|
|
@ -6,6 +6,10 @@
|
|||
# define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
int Shape::get_nshapes() {
|
||||
return nshapes;
|
||||
}
|
||||
|
||||
/* Move the shape to a new location */
|
||||
void Shape::move(double dx, double dy) {
|
||||
x += dx;
|
||||
|
|
@ -14,6 +18,10 @@ void Shape::move(double dx, double dy) {
|
|||
|
||||
int Shape::nshapes = 0;
|
||||
|
||||
void Circle::set_radius( double r ) {
|
||||
radius = r;
|
||||
}
|
||||
|
||||
double Circle::area(void) {
|
||||
return M_PI*radius*radius;
|
||||
}
|
||||
|
|
@ -29,3 +37,7 @@ double Square::area(void) {
|
|||
double Square::perimeter(void) {
|
||||
return 4*width;
|
||||
}
|
||||
|
||||
Circle *CircleFactory( double r ) {
|
||||
return new Circle(r);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ public:
|
|||
virtual double area(void) = 0;
|
||||
virtual double perimeter(void) = 0;
|
||||
static int nshapes;
|
||||
static int get_nshapes();
|
||||
};
|
||||
|
||||
class Circle : public Shape {
|
||||
|
|
@ -23,6 +24,7 @@ private:
|
|||
public:
|
||||
Circle(double r) : radius(r) { };
|
||||
~Circle() { };
|
||||
void set_radius( double r );
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
|
|
@ -37,7 +39,5 @@ public:
|
|||
virtual double perimeter(void);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
Circle *CircleFactory( double r );
|
||||
|
||||
|
|
|
|||
|
|
@ -7,5 +7,6 @@
|
|||
|
||||
/* Let's just grab the original header file here */
|
||||
|
||||
%newobject CircleFactory;
|
||||
%include "example.h"
|
||||
|
||||
|
|
|
|||
|
|
@ -9,8 +9,8 @@ include("example.php");
|
|||
# ----- Object creation -----
|
||||
|
||||
print "Creating some objects:\n";
|
||||
$c = new Circle(10);
|
||||
print " Created circle $c\n";
|
||||
$c = CircleFactory(10);
|
||||
print " Created circle $c with area ". $c->area() ."\n";
|
||||
$s = new Square(10);
|
||||
print " Created square $s\n";
|
||||
|
||||
|
|
@ -38,21 +38,31 @@ print " Square = (" . $s->x . "," . $s->y . ")\n";
|
|||
print "\nHere are some properties of the shapes:\n";
|
||||
foreach (array($c,$s) as $o) {
|
||||
print " ".get_class($o)." $o\n";
|
||||
print " x = " . $o->x . "\n";
|
||||
print " y = " . $o->y . "\n";
|
||||
print " area = " . $o->area() . "\n";
|
||||
print " perimeter = " . $o->perimeter() . "\n";
|
||||
}
|
||||
|
||||
# Need to unset($o) or else we hang on to a reference to the Square object.
|
||||
unset($o);
|
||||
|
||||
# ----- Delete everything -----
|
||||
|
||||
print "\nGuess I'll clean up now\n";
|
||||
|
||||
# Note: this invokes the virtual destructor
|
||||
|
||||
# This causes a seq fault, possibly php trying to call destructor twice ?
|
||||
#$c->_destroy();
|
||||
#$s->_destroy();
|
||||
unset($c);
|
||||
$s = 42;
|
||||
|
||||
print Shape::nshapes() . " shapes remain\n";
|
||||
|
||||
print "Manually setting nshapes\n";
|
||||
|
||||
Shape::nshapes(42);
|
||||
|
||||
print Shape::get_nshapes() ." == 42\n";
|
||||
|
||||
print "Goodbye\n";
|
||||
|
||||
?>
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
#! /bin/sh -e
|
||||
|
||||
${SWIG:=swig} -php4 -phpfull -noproxy -withc example.c example.i
|
||||
phpize && ./configure && make clean && make
|
||||
${SWIG:=swig} -php4 -make -noproxy -withc example.c example.i
|
||||
make
|
||||
php -d extension_dir=. runme.php4
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%inline %{
|
||||
extern int gcd(int x, int y);
|
||||
%{
|
||||
extern double Foo;
|
||||
%}
|
||||
|
||||
int gcd(int x, int y);
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
#! /bin/sh -e
|
||||
|
||||
${SWIG:=swig} -php4 -phpfull -c++ -withcxx example.cxx example.i
|
||||
phpize && ./configure && make clean && make
|
||||
${SWIG:=swig} -php4 -make -c++ -withcxx example.cxx example.i
|
||||
make
|
||||
php -d extension_dir=. runme.php4
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
#! /bin/sh -e
|
||||
|
||||
${SWIG:=swig} -php4 -phpfull -noproxy -withc example.c example.i
|
||||
phpize && ./configure && make clean && make
|
||||
${SWIG:=swig} -php4 -make -noproxy -withc example.c example.i
|
||||
make
|
||||
php -d extension_dir=. runme.php4
|
||||
|
|
|
|||
|
|
@ -6,10 +6,8 @@ double dot_product(Vector a, Vector b) {
|
|||
return (a.x*b.x + a.y*b.y + a.z*b.z);
|
||||
}
|
||||
|
||||
Vector vector_add(Vector a, Vector b) {
|
||||
Vector r;
|
||||
r.x = a.x + b.x;
|
||||
r.y = a.y + b.y;
|
||||
r.z = a.z + b.z;
|
||||
return r;
|
||||
void vector_add(Vector a, Vector b, Vector* result) {
|
||||
result->x = a.x + b.x;
|
||||
result->y = a.y + b.y;
|
||||
result->z = a.z + b.z;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,3 +3,6 @@
|
|||
typedef struct {
|
||||
double x, y, z;
|
||||
} Vector;
|
||||
|
||||
double dot_product(Vector a, Vector b);
|
||||
void vector_add(Vector a, Vector b, Vector* result);
|
||||
|
|
|
|||
|
|
@ -5,27 +5,11 @@
|
|||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* Some functions that manipulate Vectors by value */
|
||||
%inline %{
|
||||
extern double dot_product(Vector a, Vector b);
|
||||
extern Vector vector_add(Vector a, Vector b);
|
||||
%}
|
||||
|
||||
/* Include this because the vector_add() function will leak memory */
|
||||
void free(void *);
|
||||
%include "example.h"
|
||||
|
||||
/* Some helper functions for our interface */
|
||||
%inline %{
|
||||
|
||||
Vector *new_Vector(double x, double y, double z) {
|
||||
/* We use the Zend memory manager */
|
||||
Vector *v = (Vector *) emalloc(sizeof(Vector));
|
||||
v->x = x;
|
||||
v->y = y;
|
||||
v->z = z;
|
||||
return v;
|
||||
}
|
||||
|
||||
void vector_print(Vector *v) {
|
||||
printf("Vector %x = (%g, %g, %g)\n", v, v->x, v->y, v->z);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,8 +3,15 @@
|
|||
require "example.php";
|
||||
|
||||
|
||||
$v = new_vector(1.0,2.0,3.0);
|
||||
$w = new_vector(10.0,11.0,12.0);
|
||||
$v = new_vector();
|
||||
vector_x_set($v,1.0);
|
||||
vector_y_set($v,2.0);
|
||||
vector_z_set($v,3.0);
|
||||
|
||||
$w = new_vector();
|
||||
vector_x_set($w,10.0);
|
||||
vector_y_set($w,11.0);
|
||||
vector_z_set($w,12.0);
|
||||
|
||||
echo "I just created the following vector\n";
|
||||
vector_print($v);
|
||||
|
|
@ -18,7 +25,8 @@
|
|||
|
||||
echo "\nNow I'm going to add the vectors together\n";
|
||||
|
||||
$r = vector_add($v, $w);
|
||||
$r = new_vector();
|
||||
vector_add($v, $w, $r);
|
||||
|
||||
vector_print($r);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
#! /bin/sh -e
|
||||
|
||||
${SWIG:=swig} -php4 -phpfull -noproxy -withc example.c example.i
|
||||
phpize && ./configure && make clean && make
|
||||
${SWIG:=swig} -php4 -make -noproxy -withc example.c example.i
|
||||
make
|
||||
php -d extension_dir=. runme.php4
|
||||
|
|
|
|||
|
|
@ -4,3 +4,26 @@ typedef struct {
|
|||
int x,y;
|
||||
} Point;
|
||||
|
||||
/* Some global variable declarations */
|
||||
extern int ivar;
|
||||
extern short svar;
|
||||
extern long lvar;
|
||||
extern unsigned int uivar;
|
||||
extern unsigned short usvar;
|
||||
extern unsigned long ulvar;
|
||||
extern signed char scvar;
|
||||
extern unsigned char ucvar;
|
||||
extern char cvar;
|
||||
extern float fvar;
|
||||
extern double dvar;
|
||||
extern char *strvar;
|
||||
extern const char cstrvar[];
|
||||
extern int *iptrvar;
|
||||
extern char name[256];
|
||||
|
||||
extern Point *ptptr;
|
||||
extern Point pt;
|
||||
|
||||
extern int status;
|
||||
extern char path[256];
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@
|
|||
%}
|
||||
|
||||
/* Some global variable declarations */
|
||||
%inline %{
|
||||
extern int ivar;
|
||||
extern short svar;
|
||||
extern long lvar;
|
||||
|
|
@ -24,27 +23,22 @@ extern char name[256];
|
|||
|
||||
extern Point *ptptr;
|
||||
extern Point pt;
|
||||
%}
|
||||
|
||||
/* Some read-only variables */
|
||||
|
||||
%immutable;
|
||||
|
||||
%inline %{
|
||||
extern int status;
|
||||
extern char path[256];
|
||||
%}
|
||||
|
||||
%mutable;
|
||||
|
||||
/* Some helper functions to make it easier to test */
|
||||
%inline %{
|
||||
extern void print_vars();
|
||||
extern int *new_int(int value);
|
||||
extern int value_ent(int *value);
|
||||
|
||||
extern Point *new_Point(int x, int y);
|
||||
extern char *Point_print(Point *p);
|
||||
extern void pt_print();
|
||||
%}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -2,8 +2,10 @@
|
|||
|
||||
require "example.php";
|
||||
/* Try to set the values of some global variables */
|
||||
$a = "42.14";
|
||||
|
||||
ivar_set(42);
|
||||
ivar_set($a);
|
||||
echo "a = $a\n";
|
||||
svar_set(-31000);
|
||||
lvar_set(65537);
|
||||
uivar_set(123456);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue