From 01ce992f5d8ca20beffbc91be3fffe412277c5eb Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Fri, 14 Mar 2014 00:01:26 +0000 Subject: [PATCH] C++11 result_of testcase --- Examples/test-suite/common.mk | 2 +- Examples/test-suite/cpp11_result_of.i | 41 ++++++++++++++++++- .../java/cpp11_result_of_runme.java | 24 +++++++++++ .../python/cpp11_result_of_runme.py | 10 ++++- 4 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 Examples/test-suite/java/cpp11_result_of_runme.java diff --git a/Examples/test-suite/common.mk b/Examples/test-suite/common.mk index cb792b8b5..e3543a78a 100644 --- a/Examples/test-suite/common.mk +++ b/Examples/test-suite/common.mk @@ -511,6 +511,7 @@ CPP11_TEST_CASES = \ cpp11_noexcept \ cpp11_null_pointer_constant \ cpp11_raw_string_literals \ + cpp11_result_of \ cpp11_rvalue_reference \ cpp11_rvalue_reference2 \ cpp11_rvalue_reference3 \ @@ -527,7 +528,6 @@ CPP11_TEST_CASES = \ # Broken C++11 test cases. CPP11_TEST_BROKEN = \ # cpp11_hash_tables \ # not fully implemented yet -# cpp11_result_of \ # SWIG does not support # cpp11_strongly_typed_enumerations \ # SWIG not quite getting this right yet in all langs # cpp11_variadic_templates \ # Broken for some languages (such as Java) # cpp11_reference_wrapper \ # No typemaps diff --git a/Examples/test-suite/cpp11_result_of.i b/Examples/test-suite/cpp11_result_of.i index 6d98ec0c0..8a26c5f24 100644 --- a/Examples/test-suite/cpp11_result_of.i +++ b/Examples/test-suite/cpp11_result_of.i @@ -4,7 +4,21 @@ %inline %{ #include -#include +typedef double(*fn_ptr)(double); +%} + +namespace std { + // Forward declaration of result_of + template struct result_of; + // Add in the required partial specialization of result_of + template<> struct result_of< fn_ptr(double) > { + typedef double type; + }; +} + +%template() std::result_of< fn_ptr(double) >; + +%inline %{ double square(double x) { return (x * x); @@ -14,7 +28,30 @@ template typename std::result_of::type test_result_impl(Fun fun, Arg arg) { return fun(arg); } + +std::result_of< fn_ptr(double) >::type test_result_alternative1(double(*fun)(double), double arg) { + return fun(arg); +} %} -%template(test_result) test_result_impl; +%{ +// Another alternative approach using decltype (not very SWIG friendly) +std::result_of< decltype(square)&(double) >::type test_result_alternative2(double(*fun)(double), double arg) { + return fun(arg); +} +%} + +%inline %{ +#include + +void cpp_testing() { + std::cout << "result: " << test_result_impl(square, 3) << std::endl; + std::cout << "result: " << test_result_impl(square, 4) << std::endl; + std::cout << "result: " << test_result_impl< fn_ptr, double >(square, 5) << std::endl; + std::cout << "result: " << test_result_alternative1(square, 6) << std::endl; + std::cout << "result: " << test_result_alternative2(square, 7) << std::endl; +} +%} + +%template(test_result) test_result_impl< fn_ptr, double>; %constant double (*SQUARE)(double) = square; diff --git a/Examples/test-suite/java/cpp11_result_of_runme.java b/Examples/test-suite/java/cpp11_result_of_runme.java new file mode 100644 index 000000000..6a492c464 --- /dev/null +++ b/Examples/test-suite/java/cpp11_result_of_runme.java @@ -0,0 +1,24 @@ +import cpp11_result_of.*; + +public class cpp11_result_of_runme { + + static { + try { + System.loadLibrary("cpp11_result_of"); + } 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[]) + { + double result = cpp11_result_of.test_result(cpp11_result_ofConstants.SQUARE, 3.0); + if (result != 9.0) + throw new RuntimeException("test_result(square, 3.0) is not 9.0. Got: " + Double.toString(result)); + + result = cpp11_result_of.test_result_alternative1(cpp11_result_ofConstants.SQUARE, 3.0); + if (result != 9.0) + throw new RuntimeException("test_result_alternative1(square, 3.0) is not 9.0. Got: " + Double.toString(result)); + } +} diff --git a/Examples/test-suite/python/cpp11_result_of_runme.py b/Examples/test-suite/python/cpp11_result_of_runme.py index a97dd7ccc..4dc39fcdf 100644 --- a/Examples/test-suite/python/cpp11_result_of_runme.py +++ b/Examples/test-suite/python/cpp11_result_of_runme.py @@ -1,3 +1,9 @@ import cpp11_result_of -if cpp11_result_of.test_result(cpp11_result_of.square, 3.0) != 9.0: - raise RuntimeError, "test_result(square, 3.0) is not 9.0." + +result = cpp11_result_of.test_result(cpp11_result_of.SQUARE, 3.0) +if result != 9.0: + raise RuntimeError, "test_result(square, 3.0) is not 9.0. Got: " + str(result) + +result = cpp11_result_of.test_result_alternative1(cpp11_result_of.SQUARE, 3.0) +if result != 9.0: + raise RuntimeError, "test_result_alternative1(square, 3.0) is not 9.0. Got: " + str(result)