import source from 1.3.40
[external/swig.git] / Examples / tcl / reference / runme.tcl
1 # file: runme.tcl
2
3 # This file illustrates the manipulation of C++ references in Tcl
4
5 catch { load ./example[info sharedlibextension] example}
6
7 # ----- Object creation -----
8
9 puts "Creating some objects:"
10 set  a [new_Vector 3 4 5]
11 set  b [new_Vector 10 11 12]
12
13 puts "    Created [Vector_print $a]"
14 puts "    Created [Vector_print $b]"
15
16 # ----- Call an overloaded operator -----
17
18 # This calls the wrapper we placed around
19 #
20 #      operator+(const Vector &a, const Vector &) 
21 #
22 # It returns a new allocated object.
23
24 puts "Adding a+b"
25 set c [addv $a $b]
26 puts "    a+b = [Vector_print $c]"
27
28 # Note: Unless we free the result, a memory leak will occur
29 delete_Vector $c
30
31 # ----- Create a vector array -----
32
33 # Note: Using the high-level interface here
34 puts "Creating an array of vectors"
35 VectorArray va 10
36 puts "    va = [va cget -this]"
37
38
39 # ----- Set some values in the array -----
40
41 # These operators copy the value of $a and $b to the vector array
42 va set 0 $a
43 va set 1 $b
44
45 # This will work, but it will cause a memory leak!
46
47 va set 2 [addv $a $b]
48
49 # The non-leaky way to do it
50
51 set c [addv $a $b]
52 va set 3 $c
53 delete_Vector $c
54
55 # Get some values from the array
56
57 puts "Getting some array values"
58 for {set i 0} {$i < 5} {incr i 1} {
59     puts "    va($i) = [Vector_print [va get $i]]"
60 }
61
62 # Watch under resource meter to check on this
63 puts "Making sure we don't leak memory."
64 for {set i 0} {$i < 1000000} {incr i 1} {
65     set c [va get [expr {$i % 10}]]
66 }
67
68 # ----- Clean up -----
69 puts "Cleaning up"
70
71 rename va ""
72
73 delete_Vector $a
74 delete_Vector $b
75
76