summaryrefslogtreecommitdiff
path: root/Examples/test-suite/d/director_classic_runme.2.d
blob: c50781b07dfd3262779dc22f1791e0e29cc4c390 (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
module director_classic_runme;

import std.exception;
import std.stdio;
import director_classic.Caller;
import director_classic.Person;
import director_classic.Child;
import director_classic.GrandChild;
import director_classic.OrphanPerson;
import director_classic.OrphanChild;

enum TRACE = false;

void main() {
  {
    scope person = new Person();
    check(person, "Person");
  }
  {
    scope person = new Child();
    check(person, "Child");
  }
  {
    scope person = new GrandChild();
    check(person, "GrandChild");
  }
  {
    scope person = new TargetLangPerson();
    check(person, "TargetLangPerson");
  }
  {
    scope person = new TargetLangChild();
    check(person, "TargetLangChild");
  }
  {
    scope person = new TargetLangGrandChild();
    check(person, "TargetLangGrandChild");
  }

  // Semis - don't override id() in target language
  {
    scope person = new TargetLangSemiPerson();
    check(person, "Person");
  }
  {
    scope person = new TargetLangSemiChild();
    check(person, "Child");
  }
  {
    scope person = new TargetLangSemiGrandChild();
    check(person, "GrandChild");
  }

  // Orphans - don't override id() in C++
  {
    scope person = new OrphanPerson();
    check(person, "Person");
  }
  {
    scope person = new OrphanChild();
    check(person, "Child");
  }
  {
    scope person = new TargetLangOrphanPerson();
    check(person, "TargetLangOrphanPerson");
  }
  {
    scope person = new TargetLangOrphanChild();
    check(person, "TargetLangOrphanChild");
  }

  // Duals - id() makes an upcall to the base id()
  {
    scope person = new TargetLangDualPerson();
    check(person, "TargetLangDualPerson + Person");
  }
  {
    scope person = new TargetLangDualChild();
    check(person, "TargetLangDualChild + Child");
  }
  {
    scope person = new TargetLangDualGrandChild();
    check(person, "TargetLangDualGrandChild + GrandChild");
  }

  // Mix Orphans and Duals
  {
    scope person = new TargetLangDualOrphanPerson();
    check(person, "TargetLangDualOrphanPerson + Person");
  }
  {
    scope person = new TargetLangDualOrphanChild();
    check(person, "TargetLangDualOrphanChild + Child");
  }
}

void check(Person person, string expected) {
  string ret;
  // Normal D polymorphic call.
  ret = person.id();
  if (TRACE) writeln(ret);
  enforce(ret == expected, "Failed. Received: " ~ ret ~ " Expected: " ~ expected);

  // Polymorphic call from C++.
  auto caller = new Caller();
  caller.setCallback(person);
  ret = caller.call();
  if (TRACE) writeln(ret);
  enforce(ret == expected, "Failed. Received: " ~ ret ~ " Expected: " ~ expected);

  // Polymorphic call of object created in D and passed to C++ and back again.
  Person baseclass = caller.baseClass();
  ret = baseclass.id();
  if (TRACE) writeln(ret);
  enforce(ret == expected, "Failed. Received: " ~ ret ~ " Expected: " ~ expected);

  caller.resetCallback();
  if (TRACE)
    writeln("----------------------------------------");
}


// »Full« target language persons.
class TargetLangPerson : Person {
  public override string id() {
    return "TargetLangPerson";
  }
}

class TargetLangChild : Child {
  public override string id() {
    return "TargetLangChild";
  }
}

class TargetLangGrandChild : GrandChild {
  public override string id() {
    return "TargetLangGrandChild";
  }
}


// Semis - don't override id() in target language
class TargetLangSemiPerson : Person {
  // No id() override
}

class TargetLangSemiChild : Child {
  // No id() override
}

class TargetLangSemiGrandChild : GrandChild {
  // No id() override
}


// Orphans - don't override id() in C++
class TargetLangOrphanPerson : OrphanPerson {
  public override string id() {
    return "TargetLangOrphanPerson";
  }
}

class TargetLangOrphanChild : OrphanChild {
  public override string id() {
    return "TargetLangOrphanChild";
  }
}


// Duals - id() makes an upcall to the base id()
class TargetLangDualPerson : Person {
  public override string id() {
    return "TargetLangDualPerson + " ~ super.id();
  }
}

class TargetLangDualChild : Child {
  public override string id() {
    return "TargetLangDualChild + " ~ super.id();
  }
}

class TargetLangDualGrandChild : GrandChild {
  public override string id() {
    return "TargetLangDualGrandChild + " ~ super.id();
  }
}


// Mix Orphans and Duals
class TargetLangDualOrphanPerson : OrphanPerson {
  public override string id() {
    return "TargetLangDualOrphanPerson + " ~ super.id();
  }
}

class TargetLangDualOrphanChild : OrphanChild {
  public override string id() {
    return "TargetLangDualOrphanChild + " ~ super.id();
  }
}