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