Upstream version 1.3.40
[profile/ivi/swig.git] / Examples / perl5 / reference / runme.pl
1 # file: runme.pl
2
3 # This file illustrates the manipulation of C++ references in Perl.
4 # This uses the low-level interface.  Proxy classes work differently.
5
6 use example;
7
8 # ----- Object creation -----
9
10 print "Creating some objects:\n";
11 $a = example::new_Vector(3,4,5);
12 $b = example::new_Vector(10,11,12);
13
14 print "    Created",example::Vector_print($a),"\n";
15 print "    Created",example::Vector_print($b),"\n";
16
17 # ----- Call an overloaded operator -----
18
19 # This calls the wrapper we placed around
20 #
21 #      operator+(const Vector &a, const Vector &) 
22 #
23 # It returns a new allocated object.
24
25 print "Adding a+b\n";
26 $c = example::addv($a,$b);
27 print "    a+b =", example::Vector_print($c),"\n";
28
29 # Note: Unless we free the result, a memory leak will occur
30 example::delete_Vector($c);
31
32 # ----- Create a vector array -----
33
34 # Note: Using the high-level interface here
35 print "Creating an array of vectors\n";
36 $va = example::new_VectorArray(10);
37 print "    va = $va\n";
38
39 # ----- Set some values in the array -----
40
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);
44
45 # This will work, but it will cause a memory leak!
46
47 example::VectorArray_set($va,2,example::addv($a,$b));
48
49 # The non-leaky way to do it
50
51 $c = example::addv($a,$b);
52 example::VectorArray_set($va,3,$c);
53 example::delete_Vector($c);
54
55 # Get some values from the array
56
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";
60 }
61
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);
66 }
67
68 # ----- Clean up -----
69 print "Cleaning up\n";
70
71 example::delete_VectorArray($va);
72 example::delete_Vector($a);
73 example::delete_Vector($b);
74