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:
parent
3283728cf5
commit
3141dfd599
5 changed files with 25 additions and 28 deletions
|
|
@ -4,7 +4,7 @@ CXXSRCS = example.cxx
|
||||||
TARGET = example
|
TARGET = example
|
||||||
INTERFACE = example.i
|
INTERFACE = example.i
|
||||||
LIBS =
|
LIBS =
|
||||||
SWIGOPT = -noproxy
|
SWIGOPT =
|
||||||
|
|
||||||
all::
|
all::
|
||||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ Vector operator+(const Vector &a, const Vector &b) {
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *Vector::print() {
|
char *Vector::as_string() {
|
||||||
static char temp[512];
|
static char temp[512];
|
||||||
sprintf(temp,"Vector %p (%g,%g,%g)", this, x,y,z);
|
sprintf(temp,"Vector %p (%g,%g,%g)", this, x,y,z);
|
||||||
return temp;
|
return temp;
|
||||||
|
|
@ -47,4 +47,3 @@ int VectorArray::size() {
|
||||||
printf("VectorArray: size %d self=%p\n",maxsize,this);
|
printf("VectorArray: size %d self=%p\n",maxsize,this);
|
||||||
return maxsize;
|
return maxsize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ public:
|
||||||
Vector() : x(0), y(0), z(0) { };
|
Vector() : x(0), y(0), z(0) { };
|
||||||
Vector(double x, double y, double z) : x(x), y(y), z(z) { };
|
Vector(double x, double y, double z) : x(x), y(y), z(z) { };
|
||||||
friend Vector operator+(const Vector &a, const Vector &b);
|
friend Vector operator+(const Vector &a, const Vector &b);
|
||||||
char *print();
|
char *as_string();
|
||||||
};
|
};
|
||||||
|
|
||||||
class VectorArray {
|
class VectorArray {
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ class Vector {
|
||||||
public:
|
public:
|
||||||
Vector(double x, double y, double z);
|
Vector(double x, double y, double z);
|
||||||
~Vector();
|
~Vector();
|
||||||
char *print();
|
char *as_string();
|
||||||
};
|
};
|
||||||
|
|
||||||
/* This helper function calls an overloaded operator */
|
/* This helper function calls an overloaded operator */
|
||||||
|
|
@ -41,4 +41,3 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,17 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
# This file illustrates the manipulation of C++ references in Php.
|
# This file illustrates the manipulation of C++ references in PHP.
|
||||||
# This uses the low-level interface. Proxy classes work differently.
|
|
||||||
|
|
||||||
require "example.php";
|
require "example.php";
|
||||||
|
|
||||||
# ----- Object creation -----
|
# ----- Object creation -----
|
||||||
|
|
||||||
print "Creating some objects:\n";
|
print "Creating some objects:\n";
|
||||||
$a = new_Vector(3,4,5);
|
$a = new Vector(3,4,5);
|
||||||
$b = new_Vector(10,11,12);
|
$b = new Vector(10,11,12);
|
||||||
|
|
||||||
print " Created a: $a " . Vector_print($a) . "\n";
|
print " Created a: {$a->as_string()}\n";
|
||||||
print " Created b: $b " . Vector_print($b) . "\n";
|
print " Created b: {$b->as_string()}\n";
|
||||||
|
|
||||||
# ----- Call an overloaded operator -----
|
# ----- Call an overloaded operator -----
|
||||||
|
|
||||||
|
|
@ -23,8 +22,8 @@ print " Created b: $b " . Vector_print($b) . "\n";
|
||||||
# It returns a new allocated object.
|
# It returns a new allocated object.
|
||||||
|
|
||||||
print "Adding a+b\n";
|
print "Adding a+b\n";
|
||||||
$c = addv($a,$b);
|
$c = example::addv($a,$b);
|
||||||
print " a+b =". Vector_print($c)."\n";
|
print " a+b ={$c->as_string()}\n";
|
||||||
|
|
||||||
# Note: Unless we free the result, a memory leak will occur
|
# Note: Unless we free the result, a memory leak will occur
|
||||||
$c = None;
|
$c = None;
|
||||||
|
|
@ -33,46 +32,46 @@ $c = None;
|
||||||
|
|
||||||
# Note: Using the high-level interface here
|
# Note: Using the high-level interface here
|
||||||
print "Creating an array of vectors\n";
|
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 -----
|
# ----- Set some values in the array -----
|
||||||
|
|
||||||
# These operators copy the value of $a and $b to the vector array
|
# These operators copy the value of $a and $b to the vector array
|
||||||
VectorArray_set($va,0,$a);
|
$va->set(0,$a);
|
||||||
VectorArray_set($va,1,$b);
|
$va->set(1,$b);
|
||||||
|
|
||||||
VectorArray_get($va,0);
|
$va->get(0);
|
||||||
# This will work, but it will cause a memory leak!
|
# 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
|
# The non-leaky way to do it
|
||||||
|
|
||||||
$c = addv($a,$b);
|
$c = addv($a,$b);
|
||||||
VectorArray_set($va,3,$c);
|
$va->set(3,$c);
|
||||||
$c = None;
|
$c = NULL;
|
||||||
|
|
||||||
# Get some values from the array
|
# Get some values from the array
|
||||||
|
|
||||||
print "Getting some array values\n";
|
print "Getting some array values\n";
|
||||||
for ($i = 0; $i < 5; $i++) {
|
for ($i = 0; $i < 5; $i++) {
|
||||||
print "do $i\n";
|
print "do $i\n";
|
||||||
print " va($i) = ". Vector_print(VectorArray_get($va,$i)). "\n";
|
print " va($i) = {$va->get($i)->as_string()}\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
# Watch under resource meter to check on this
|
# Watch under resource meter to check on this
|
||||||
#print "Making sure we don't leak memory.\n";
|
#print "Making sure we don't leak memory.\n";
|
||||||
#for ($i = 0; $i < 1000000; $i++) {
|
#for ($i = 0; $i < 1000000; $i++) {
|
||||||
# $c = VectorArray_get($va,$i % 10);
|
# $c = $va->get($i % 10);
|
||||||
#}
|
#}
|
||||||
|
|
||||||
# ----- Clean up -----
|
# ----- Clean up -----
|
||||||
print "Cleaning up\n";
|
print "Cleaning up\n";
|
||||||
# wants fixing FIXME
|
# wants fixing FIXME
|
||||||
$va = None;
|
$va = NULL;
|
||||||
$a = None;
|
$a = NULL;
|
||||||
$b = None;
|
$b = NULL;
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue