Convert to use proxy classes.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@11601 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Olly Betts 2009-08-17 01:08:23 +00:00
commit 3141dfd599
5 changed files with 25 additions and 28 deletions

View file

@ -4,7 +4,7 @@ CXXSRCS = example.cxx
TARGET = example
INTERFACE = example.i
LIBS =
SWIGOPT = -noproxy
SWIGOPT =
all::
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \

View file

@ -17,7 +17,7 @@ Vector operator+(const Vector &a, const Vector &b) {
return r;
}
char *Vector::print() {
char *Vector::as_string() {
static char temp[512];
sprintf(temp,"Vector %p (%g,%g,%g)", this, x,y,z);
return temp;
@ -47,4 +47,3 @@ int VectorArray::size() {
printf("VectorArray: size %d self=%p\n",maxsize,this);
return maxsize;
}

View file

@ -7,7 +7,7 @@ public:
Vector() : x(0), y(0), z(0) { };
Vector(double x, double y, double z) : x(x), y(y), z(z) { };
friend Vector operator+(const Vector &a, const Vector &b);
char *print();
char *as_string();
};
class VectorArray {

View file

@ -12,7 +12,7 @@ class Vector {
public:
Vector(double x, double y, double z);
~Vector();
char *print();
char *as_string();
};
/* This helper function calls an overloaded operator */
@ -41,4 +41,3 @@ public:
}
}
};

View file

@ -1,18 +1,17 @@
<?php
# This file illustrates the manipulation of C++ references in Php.
# This uses the low-level interface. Proxy classes work differently.
# This file illustrates the manipulation of C++ references in PHP.
require "example.php";
# ----- Object creation -----
print "Creating some objects:\n";
$a = new_Vector(3,4,5);
$b = new_Vector(10,11,12);
$a = new Vector(3,4,5);
$b = new Vector(10,11,12);
print " Created a: $a " . Vector_print($a) . "\n";
print " Created b: $b " . Vector_print($b) . "\n";
print " Created a: {$a->as_string()}\n";
print " Created b: {$b->as_string()}\n";
# ----- Call an overloaded operator -----
@ -23,8 +22,8 @@ print " Created b: $b " . Vector_print($b) . "\n";
# It returns a new allocated object.
print "Adding a+b\n";
$c = addv($a,$b);
print " a+b =". Vector_print($c)."\n";
$c = example::addv($a,$b);
print " a+b ={$c->as_string()}\n";
# Note: Unless we free the result, a memory leak will occur
$c = None;
@ -33,46 +32,46 @@ $c = None;
# Note: Using the high-level interface here
print "Creating an array of vectors\n";
$va = new_VectorArray(10);
$va = new VectorArray(10);
print " va: $va size=".VectorArray_size($va)."\n";
print " va: size={$va->size()}\n";
# ----- Set some values in the array -----
# These operators copy the value of $a and $b to the vector array
VectorArray_set($va,0,$a);
VectorArray_set($va,1,$b);
$va->set(0,$a);
$va->set(1,$b);
VectorArray_get($va,0);
$va->get(0);
# This will work, but it will cause a memory leak!
VectorArray_set($va,2,addv($a,$b));
$va->set(2,addv($a,$b));
# The non-leaky way to do it
$c = addv($a,$b);
VectorArray_set($va,3,$c);
$c = None;
$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";
print " va($i) = ". Vector_print(VectorArray_get($va,$i)). "\n";
print "do $i\n";
print " va($i) = {$va->get($i)->as_string()}\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);
# $c = $va->get($i % 10);
#}
# ----- Clean up -----
print "Cleaning up\n";
# wants fixing FIXME
$va = None;
$a = None;
$b = None;
$va = NULL;
$a = NULL;
$b = NULL;
?>