PageRenderTime 18ms CodeModel.GetById 1ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
D | 168 lines | 86 code | 24 blank | 58 comment | 35 complexity | 04e12d9ed4853a19a4f769f6601f4cce MD5 | raw file
  1/**
  2 * This test demonstrates director classes when the types are classes. Shown are
  3 * virtual function calls which use classes passed by
  4 *  - Value
  5 *  - Reference
  6 *  - Pointer
  7 * as both parameters and return values
  8 *
  9 * The test also demonstrates directors used with:
 10 *  - method overloading
 11 *  - default parameters
 12 *
 13 * Note: Methods with default parameters that call up from C++ cannot call the
 14 * overloaded D methods, see DefaultParms method.
 15
 16 * Expected output if PrintDebug enabled:
 17 * ------------ Start ------------
 18 * Base - Val(444.555)
 19 * Base - Ref(444.555)
 20 * Base - Ptr(444.555)
 21 * Base - FullyOverloaded(int 10)
 22 * Base - FullyOverloaded(bool 1)
 23 * Base - SemiOverloaded(int -678)
 24 * Base - SemiOverloaded(bool 1)
 25 * Base - DefaultParms(10, 2.2)
 26 * Base - DefaultParms(10, 1.1)
 27 * --------------------------------
 28 * Derived - Val(444.555)
 29 * Derived - Ref(444.555)
 30 * Derived - Ptr(444.555)
 31 * Derived - FullyOverloaded(int 10)
 32 * Derived - FullyOverloaded(bool 1)
 33 * Derived - SemiOverloaded(int -678)
 34 * Base - SemiOverloaded(bool 1)
 35 * Derived - DefaultParms(10, 2.2)
 36 * Derived - DefaultParms(10, 1.1)
 37 * --------------------------------
 38 * DDerived - Val(444.555)
 39 * DDerived - Ref(444.555)
 40 * DDerived - Ptr(444.555)
 41 * DDerived - FullyOverloaded(int 10)
 42 * DDerived - FullyOverloaded(bool True)
 43 * DDerived - SemiOverloaded(-678)
 44 * Base - SemiOverloaded(bool 1)
 45 * DDerived - DefaultParms(10, 2.2)
 46 * DDerived - DefaultParms(10, 1.1)
 47 * ------------ Finish ------------
 48 */
 49module director_classes_runme;
 50
 51import tango.io.Stdout;
 52import tango.text.Util;
 53import director_classes.director_classes;
 54import director_classes.Caller;
 55import director_classes.Base;
 56import director_classes.Derived;
 57import director_classes.DoubleHolder;
 58
 59void main() {
 60  if (PrintDebug) Stdout.formatln("------------ Start ------------ ");
 61
 62  auto myCaller = new Caller();
 63
 64  // Test C++ base class.
 65  {
 66    scope myBase = new Base(100.0);
 67    makeCalls(myCaller, myBase);
 68  }
 69
 70  if (PrintDebug) Stdout.formatln("--------------------------------");
 71
 72  // Test vanilla C++ wrapped derived class.
 73  {
 74    scope myBase = new Derived(200.0);
 75    makeCalls(myCaller, myBase);
 76  }
 77
 78  if (PrintDebug) Stdout.formatln("--------------------------------");
 79
 80  // Test director / D derived class.
 81  {
 82    scope myBase = new DDerived(300.0);
 83    makeCalls(myCaller, myBase);
 84  }
 85
 86  if (PrintDebug) Stdout.formatln("------------ Finish ------------ ");
 87}
 88
 89void makeCalls(Caller myCaller, Base myBase) {
 90  char[] myBaseType = myBase.classinfo.name.split(".")[$-1];
 91  myCaller.set(myBase);
 92
 93  DoubleHolder dh = new DoubleHolder(444.555);
 94
 95  // Class pointer, reference and pass by value tests
 96  if (myCaller.ValCall(dh).val != dh.val) throw new Exception("[1] failed");
 97  if (myCaller.RefCall(dh).val != dh.val) throw new Exception("[2] failed");
 98  if (myCaller.PtrCall(dh).val != dh.val) throw new Exception("[3] failed");
 99
100  // Fully overloaded method test (all methods in base class are overloaded)
101  if (myCaller.FullyOverloadedCall(10) != myBaseType ~ "::FullyOverloaded(int)") throw new Exception("[4] failed");
102  if (myCaller.FullyOverloadedCall(true) != myBaseType ~ "::FullyOverloaded(bool)") throw new Exception("[5] failed");
103
104  // Semi overloaded method test (some methods in base class are overloaded)
105  if (myCaller.SemiOverloadedCall(-678) != myBaseType ~ "::SemiOverloaded(int)") throw new Exception("[6] failed");
106  if (myCaller.SemiOverloadedCall(true) != "Base" ~ "::SemiOverloaded(bool)") throw new Exception("[7] failed");
107
108  // Default parameters methods test
109  if (myCaller.DefaultParmsCall(10, 2.2) != myBaseType ~ "::DefaultParms(int, double)") throw new Exception("[8] failed");
110  if (myBase.classinfo == DDerived.classinfo) { // special handling for D derived classes, there is no way to do this any other way
111    if (myCaller.DefaultParmsCall(10) != myBaseType ~ "::DefaultParms(int, double)") throw new Exception("[9] failed");
112  } else {
113    if (myCaller.DefaultParmsCall(10) != myBaseType ~ "::DefaultParms(int)") throw new Exception("[10] failed");
114  }
115
116  myCaller.reset();
117}
118
119public class DDerived : Base {
120  public this(double dd) {
121    super(dd);
122  }
123
124  public override DoubleHolder Val(DoubleHolder x) {
125    if (PrintDebug) Stdout.formatln("DDerived - Val({0:d3})", x.val);
126    return x;
127  }
128
129  public override DoubleHolder Ref(DoubleHolder x) {
130    if (PrintDebug) Stdout.formatln("DDerived - Ref({0:d3})", x.val);
131    return x;
132  }
133
134  public override DoubleHolder Ptr(DoubleHolder x) {
135    if (PrintDebug) Stdout.formatln("DDerived - Ptr({0:d3})", x.val);
136    return x;
137  }
138
139  public override char[] FullyOverloaded(int x) {
140    if (PrintDebug) Stdout.formatln("DDerived - FullyOverloaded(int {0})", x);
141    return "DDerived::FullyOverloaded(int)";
142  }
143
144  public override char[] FullyOverloaded(bool x) {
145    if (PrintDebug) Stdout.formatln("DDerived - FullyOverloaded(bool {0})", x);
146    return "DDerived::FullyOverloaded(bool)";
147  }
148
149  public override char[] SemiOverloaded(int x) {
150    char[] ret = "DDerived::SemiOverloaded(int)";
151    if (PrintDebug) Stdout.formatln("DDerived - SemiOverloaded({0})", x);
152    return ret;
153  }
154  alias Base.SemiOverloaded SemiOverloaded; // Alias in SemiOverloaded(bool x).
155
156  public override char[] DefaultParms(int x, double y) {
157    char[] ret = "DDerived::DefaultParms(int, double)";
158    if (PrintDebug) Stdout.formatln("DDerived - DefaultParms({0}, {1:d1})", x, y);
159    return ret;
160  }
161  // This method will never be called from C++ code because the two-parameter
162  // DefaultParams() has a default value for the second parameter there. It is
163  // only here to ensure consistent behavior for calls from C++ and D code.
164  public override char[] DefaultParms(int x) {
165    if (PrintDebug) Stdout.formatln("DDerived - DefaultParms({0})", x);
166    return DefaultParms(x, 1.1/*use C++ default here*/);
167  }
168}