diff --git a/SWIG/Examples/test-suite/common.mk b/SWIG/Examples/test-suite/common.mk index 43e07192a..04c94a542 100644 --- a/SWIG/Examples/test-suite/common.mk +++ b/SWIG/Examples/test-suite/common.mk @@ -119,6 +119,7 @@ CPP_TEST_CASES += \ director_abstract \ director_basic \ director_classes \ + director_classic \ director_constructor \ director_detect \ director_default \ diff --git a/SWIG/Examples/test-suite/csharp/director_classic_runme.cs b/SWIG/Examples/test-suite/csharp/director_classic_runme.cs new file mode 100755 index 000000000..d6c47a8cd --- /dev/null +++ b/SWIG/Examples/test-suite/csharp/director_classic_runme.cs @@ -0,0 +1,116 @@ +using System; + +namespace director_classicNamespace { + +public class runme +{ + static void Main() + { + { + Person person = new Person(); + check(person, "Person"); + person.Dispose(); + } + { + Person person = new Child(); + check(person, "Child"); + person.Dispose(); + } + { + Person person = new GrandChild(); + check(person, "GrandChild"); + person.Dispose(); + } + { + Person person = new TargetLangPerson(); + check(person, "TargetLangPerson"); + person.Dispose(); + } + { + Person person = new TargetLangChild(); + check(person, "TargetLangChild"); + person.Dispose(); + } + { + Person person = new TargetLangGrandChild(); + check(person, "TargetLangGrandChild"); + person.Dispose(); + } + } + + static void check(Person person, String expected) { + String ret; + // Normal target language polymorphic call + ret = person.id(); + if (debug) + Console.WriteLine(ret); + if (ret != expected) + throw new Exception("Failed. Received: " + ret + " Expected: " + expected); + + // Polymorphic call from C++ + Caller caller = new Caller(); + caller.setCallback(person); + ret = caller.call(); + if (debug) + Console.WriteLine(ret); + if (ret != expected) + throw new Exception("Failed. Received: " + ret + " Expected: " + expected); + + // Polymorphic call of object created in target language and passed to C++ and back again + Person baseclass = caller.baseClass(); + ret = baseclass.id(); + if (debug) + Console.WriteLine(ret); + if (ret != expected) + throw new Exception("Failed. Received: " + ret + " Expected: " + expected); + + caller.resetCallback(); + if (debug) + Console.WriteLine("----------------------------------------"); + } + static bool debug = false; +} + +public class TargetLangPerson : Person +{ + public TargetLangPerson() + : base() + { + } + + public override String id() + { + String identifier = "TargetLangPerson"; + return identifier; + } +} + +public class TargetLangChild : Child +{ + public TargetLangChild() + : base() + { + } + + public override String id() + { + String identifier = "TargetLangChild"; + return identifier; + } +} + +public class TargetLangGrandChild : GrandChild +{ + public TargetLangGrandChild() + : base() + { + } + + public override String id() + { + String identifier = "TargetLangGrandChild"; + return identifier; + } +} + +} diff --git a/SWIG/Examples/test-suite/director_classic.i b/SWIG/Examples/test-suite/director_classic.i new file mode 100755 index 000000000..25aecaf8a --- /dev/null +++ b/SWIG/Examples/test-suite/director_classic.i @@ -0,0 +1,40 @@ +%module(directors="1") director_classic + +%include "std_string.i" + +%feature("director"); + +%inline %{ + +#include +#include +#include + +struct Person { + virtual std::string id() { return "Person"; } + virtual ~Person() {} +}; + +struct Child : Person { + virtual std::string id() { return "Child"; } +}; + +struct GrandChild : Child { + virtual std::string id() { return "GrandChild"; } +}; + +class Caller { +private: + Person *_callback; +public: + Caller(): _callback(0) {} + ~Caller() { delCallback(); } + void delCallback() { delete _callback; _callback = 0; } + void setCallback(Person *cb) { delCallback(); _callback = cb; } + void resetCallback() { _callback = 0; } + std::string call() { if (_callback) return _callback->id(); else return "oops"; } + Person* baseClass() { return _callback; } +}; + +%} + diff --git a/SWIG/Examples/test-suite/java/director_classic_runme.java b/SWIG/Examples/test-suite/java/director_classic_runme.java new file mode 100755 index 000000000..a79fbb0b1 --- /dev/null +++ b/SWIG/Examples/test-suite/java/director_classic_runme.java @@ -0,0 +1,121 @@ +import director_classic.*; + +public class director_classic_runme { + static { + try { + System.loadLibrary("director_classic"); + } 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[]) + { + { + Person person = new Person(); + check(person, "Person"); + person.delete(); + } + { + Person person = new Child(); + check(person, "Child"); + person.delete(); + } + { + Person person = new GrandChild(); + check(person, "GrandChild"); + person.delete(); + } + { + Person person = new TargetLangPerson(); + check(person, "TargetLangPerson"); + person.delete(); + } + { + Person person = new TargetLangChild(); + check(person, "TargetLangChild"); + person.delete(); + } + { + Person person = new TargetLangGrandChild(); + check(person, "TargetLangGrandChild"); + person.delete(); + } + } + + static void check(Person person, String expected) { + String ret; + // Normal target language polymorphic call + ret = person.id(); + if (debug) + System.out.println(ret); + if (!ret.equals(expected)) + throw new RuntimeException("Failed. Received: " + ret + " Expected: " + expected); + + // Polymorphic call from C++ + Caller caller = new Caller(); + caller.setCallback(person); + ret = caller.call(); + if (debug) + System.out.println(ret); + if (!ret.equals(expected)) + throw new RuntimeException("Failed. Received: " + ret + " Expected: " + expected); + + // Polymorphic call of object created in target language and passed to C++ and back again + Person baseclass = caller.baseClass(); + ret = baseclass.id(); + if (debug) + System.out.println(ret); + if (!ret.equals(expected)) + throw new RuntimeException("Failed. Received: " + ret + " Expected: " + expected); + + caller.resetCallback(); + if (debug) + System.out.println("----------------------------------------"); + } + static boolean debug = false; +} + +class TargetLangPerson extends Person +{ + public TargetLangPerson() + { + super(); + } + + public String id() + { + String identifier = "TargetLangPerson"; + return identifier; + } +} + +class TargetLangChild extends Child +{ + public TargetLangChild() + { + super(); + } + + public String id() + { + String identifier = "TargetLangChild"; + return identifier; + } +} + +class TargetLangGrandChild extends GrandChild +{ + public TargetLangGrandChild() + { + super(); + } + + public String id() + { + String identifier = "TargetLangGrandChild"; + return identifier; + } +} + diff --git a/SWIG/Examples/test-suite/python/director_classic_runme.py b/SWIG/Examples/test-suite/python/director_classic_runme.py new file mode 100644 index 000000000..4eb186cd4 --- /dev/null +++ b/SWIG/Examples/test-suite/python/director_classic_runme.py @@ -0,0 +1,81 @@ +from director_classic import * + +class TargetLangPerson(Person): + def __init__(self): + Person.__init__(self) + def id(self): + identifier = "TargetLangPerson" + return identifier + +class TargetLangChild(Child): + def __init__(self): + Child.__init__(self) + def id(self): + identifier = "TargetLangChild" + return identifier + +class TargetLangGrandChild(GrandChild): + def __init__(self): + GrandChild.__init__(self) + def id(self): + identifier = "TargetLangGrandChild" + return identifier + +def check(person, expected): + + debug = 0 + # Normal target language polymorphic call + ret = person.id() + if (debug): + print(ret) + if (ret != expected): + raise ("Failed. Received: " + ret + " Expected: " + expected) + + # Polymorphic call from C++ + caller = Caller() + caller.setCallback(person) + ret = caller.call() + if (debug): + print(ret) + if (ret != expected): + raise ("Failed. Received: " + ret + " Expected: " + expected) + + # Polymorphic call of object created in target language and passed to C++ and back again + baseclass = caller.baseClass() + ret = baseclass.id() + if (debug): + print(ret) + if (ret != expected): + raise ("Failed. Received: " + ret + " Expected: " + expected) + + caller.resetCallback() + if (debug): + print("----------------------------------------") + + + +person = Person(); +check(person, "Person"); +del person + +person = Child(); +check(person, "Child"); +del person + +person = GrandChild(); +check(person, "GrandChild"); +del person + +person = TargetLangPerson(); +check(person, "TargetLangPerson"); +del person + +person = TargetLangChild(); +check(person, "TargetLangChild"); +del person + +person = TargetLangGrandChild(); +check(person, "TargetLangGrandChild"); +del person + +