PageRenderTime 23ms CodeModel.GetById 12ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/trunk/Examples/test-suite/csharp/director_classes_runme.cs

#
C# | 180 lines | 100 code | 21 blank | 59 comment | 35 complexity | 2fe63b0472da0b9c75b03c9fc2824c9d MD5 | raw file
  1/*
  2This test demonstrates director classes when the types are classes.
  3Shown are virtual function calls which use classes passed by:
  4  - Value
  5  - Reference
  6  - Pointer
  7as both parameters and return values.
  8The test also demonstrates directors used with:
  9  - method overloading
 10  - default parameters
 11Note: Methods with default parameters that call up from C++ cannot call
 12the overloaded C# methods, see DefaultParms method.
 13
 14Expected output if PrintDebug enabled:
 15------------ Start ------------
 16Base - Val(444.555)
 17Base - Ref(444.555)
 18Base - Ptr(444.555)
 19Base - FullyOverloaded(int 10)
 20Base - FullyOverloaded(bool 1)
 21Base - SemiOverloaded(int -678)
 22Base - SemiOverloaded(bool 1)
 23Base - DefaultParms(10, 2.2)
 24Base - DefaultParms(10, 1.1)
 25--------------------------------
 26Derived - Val(444.555)
 27Derived - Ref(444.555)
 28Derived - Ptr(444.555)
 29Derived - FullyOverloaded(int 10)
 30Derived - FullyOverloaded(bool 1)
 31Derived - SemiOverloaded(int -678)
 32Base - SemiOverloaded(bool 1)
 33Derived - DefaultParms(10, 2.2)
 34Derived - DefaultParms(10, 1.1)
 35--------------------------------
 36CSharpDerived - Val(444.555)
 37CSharpDerived - Ref(444.555)
 38CSharpDerived - Ptr(444.555)
 39CSharpDerived - FullyOverloaded(int 10)
 40CSharpDerived - FullyOverloaded(bool True)
 41CSharpDerived - SemiOverloaded(-678)
 42Base - SemiOverloaded(bool 1)
 43CSharpDerived - DefaultParms(10, 2.2)
 44CSharpDerived - DefaultParms(10, 1.1)
 45------------ Finish ------------
 46*/
 47
 48using System;
 49
 50namespace director_classesNamespace {
 51
 52public class runme
 53{
 54  static void Main() 
 55  {
 56    runme r = new runme();
 57    r.run();
 58  }
 59
 60  void run()
 61  {
 62    if (director_classes.PrintDebug) Console.WriteLine("------------ Start ------------ ");
 63
 64    Caller myCaller = new Caller();
 65
 66    // test C++ base class
 67    using (Base myBase = new Base(100.0))
 68    {
 69      makeCalls(myCaller, myBase);
 70    }
 71
 72    if (director_classes.PrintDebug) Console.WriteLine("--------------------------------");
 73
 74    // test vanilla C++ wrapped derived class
 75    using (Base myBase = new Derived(200.0))
 76    {
 77      makeCalls(myCaller, myBase);
 78    }
 79
 80    if (director_classes.PrintDebug) Console.WriteLine("--------------------------------");
 81
 82    // test director / C# derived class
 83    using (Base myBase = new CSharpDerived(300.0))
 84    {
 85      makeCalls(myCaller, myBase);
 86    }
 87
 88    if (director_classes.PrintDebug) Console.WriteLine("------------ Finish ------------ ");
 89  }
 90
 91  void makeCalls(Caller myCaller, Base myBase)
 92  {
 93    string NAMESPACE = "director_classesNamespace.";
 94    myCaller.set(myBase);
 95
 96    DoubleHolder dh = new DoubleHolder(444.555);
 97
 98    // Class pointer, reference and pass by value tests
 99    if (myCaller.ValCall(dh).val != dh.val) throw new Exception("failed");
100    if (myCaller.RefCall(dh).val != dh.val) throw new Exception("failed");
101    if (myCaller.PtrCall(dh).val != dh.val) throw new Exception("failed");
102
103    // Fully overloaded method test (all methods in base class are overloaded)
104    if (NAMESPACE + myCaller.FullyOverloadedCall(10) != myBase.GetType() + "::FullyOverloaded(int)") throw new Exception("failed");
105    if (NAMESPACE + myCaller.FullyOverloadedCall(true) != myBase.GetType() + "::FullyOverloaded(bool)") throw new Exception("failed");
106
107    // Semi overloaded method test (some methods in base class are overloaded)
108    if (NAMESPACE + myCaller.SemiOverloadedCall(-678) != myBase.GetType() + "::SemiOverloaded(int)") throw new Exception("failed");
109    if (myCaller.SemiOverloadedCall(true) != "Base" + "::SemiOverloaded(bool)") throw new Exception("failed");
110
111    // Default parameters methods test
112    if (NAMESPACE + myCaller.DefaultParmsCall(10, 2.2) != myBase.GetType() + "::DefaultParms(int, double)") throw new Exception("failed");
113    if (myBase.GetType() == typeof(CSharpDerived)) { // special handling for C# derived classes, there is no way to do this any other way
114      if (NAMESPACE + myCaller.DefaultParmsCall(10) != myBase.GetType() + "::DefaultParms(int, double)") throw new Exception("failed");
115    } else {
116      if (NAMESPACE + myCaller.DefaultParmsCall(10) != myBase.GetType() + "::DefaultParms(int)") throw new Exception("failed");
117    }
118
119    myCaller.reset();
120  }
121}
122
123public class CSharpDerived : Base
124{
125  public CSharpDerived(double dd)
126    : base(dd)
127  {
128  }
129
130  public override DoubleHolder Val(DoubleHolder x)
131  {
132    if (director_classes.PrintDebug) Console.WriteLine("CSharpDerived - Val({0})", x.val);
133    return x;
134  }
135  public override DoubleHolder Ref(DoubleHolder x)
136  {
137    if (director_classes.PrintDebug) Console.WriteLine("CSharpDerived - Ref({0})", x.val);
138    return x;
139  }
140  public override DoubleHolder Ptr(DoubleHolder x)
141  {
142    if (director_classes.PrintDebug) Console.WriteLine("CSharpDerived - Ptr({0})", x.val);
143    return x;
144  }
145  public override String FullyOverloaded(int x)
146  {
147    if (director_classes.PrintDebug) Console.WriteLine("CSharpDerived - FullyOverloaded(int {0})", x);
148    return "CSharpDerived::FullyOverloaded(int)";
149  }
150  public override String FullyOverloaded(bool x)
151  {
152    if (director_classes.PrintDebug) Console.WriteLine("CSharpDerived - FullyOverloaded(bool {0})", x);
153    return "CSharpDerived::FullyOverloaded(bool)";
154  }
155  // Note no SemiOverloaded(bool x) method
156  public override String SemiOverloaded(int x)
157  {
158    String ret = "CSharpDerived::SemiOverloaded(int)";
159    if (director_classes.PrintDebug) Console.WriteLine("CSharpDerived - SemiOverloaded({0})", x);
160    return ret;
161  }
162  public override String DefaultParms(int x, double y)
163  {
164    String ret = "CSharpDerived::DefaultParms(int, double)";
165    if (director_classes.PrintDebug) Console.WriteLine("CSharpDerived - DefaultParms({0}, {1})", x, y);
166    return ret;
167  }
168  // Note the following method can never be called from unmanaged code.
169  // It is here only for code that calls it directly from managed code.
170  // But should always be defined to ensure behaviour is consistent
171  // independent of where DefaultParsms is called from (managed or unmanaged code).
172  // Note this method can never be called from unmanaged code
173  public override String DefaultParms(int x)
174  {
175    if (director_classes.PrintDebug) Console.WriteLine("CSharpDerived - DefaultParms({0})", x);
176    return DefaultParms(x, 1.1/*use C++ default here*/);
177  }
178}
179
180}