3 # This file illustrates the manipulation of C++ references in Perl.
4 # This uses the low-level interface. Proxy classes work differently.
8 # ----- Object creation -----
10 print "Creating some objects:\n";
11 $a = example::new_Vector(3,4,5);
12 $b = example::new_Vector(10,11,12);
14 print " Created",example::Vector_print($a),"\n";
15 print " Created",example::Vector_print($b),"\n";
17 # ----- Call an overloaded operator -----
19 # This calls the wrapper we placed around
21 # operator+(const Vector &a, const Vector &)
23 # It returns a new allocated object.
26 $c = example::addv($a,$b);
27 print " a+b =", example::Vector_print($c),"\n";
29 # Note: Unless we free the result, a memory leak will occur
30 example::delete_Vector($c);
32 # ----- Create a vector array -----
34 # Note: Using the high-level interface here
35 print "Creating an array of vectors\n";
36 $va = example::new_VectorArray(10);
39 # ----- Set some values in the array -----
41 # These operators copy the value of $a and $b to the vector array
42 example::VectorArray_set($va,0,$a);
43 example::VectorArray_set($va,1,$b);
45 # This will work, but it will cause a memory leak!
47 example::VectorArray_set($va,2,example::addv($a,$b));
49 # The non-leaky way to do it
51 $c = example::addv($a,$b);
52 example::VectorArray_set($va,3,$c);
53 example::delete_Vector($c);
55 # Get some values from the array
57 print "Getting some array values\n";
58 for ($i = 0; $i < 5; $i++) {
59 print " va($i) = ", example::Vector_print(example::VectorArray_get($va,$i)), "\n";
62 # Watch under resource meter to check on this
63 print "Making sure we don't leak memory.\n";
64 for ($i = 0; $i < 1000000; $i++) {
65 $c = example::VectorArray_get($va,$i % 10);
68 # ----- Clean up -----
69 print "Cleaning up\n";
71 example::delete_VectorArray($va);
72 example::delete_Vector($a);
73 example::delete_Vector($b);