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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
package main
import "fmt"
import . "./director_classic"
type TargetLangPerson struct{} // From Person
func (p *TargetLangPerson) Id() string {
return "TargetLangPerson"
}
type TargetLangChild struct{} // Form Child
func (p *TargetLangChild) Id() string {
return "TargetLangChild"
}
type TargetLangGrandChild struct{} // From Grandchild
func (p *TargetLangGrandChild) Id() string {
return "TargetLangGrandChild"
}
// Semis - don't override id() in target language
type TargetLangSemiPerson struct{} // From Person
type TargetLangSemiChild struct{} // From Child
type TargetLangSemiGrandChild struct{} // From GrandChild
// Orphans - don't override id() in C++
type TargetLangOrphanPerson struct{} // From OrphanPerson
func (p *TargetLangOrphanPerson) Id() string {
return "TargetLangOrphanPerson"
}
type TargetLangOrphanChild struct{} // From OrphanChild
func (p *TargetLangOrphanChild) Id() string {
return "TargetLangOrphanChild"
}
func check(person Person, expected string) {
debug := false
// Normal target language polymorphic call
ret := person.Id()
if debug {
fmt.Println(ret)
}
if ret != expected {
panic("Failed. Received: " + ret + " Expected: " + expected)
}
// Polymorphic call from C++
caller := NewCaller()
caller.SetCallback(person)
ret = caller.Call()
if debug {
fmt.Println(ret)
}
if ret != expected {
panic("Failed. Received: " + ret + " Expected: " + expected)
}
// Polymorphic call of object created in target language and
// passed to C++ and back again
baseclass := caller.BaseClass()
ret = baseclass.Id()
if debug {
fmt.Println(ret)
}
if ret != expected {
panic("Failed. Received: " + ret + " Expected: " + expected)
}
caller.ResetCallback()
if debug {
fmt.Println("----------------------------------------")
}
}
func main() {
person := NewPerson()
check(person, "Person")
DeletePerson(person)
person = NewChild()
check(person, "Child")
DeletePerson(person)
person = NewGrandChild()
check(person, "GrandChild")
DeletePerson(person)
person = NewDirectorPerson(&TargetLangPerson{})
check(person, "TargetLangPerson")
DeleteDirectorPerson(person)
person = NewDirectorChild(&TargetLangChild{})
check(person, "TargetLangChild")
DeleteDirectorChild(person.(Child))
person = NewDirectorGrandChild(&TargetLangGrandChild{})
check(person, "TargetLangGrandChild")
DeleteDirectorGrandChild(person.(GrandChild))
// Semis - don't override id() in target language
person = NewDirectorPerson(&TargetLangSemiPerson{})
check(person, "Person")
DeleteDirectorPerson(person)
person = NewDirectorChild(&TargetLangSemiChild{})
check(person, "Child")
DeleteDirectorChild(person.(Child))
person = NewDirectorGrandChild(&TargetLangSemiGrandChild{})
check(person, "GrandChild")
DeleteDirectorGrandChild(person.(GrandChild))
// Orphans - don't override id() in C++
person = NewOrphanPerson()
check(person, "Person")
DeleteOrphanPerson(person.(OrphanPerson))
person = NewOrphanChild()
check(person, "Child")
DeleteOrphanChild(person.(OrphanChild))
person = NewDirectorOrphanPerson(&TargetLangOrphanPerson{})
check(person, "TargetLangOrphanPerson")
DeleteDirectorOrphanPerson(person.(OrphanPerson))
person = NewDirectorOrphanChild(&TargetLangOrphanChild{})
check(person, "TargetLangOrphanChild")
DeleteDirectorOrphanChild(person.(OrphanChild))
}
|