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 203 204 205 206 207
|
/*
This test demonstrates director classes when the types are classes.
Shown are virtual function calls which use classes passed by:
- Value
- Reference
- Pointer
as both parameters and return values.
The test also demonstrates directors used with:
- method overloading
- default parameters
Note: Methods with default parameters that call up from C++ cannot call
the overloaded Java methods, see DefaultParms method.
Expected output if PrintDebug enabled:
------------ Start ------------
Base - Val(444.555)
Base - Ref(444.555)
Base - Ptr(444.555)
Base - FullyOverloaded(int 10)
Base - FullyOverloaded(bool 1)
Base - SemiOverloaded(int -678)
Base - SemiOverloaded(bool 1)
Base - DefaultParms(10, 2.2)
Base - DefaultParms(10, 1.1)
--------------------------------
Derived - Val(444.555)
Derived - Ref(444.555)
Derived - Ptr(444.555)
Derived - FullyOverloaded(int 10)
Derived - FullyOverloaded(bool 1)
Derived - SemiOverloaded(int -678)
Base - SemiOverloaded(bool 1)
Derived - DefaultParms(10, 2.2)
Derived - DefaultParms(10, 1.1)
--------------------------------
JavaDerived - Val(444.555)
JavaDerived - Ref(444.555)
JavaDerived - Ptr(444.555)
JavaDerived - FullyOverloaded(int 10)
JavaDerived - FullyOverloaded(bool True)
JavaDerived - SemiOverloaded(-678)
Base - SemiOverloaded(bool 1)
JavaDerived - DefaultParms(10, 2.2)
JavaDerived - DefaultParms(10, 1.1)
------------ Finish ------------
*/
import director_classes.*;
public class director_classes_runme {
static {
try {
System.loadLibrary("director_classes");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
System.exit(1);
}
}
public static void main(String argv[]) throws Throwable
{
director_classes_runme r = new director_classes_runme();
r.run();
}
void run()
{
if (director_classes.getPrintDebug()) System.out.println("------------ Start ------------ ");
Caller myCaller = new Caller();
// test C++ base class
{
Base myBase = new Base(100.0);
makeCalls(myCaller, myBase);
myBase.delete();
}
if (director_classes.getPrintDebug()) System.out.println("--------------------------------");
// test vanilla C++ wrapped derived class
{
Base myBase = new Derived(200.0);
makeCalls(myCaller, myBase);
myBase.delete();
}
if (director_classes.getPrintDebug()) System.out.println("--------------------------------");
// test director / Java derived class
{
Base myBase = new JavaDerived(300.0);
makeCalls(myCaller, myBase);
myBase.delete();
}
if (director_classes.getPrintDebug()) System.out.println("------------ Finish ------------ ");
}
void makeCalls(Caller myCaller, Base myBase)
{
String baseSimpleName = getSimpleName(myBase.getClass());
myCaller.set(myBase);
DoubleHolder dh = new DoubleHolder(444.555);
// Class pointer, reference and pass by value tests
if (myCaller.ValCall(dh).getVal() != dh.getVal()) throw new RuntimeException("failed");
if (myCaller.RefCall(dh).getVal() != dh.getVal()) throw new RuntimeException("failed");
if (myCaller.PtrCall(dh).getVal() != dh.getVal()) throw new RuntimeException("failed");
// Fully overloaded method test (all methods in base class are overloaded)
if (!myCaller.FullyOverloadedCall(10).equals(baseSimpleName + "::FullyOverloaded(int)")) {
System.out.println(myCaller.FullyOverloadedCall(10) + "----" + (baseSimpleName + "::FullyOverloaded(int)"));
throw new RuntimeException("failed");
}
if (!myCaller.FullyOverloadedCall(true).equals(baseSimpleName + "::FullyOverloaded(bool)")) throw new RuntimeException("failed");
// Semi overloaded method test (some methods in base class are overloaded)
if (!myCaller.SemiOverloadedCall(-678).equals(baseSimpleName + "::SemiOverloaded(int)")) throw new RuntimeException("failed");
if (!myCaller.SemiOverloadedCall(true).equals("Base" + "::SemiOverloaded(bool)")) throw new RuntimeException("failed");
// Default parameters methods test
if (!(myCaller.DefaultParmsCall(10, 2.2)).equals(baseSimpleName + "::DefaultParms(int, double)")) throw new RuntimeException("failed");
if (myBase instanceof JavaDerived) { // special handling for Java derived classes, there is no way to do this any other way
if (!myCaller.DefaultParmsCall(10).equals(baseSimpleName + "::DefaultParms(int, double)")) throw new RuntimeException("failed");
} else {
if (!myCaller.DefaultParmsCall(10).equals(baseSimpleName + "::DefaultParms(int)")) throw new RuntimeException("failed");
}
myCaller.reset();
}
// Same as Class.getSimpleName() which is not present in all jdks
static String getSimpleName(Class klass) {
String fullName = klass.getName();
Package packag = klass.getPackage();
String simpleName = null;
if (packag != null)
simpleName = fullName.replaceAll(packag.getName() + "\\.", "");
else
simpleName = fullName;
return simpleName;
}
}
class JavaDerived extends Base
{
public JavaDerived(double dd)
{
super(dd);
}
public DoubleHolder Val(DoubleHolder x)
{
if (director_classes.getPrintDebug()) System.out.println("JavaDerived - Val(" + x.getVal() + ")");
return x;
}
public DoubleHolder Ref(DoubleHolder x)
{
if (director_classes.getPrintDebug()) System.out.println("JavaDerived - Ref(" + x.getVal() + ")");
return x;
}
public DoubleHolder Ptr(DoubleHolder x)
{
if (director_classes.getPrintDebug()) System.out.println("JavaDerived - Ptr(" + x.getVal() + ")");
return x;
}
public String FullyOverloaded(int x)
{
if (director_classes.getPrintDebug()) System.out.println("JavaDerived - FullyOverloaded(int " + x + ")");
return "JavaDerived::FullyOverloaded(int)";
}
public String FullyOverloaded(boolean x)
{
if (director_classes.getPrintDebug()) System.out.println("JavaDerived - FullyOverloaded(bool " + x + ")");
return "JavaDerived::FullyOverloaded(bool)";
}
// Note no SemiOverloaded(bool x) method
public String SemiOverloaded(int x)
{
String ret = "JavaDerived::SemiOverloaded(int)";
if (director_classes.getPrintDebug()) System.out.println("JavaDerived - SemiOverloaded(" + x + ")");
return ret;
}
public String DefaultParms(int x, double y)
{
String ret = "JavaDerived::DefaultParms(int, double)";
if (director_classes.getPrintDebug()) System.out.println("JavaDerived - DefaultParms(" + x + ", " + y + ")");
return ret;
}
// Note the following method can never be called from unmanaged code.
// It is here only for code that calls it directly from managed code.
// But should always be defined to ensure behaviour is consistent
// independent of where DefaultParsms is called from (managed or unmanaged code).
// Note this method can never be called from unmanaged code
public String DefaultParms(int x)
{
if (director_classes.getPrintDebug()) System.out.println("JavaDerived - DefaultParms(" + x + ")");
return DefaultParms(x, 1.1/*use C++ default here*/);
}
}
|