From 323f841d933c5a11b592767fbadf76a0dcb2b3c2 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Thu, 13 Sep 2012 20:15:03 +0000 Subject: [PATCH] Add a testcase for the Curiously Recurring Template Pattern - CRTP git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@13839 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Examples/test-suite/common.mk | 1 + .../curiously_recurring_template_pattern.i | 55 +++++++++++++++++++ ...usly_recurring_template_pattern_runme.java | 29 ++++++++++ 3 files changed, 85 insertions(+) create mode 100644 Examples/test-suite/curiously_recurring_template_pattern.i create mode 100644 Examples/test-suite/java/curiously_recurring_template_pattern_runme.java diff --git a/Examples/test-suite/common.mk b/Examples/test-suite/common.mk index f6a8056e5..eaef4980b 100644 --- a/Examples/test-suite/common.mk +++ b/Examples/test-suite/common.mk @@ -152,6 +152,7 @@ CPP_TEST_CASES += \ cpp_nodefault \ cpp_static \ cpp_typedef \ + curiously_recurring_template_pattern \ default_args \ default_arg_values \ default_constructor \ diff --git a/Examples/test-suite/curiously_recurring_template_pattern.i b/Examples/test-suite/curiously_recurring_template_pattern.i new file mode 100644 index 000000000..96b30fc3d --- /dev/null +++ b/Examples/test-suite/curiously_recurring_template_pattern.i @@ -0,0 +1,55 @@ +%module curiously_recurring_template_pattern + +// Test Curiously Recurring Template Pattern - CRTP + +%inline %{ +template class Base { + int base1Param; + int base2Param; +public: + Base() : base1Param(0) {} + int getBase1Param() { + return base1Param; + } + T& setBase1Param(int value) { + base1Param = value; + return *static_cast(this); + } + int getBase2Param() { + return base2Param; + } + T& setBase2Param(int value) { + base2Param = value; + return *static_cast(this); + } + virtual ~Base() {} +}; +%} + +%template(basederived) Base; + +%inline %{ +class Derived : public Base { + int derived1Param; + int derived2Param; +public: + Derived() : derived1Param(0), derived2Param(0) {} + int getDerived1Param() { + return derived1Param; + } + Derived& setDerived1Param(int value) { + derived1Param = value; + return *this; + } + int getDerived2Param() { + return derived2Param; + } + Derived& setDerived2Param(int value) { + derived2Param = value; + return *this; + } +}; +%} + + + diff --git a/Examples/test-suite/java/curiously_recurring_template_pattern_runme.java b/Examples/test-suite/java/curiously_recurring_template_pattern_runme.java new file mode 100644 index 000000000..ac7e78ba5 --- /dev/null +++ b/Examples/test-suite/java/curiously_recurring_template_pattern_runme.java @@ -0,0 +1,29 @@ + +import curiously_recurring_template_pattern.*; + +public class curiously_recurring_template_pattern_runme { + + static { + try { + System.loadLibrary("curiously_recurring_template_pattern"); + } 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[]) { + Derived d = new Derived(); + d.setBase1Param(1).setDerived1Param(10).setDerived2Param(20).setBase2Param(2); + + if (d.getBase1Param() != 1) + throw new RuntimeException("fail"); + if (d.getDerived1Param() != 10) + throw new RuntimeException("fail"); + if (d.getBase2Param() != 2) + throw new RuntimeException("fail"); + if (d.getDerived2Param() != 20) + throw new RuntimeException("fail"); + } +} +