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