PageRenderTime 45ms CodeModel.GetById 1ms app.highlight 39ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/test-suite/d/director_classic_runme.1.d

#
D | 207 lines | 163 code | 29 blank | 15 comment | 10 complexity | e5660832566622d13dcfc9df9cb21007 MD5 | raw file
  1module director_classic_runme;
  2
  3import tango.io.Stdout;
  4import director_classic.Caller;
  5import director_classic.Person;
  6import director_classic.Child;
  7import director_classic.GrandChild;
  8import director_classic.OrphanPerson;
  9import director_classic.OrphanChild;
 10
 11const bool TRACE = false;
 12
 13void main() {
 14  {
 15    scope person = new Person();
 16    check(person, "Person");
 17  }
 18  {
 19    scope person = new Child();
 20    check(person, "Child");
 21  }
 22  {
 23    scope person = new GrandChild();
 24    check(person, "GrandChild");
 25  }
 26  {
 27    scope person = new TargetLangPerson();
 28    check(person, "TargetLangPerson");
 29  }
 30  {
 31    scope person = new TargetLangChild();
 32    check(person, "TargetLangChild");
 33  }
 34  {
 35    scope person = new TargetLangGrandChild();
 36    check(person, "TargetLangGrandChild");
 37  }
 38
 39  // Semis - don't override id() in target language
 40  {
 41    scope person = new TargetLangSemiPerson();
 42    check(person, "Person");
 43  }
 44  {
 45    scope person = new TargetLangSemiChild();
 46    check(person, "Child");
 47  }
 48  {
 49    scope person = new TargetLangSemiGrandChild();
 50    check(person, "GrandChild");
 51  }
 52
 53  // Orphans - don't override id() in C++
 54  {
 55    scope person = new OrphanPerson();
 56    check(person, "Person");
 57  }
 58  {
 59    scope person = new OrphanChild();
 60    check(person, "Child");
 61  }
 62  {
 63    scope person = new TargetLangOrphanPerson();
 64    check(person, "TargetLangOrphanPerson");
 65  }
 66  {
 67    scope person = new TargetLangOrphanChild();
 68    check(person, "TargetLangOrphanChild");
 69  }
 70
 71  // Duals - id() makes an upcall to the base id()
 72  {
 73    scope person = new TargetLangDualPerson();
 74    check(person, "TargetLangDualPerson + Person");
 75  }
 76  {
 77    scope person = new TargetLangDualChild();
 78    check(person, "TargetLangDualChild + Child");
 79  }
 80  {
 81    scope person = new TargetLangDualGrandChild();
 82    check(person, "TargetLangDualGrandChild + GrandChild");
 83  }
 84
 85  // Mix Orphans and Duals
 86  {
 87    scope person = new TargetLangDualOrphanPerson();
 88    check(person, "TargetLangDualOrphanPerson + Person");
 89  }
 90  {
 91    scope person = new TargetLangDualOrphanChild();
 92    check(person, "TargetLangDualOrphanChild + Child");
 93  }
 94}
 95
 96void check(Person person, char[] expected) {
 97  char[] ret;
 98  // Normal D polymorphic call.
 99  ret = person.id();
100  if (TRACE)
101    Stdout(ret).newline;
102  if (ret != expected)
103    throw new Exception("Failed. Received: " ~ ret ~ " Expected: " ~ expected);
104
105  // Polymorphic call from C++.
106  Caller caller = new Caller();
107  caller.setCallback(person);
108  ret = caller.call();
109  if (TRACE)
110    Stdout(ret).newline;
111  if (ret != expected)
112    throw new Exception("Failed. Received: " ~ ret ~ " Expected: " ~ expected);
113
114  // Polymorphic call of object created in D and passed to C++ and back again.
115  Person baseclass = caller.baseClass();
116  ret = baseclass.id();
117  if (TRACE)
118    Stdout(ret).newline;
119  if (ret != expected)
120    throw new Exception("Failed. Received: " ~ ret ~ " Expected: " ~ expected);
121
122  caller.resetCallback();
123  if (TRACE)
124    Stdout("----------------------------------------").newline;
125}
126
127
128// ťFullŤ target language persons.
129class TargetLangPerson : Person {
130  public override char[] id() {
131    return "TargetLangPerson";
132  }
133}
134
135class TargetLangChild : Child {
136  public override char[] id() {
137    return "TargetLangChild";
138  }
139}
140
141class TargetLangGrandChild : GrandChild {
142  public override char[] id() {
143    return "TargetLangGrandChild";
144  }
145}
146
147
148// Semis - don't override id() in target language
149class TargetLangSemiPerson : Person {
150  // No id() override
151}
152
153class TargetLangSemiChild : Child {
154  // No id() override
155}
156
157class TargetLangSemiGrandChild : GrandChild {
158  // No id() override
159}
160
161
162// Orphans - don't override id() in C++
163class TargetLangOrphanPerson : OrphanPerson {
164  public override char[] id() {
165    return "TargetLangOrphanPerson";
166  }
167}
168
169class TargetLangOrphanChild : OrphanChild {
170  public override char[] id() {
171    return "TargetLangOrphanChild";
172  }
173}
174
175
176// Duals - id() makes an upcall to the base id()
177class TargetLangDualPerson : Person {
178  public override char[] id() {
179    return "TargetLangDualPerson + " ~ super.id();
180  }
181}
182
183class TargetLangDualChild : Child {
184  public override char[] id() {
185    return "TargetLangDualChild + " ~ super.id();
186  }
187}
188
189class TargetLangDualGrandChild : GrandChild {
190  public override char[] id() {
191    return "TargetLangDualGrandChild + " ~ super.id();
192  }
193}
194
195
196// Mix Orphans and Duals
197class TargetLangDualOrphanPerson : OrphanPerson {
198  public override char[] id() {
199    return "TargetLangDualOrphanPerson + " ~ super.id();
200  }
201}
202
203class TargetLangDualOrphanChild : OrphanChild {
204  public override char[] id() {
205    return "TargetLangDualOrphanChild + " ~ super.id();
206  }
207}