PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/go/extend/runme.go

#
Go | 76 lines | 27 code | 15 blank | 34 comment | 0 complexity | 8abe259918c361552593cdc570bf886a MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. // This file illustrates the cross language polymorphism using directors.
  2. package main
  3. import (
  4. "fmt"
  5. . "./example"
  6. )
  7. type CEO struct{}
  8. func (p *CEO) GetPosition() string {
  9. return "CEO"
  10. }
  11. func main() {
  12. // Create an instance of CEO, a class derived from the Go
  13. // proxy of the underlying C++ class. The calls to getName()
  14. // and getPosition() are standard, the call to getTitle() uses
  15. // the director wrappers to call CEO.getPosition().
  16. e := NewDirectorManager(new(CEO), "Alice")
  17. fmt.Println(e.GetName(), " is a ", e.GetPosition())
  18. fmt.Println("Just call her \"", e.GetTitle(), "\"")
  19. fmt.Println("----------------------")
  20. // Create a new EmployeeList instance. This class does not
  21. // have a C++ director wrapper, but can be used freely with
  22. // other classes that do.
  23. list := NewEmployeeList()
  24. // EmployeeList owns its items, so we must surrender ownership
  25. // of objects we add.
  26. // e.DisownMemory()
  27. list.AddEmployee(e)
  28. fmt.Println("----------------------")
  29. // Now we access the first four items in list (three are C++
  30. // objects that EmployeeList's constructor adds, the last is
  31. // our CEO). The virtual methods of all these instances are
  32. // treated the same. For items 0, 1, and 2, all methods
  33. // resolve in C++. For item 3, our CEO, GetTitle calls
  34. // GetPosition which resolves in Go. The call to GetPosition
  35. // is slightly different, however, because of the overidden
  36. // GetPosition() call, since now the object reference has been
  37. // "laundered" by passing through EmployeeList as an
  38. // Employee*. Previously, Go resolved the call immediately in
  39. // CEO, but now Go thinks the object is an instance of class
  40. // Employee. So the call passes through the Employee proxy
  41. // class and on to the C wrappers and C++ director, eventually
  42. // ending up back at the Java CEO implementation of
  43. // getPosition(). The call to GetTitle() for item 3 runs the
  44. // C++ Employee::getTitle() method, which in turn calls
  45. // GetPosition(). This virtual method call passes down
  46. // through the C++ director class to the Java implementation
  47. // in CEO. All this routing takes place transparently.
  48. fmt.Println("(position, title) for items 0-3:")
  49. fmt.Println(" ", list.Get_item(0).GetPosition(), ", \"", list.Get_item(0).GetTitle(), "\"")
  50. fmt.Println(" ", list.Get_item(1).GetPosition(), ", \"", list.Get_item(1).GetTitle(), "\"")
  51. fmt.Println(" ", list.Get_item(2).GetPosition(), ", \"", list.Get_item(2).GetTitle(), "\"")
  52. fmt.Println(" ", list.Get_item(3).GetPosition(), ", \"", list.Get_item(3).GetTitle(), "\"")
  53. fmt.Println("----------------------")
  54. // Time to delete the EmployeeList, which will delete all the
  55. // Employee* items it contains. The last item is our CEO,
  56. // which gets destroyed as well.
  57. DeleteEmployeeList(list)
  58. fmt.Println("----------------------")
  59. // All done.
  60. fmt.Println("Go exit")
  61. }