diff --git a/Examples/php4/Makefile.php b/Examples/php4/Makefile.php index ed9d786e5..399fdfc7a 100644 --- a/Examples/php4/Makefile.php +++ b/Examples/php4/Makefile.php @@ -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++ example.i" - echo "# and then the rest is always the same..." - echo "phpize && ./configure && make && make install" - echo - echo - exit 1 diff --git a/Examples/php4/class/BUILD.sh b/Examples/php4/class/BUILD.sh index 99ee15273..241ed0228 100755 --- a/Examples/php4/class/BUILD.sh +++ b/Examples/php4/class/BUILD.sh @@ -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 diff --git a/Examples/php4/class/example.cxx b/Examples/php4/class/example.cxx index 1e8e203dd..f171f10e9 100644 --- a/Examples/php4/class/example.cxx +++ b/Examples/php4/class/example.cxx @@ -1,7 +1,14 @@ /* File : example.c */ #include "example.h" -#define M_PI 3.14159265358979323846 +#include +#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; } diff --git a/Examples/php4/class/example.h b/Examples/php4/class/example.h index 46d901361..389679469 100644 --- a/Examples/php4/class/example.h +++ b/Examples/php4/class/example.h @@ -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); }; diff --git a/Examples/php4/class/runme.php4 b/Examples/php4/class/runme.php4 index 653ced256..a9ca657dc 100644 --- a/Examples/php4/class/runme.php4 +++ b/Examples/php4/class/runme.php4 @@ -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"; diff --git a/Examples/php4/constants/BUILD.sh b/Examples/php4/constants/BUILD.sh index 19b79dbc6..2dfdbbc67 100755 --- a/Examples/php4/constants/BUILD.sh +++ b/Examples/php4/constants/BUILD.sh @@ -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 diff --git a/Examples/php4/cpointer/BUILD.sh b/Examples/php4/cpointer/BUILD.sh new file mode 100755 index 000000000..b79290003 --- /dev/null +++ b/Examples/php4/cpointer/BUILD.sh @@ -0,0 +1,5 @@ +#! /bin/sh -e + +${SWIG:=swig} -php4 -make -noproxy -withc example.c example.i +make +php -d extension_dir=. runme.php4 diff --git a/Examples/php4/cpointer/example.c b/Examples/php4/cpointer/example.c new file mode 100644 index 000000000..3326dec3e --- /dev/null +++ b/Examples/php4/cpointer/example.c @@ -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; +} diff --git a/Examples/php4/cpointer/example.i b/Examples/php4/cpointer/example.i new file mode 100644 index 000000000..52e6df190 --- /dev/null +++ b/Examples/php4/cpointer/example.i @@ -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); + + + + + diff --git a/Examples/php4/cpointer/runme.php4 b/Examples/php4/cpointer/runme.php4 new file mode 100644 index 000000000..f5ef08a3c --- /dev/null +++ b/Examples/php4/cpointer/runme.php4 @@ -0,0 +1,45 @@ + diff --git a/Examples/php4/enum/BUILD.sh b/Examples/php4/enum/BUILD.sh index cccb24029..241ed0228 100755 --- a/Examples/php4/enum/BUILD.sh +++ b/Examples/php4/enum/BUILD.sh @@ -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 diff --git a/Examples/php4/funcptr/BUILD.sh b/Examples/php4/funcptr/BUILD.sh index 881e5881f..d09461979 100755 --- a/Examples/php4/funcptr/BUILD.sh +++ b/Examples/php4/funcptr/BUILD.sh @@ -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 diff --git a/Examples/php4/funcptr/example.h b/Examples/php4/funcptr/example.h index 9936e24fc..58989db79 100644 --- a/Examples/php4/funcptr/example.h +++ b/Examples/php4/funcptr/example.h @@ -5,5 +5,3 @@ extern int add(int,int); extern int sub(int,int); extern int mul(int,int); -extern int (*funcvar)(int,int); - diff --git a/Examples/php4/overloading/BUILD.sh b/Examples/php4/overloading/BUILD.sh new file mode 100755 index 000000000..6d08c3b23 --- /dev/null +++ b/Examples/php4/overloading/BUILD.sh @@ -0,0 +1,5 @@ +#! /bin/sh -e + +${SWIG:=swig} -php4 -make -c++ -withcxx example.cxx example.i +make +php -d extension_dir=. runme.php4 diff --git a/Examples/php4/overloading/example.cxx b/Examples/php4/overloading/example.cxx new file mode 100644 index 000000000..8d57f5dd9 --- /dev/null +++ b/Examples/php4/overloading/example.cxx @@ -0,0 +1,51 @@ +/* File : example.c */ + +#include "example.h" +#include +#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"; +} diff --git a/Examples/php4/overloading/example.h b/Examples/php4/overloading/example.h new file mode 100644 index 000000000..b7ffc31a6 --- /dev/null +++ b/Examples/php4/overloading/example.h @@ -0,0 +1,45 @@ +/* File : example.h */ + +#include + +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& ); + diff --git a/Examples/php4/overloading/example.i b/Examples/php4/overloading/example.i new file mode 100644 index 000000000..950d2549d --- /dev/null +++ b/Examples/php4/overloading/example.i @@ -0,0 +1,8 @@ +/* File : example.i */ +%module example + +%{ +#include "example.h" +%} + +%include "example.h" diff --git a/Examples/php4/overloading/runme.php4 b/Examples/php4/overloading/runme.php4 new file mode 100644 index 000000000..d77aacb88 --- /dev/null +++ b/Examples/php4/overloading/runme.php4 @@ -0,0 +1,59 @@ +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"; + +?> diff --git a/Examples/php4/pointer/BUILD.sh b/Examples/php4/pointer/BUILD.sh index 881e5881f..b79290003 100755 --- a/Examples/php4/pointer/BUILD.sh +++ b/Examples/php4/pointer/BUILD.sh @@ -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 diff --git a/Examples/php4/pointer/example.i b/Examples/php4/pointer/example.i index 66fb19956..a8014e50c 100644 --- a/Examples/php4/pointer/example.i +++ b/Examples/php4/pointer/example.i @@ -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 */ diff --git a/Examples/php4/pointer/runme.php4 b/Examples/php4/pointer/runme.php4 index 48f0ad631..5e86de6a2 100644 --- a/Examples/php4/pointer/runme.php4 +++ b/Examples/php4/pointer/runme.php4 @@ -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 diff --git a/Examples/php4/reference/BUILD-proxy.sh b/Examples/php4/reference/BUILD-proxy.sh new file mode 100755 index 000000000..b1c8c71a4 --- /dev/null +++ b/Examples/php4/reference/BUILD-proxy.sh @@ -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 diff --git a/Examples/php4/reference/BUILD.sh b/Examples/php4/reference/BUILD.sh index cccb24029..241ed0228 100755 --- a/Examples/php4/reference/BUILD.sh +++ b/Examples/php4/reference/BUILD.sh @@ -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 diff --git a/Examples/php4/reference/example.cxx b/Examples/php4/reference/example.cxx index 6c8dc8edb..d92a1b638 100644 --- a/Examples/php4/reference/example.cxx +++ b/Examples/php4/reference/example.cxx @@ -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; } diff --git a/Examples/php4/reference/example.i b/Examples/php4/reference/example.i index 73e8c62ec..0a7212d1d 100644 --- a/Examples/php4/reference/example.i +++ b/Examples/php4/reference/example.i @@ -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) { diff --git a/Examples/php4/reference/runme-proxy.php4 b/Examples/php4/reference/runme-proxy.php4 new file mode 100644 index 000000000..3224daf11 --- /dev/null +++ b/Examples/php4/reference/runme-proxy.php4 @@ -0,0 +1,79 @@ +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); + +?> diff --git a/Examples/php4/reference/runme.php4 b/Examples/php4/reference/runme.php4 index ecf9ce507..aea3a9453 100644 --- a/Examples/php4/reference/runme.php4 +++ b/Examples/php4/reference/runme.php4 @@ -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; ?> diff --git a/Examples/php4/shadow/BUILD.sh b/Examples/php4/shadow/BUILD.sh index 99b520879..ee6be4b4b 100755 --- a/Examples/php4/shadow/BUILD.sh +++ b/Examples/php4/shadow/BUILD.sh @@ -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 diff --git a/Examples/php4/shadow/example.cxx b/Examples/php4/shadow/example.cxx index bf088228f..94e4a7888 100644 --- a/Examples/php4/shadow/example.cxx +++ b/Examples/php4/shadow/example.cxx @@ -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); +} diff --git a/Examples/php4/shadow/example.h b/Examples/php4/shadow/example.h index 226ec3a9b..361dff898 100644 --- a/Examples/php4/shadow/example.h +++ b/Examples/php4/shadow/example.h @@ -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 ); diff --git a/Examples/php4/shadow/example.i b/Examples/php4/shadow/example.i index 23ee8a822..ce73746d0 100644 --- a/Examples/php4/shadow/example.i +++ b/Examples/php4/shadow/example.i @@ -7,5 +7,6 @@ /* Let's just grab the original header file here */ +%newobject CircleFactory; %include "example.h" diff --git a/Examples/php4/shadow/runme.php4 b/Examples/php4/shadow/runme.php4 index bd10dad6e..dea80e358 100644 --- a/Examples/php4/shadow/runme.php4 +++ b/Examples/php4/shadow/runme.php4 @@ -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"; ?> diff --git a/Examples/php4/simple/BUILD.sh b/Examples/php4/simple/BUILD.sh index 881e5881f..b79290003 100755 --- a/Examples/php4/simple/BUILD.sh +++ b/Examples/php4/simple/BUILD.sh @@ -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 diff --git a/Examples/php4/simple/example.i b/Examples/php4/simple/example.i index 24093b9bf..55e1d46a2 100644 --- a/Examples/php4/simple/example.i +++ b/Examples/php4/simple/example.i @@ -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); diff --git a/Examples/php4/sync/BUILD.sh b/Examples/php4/sync/BUILD.sh index 99b520879..6d08c3b23 100755 --- a/Examples/php4/sync/BUILD.sh +++ b/Examples/php4/sync/BUILD.sh @@ -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 diff --git a/Examples/php4/value/BUILD.sh b/Examples/php4/value/BUILD.sh index 881e5881f..b79290003 100755 --- a/Examples/php4/value/BUILD.sh +++ b/Examples/php4/value/BUILD.sh @@ -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 diff --git a/Examples/php4/value/example.c b/Examples/php4/value/example.c index 4ed2fe10a..3c402a214 100644 --- a/Examples/php4/value/example.c +++ b/Examples/php4/value/example.c @@ -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; } diff --git a/Examples/php4/value/example.h b/Examples/php4/value/example.h index 212cf4bdb..f55752a5f 100644 --- a/Examples/php4/value/example.h +++ b/Examples/php4/value/example.h @@ -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); diff --git a/Examples/php4/value/example.i b/Examples/php4/value/example.i index b25c15db1..48fd8ffe0 100644 --- a/Examples/php4/value/example.i +++ b/Examples/php4/value/example.i @@ -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); } diff --git a/Examples/php4/value/runme.php4 b/Examples/php4/value/runme.php4 index 80cb88408..49115376c 100644 --- a/Examples/php4/value/runme.php4 +++ b/Examples/php4/value/runme.php4 @@ -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); diff --git a/Examples/php4/variables/BUILD.sh b/Examples/php4/variables/BUILD.sh index 881e5881f..b79290003 100755 --- a/Examples/php4/variables/BUILD.sh +++ b/Examples/php4/variables/BUILD.sh @@ -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 diff --git a/Examples/php4/variables/example.h b/Examples/php4/variables/example.h index 0f7e89594..e481177aa 100644 --- a/Examples/php4/variables/example.h +++ b/Examples/php4/variables/example.h @@ -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]; + diff --git a/Examples/php4/variables/example.i b/Examples/php4/variables/example.i index 9ec751d71..a36029d92 100644 --- a/Examples/php4/variables/example.i +++ b/Examples/php4/variables/example.i @@ -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(); -%} + + + diff --git a/Examples/php4/variables/runme.php4 b/Examples/php4/variables/runme.php4 index d96f00359..41229880d 100644 --- a/Examples/php4/variables/runme.php4 +++ b/Examples/php4/variables/runme.php4 @@ -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);