aboutsummaryrefslogtreecommitdiff
path: root/Examples/go/reference/runme.go
blob: 7391d9c8b3d6bb5eabb997872236cf22c9a46472 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// This example illustrates the manipulation of C++ references in Java.

package main

import (
	"fmt"

	. "swigtests/example"
)

func main() {
	fmt.Println("Creating some objects:")
	a := NewVector(3, 4, 5)
	b := NewVector(10, 11, 12)

	fmt.Println("    Created ", a.Print())
	fmt.Println("    Created ", b.Print())

	// ----- Call an overloaded operator -----

	// This calls the wrapper we placed around
	//
	//      operator+(const Vector &a, const Vector &)
	//
	// It returns a new allocated object.

	fmt.Println("Adding a+b")
	c := Addv(a, b)
	fmt.Println("    a+b = " + c.Print())

	// Because addv returns a reference, Addv will return a
	// pointer allocated using Go's memory allocator.  That means
	// that it will be freed by Go's garbage collector, and we can
	// not use DeleteVector to release it.

	c = nil

	// ----- Create a vector array -----

	fmt.Println("Creating an array of vectors")
	va := NewVectorArray(10)
	fmt.Println("    va = ", va)

	// ----- Set some values in the array -----

	// These operators copy the value of Vector a and Vector b to
	// the vector array
	va.Set(0, a)
	va.Set(1, b)

	va.Set(2, Addv(a, b))

	// Get some values from the array

	fmt.Println("Getting some array values")
	for i := 0; i < 5; i++ {
		fmt.Println("    va(", i, ") = ", va.Get(i).Print())
	}

	// Watch under resource meter to check on this
	fmt.Println("Making sure we don't leak memory.")
	for i := 0; i < 1000000; i++ {
		c = va.Get(i % 10)
	}

	// ----- Clean up ----- This could be omitted. The garbage
	// collector would then clean up for us.
	fmt.Println("Cleaning up")
	DeleteVectorArray(va)
	DeleteVector(a)
	DeleteVector(b)
}