PageRenderTime 26ms CodeModel.GetById 3ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/go/reference/runme.go

#
Go | 71 lines | 34 code | 18 blank | 19 comment | 2 complexity | 632359b8db363e0fbd88b8fca2e68bf7 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. // This example illustrates the manipulation of C++ references in Java.
  2. package main
  3. import (
  4. "fmt"
  5. . "./example"
  6. )
  7. func main() {
  8. fmt.Println("Creating some objects:")
  9. a := NewVector(3, 4, 5)
  10. b := NewVector(10, 11, 12)
  11. fmt.Println(" Created ", a.Print())
  12. fmt.Println(" Created ", b.Print())
  13. // ----- Call an overloaded operator -----
  14. // This calls the wrapper we placed around
  15. //
  16. // operator+(const Vector &a, const Vector &)
  17. //
  18. // It returns a new allocated object.
  19. fmt.Println("Adding a+b")
  20. c := Addv(a, b)
  21. fmt.Println(" a+b = " + c.Print())
  22. // Because addv returns a reference, Addv will return a
  23. // pointer allocated using Go's memory allocator. That means
  24. // that it will be freed by Go's garbage collector, and we can
  25. // not use DeleteVector to release it.
  26. c = nil
  27. // ----- Create a vector array -----
  28. fmt.Println("Creating an array of vectors")
  29. va := NewVectorArray(10)
  30. fmt.Println(" va = ", va)
  31. // ----- Set some values in the array -----
  32. // These operators copy the value of Vector a and Vector b to
  33. // the vector array
  34. va.Set(0, a)
  35. va.Set(1, b)
  36. va.Set(2, Addv(a, b))
  37. // Get some values from the array
  38. fmt.Println("Getting some array values")
  39. for i := 0; i < 5; i++ {
  40. fmt.Println(" va(", i, ") = ", va.Get(i).Print())
  41. }
  42. // Watch under resource meter to check on this
  43. fmt.Println("Making sure we don't leak memory.")
  44. for i := 0; i < 1000000; i++ {
  45. c = va.Get(i % 10)
  46. }
  47. // ----- Clean up ----- This could be omitted. The garbage
  48. // collector would then clean up for us.
  49. fmt.Println("Cleaning up")
  50. DeleteVectorArray(va)
  51. DeleteVector(a)
  52. DeleteVector(b)
  53. }