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
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;
|
||||
|
||||
?>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue