diff options
author | Markus Mohrhard <markus.mohrhard@googlemail.com> | 2015-11-07 05:41:20 -0800 |
---|---|---|
committer | Markus Mohrhard <markus.mohrhard@googlemail.com> | 2015-11-07 06:06:09 -0800 |
commit | 5cb290d77a3539a492eac43202f27ecd150c99a0 (patch) | |
tree | 794065d4987c1b226209dd30b24830b80a47e6b2 | |
parent | c5813be8793da22ec25e4dfdf9d6dec43695cbeb (diff) |
add a flag for adding optional features
These features will switch the used C++ version from C++03 to C++11. We
are also going to use std::unique_ptr instead of std::auto_ptr for the
c++11 mode.
-rw-r--r-- | Makefile.am | 1 | ||||
-rw-r--r-- | configure.ac | 25 | ||||
-rw-r--r-- | examples/cppunittest/ExceptionTest.cpp | 4 | ||||
-rw-r--r-- | examples/cppunittest/HelperMacrosTest.cpp | 18 | ||||
-rw-r--r-- | examples/msvc6/HostApp/ExampleTestCase.cpp | 11 | ||||
-rw-r--r-- | examples/qt/ExampleTestCases.cpp | 5 | ||||
-rw-r--r-- | include/cppunit/portability/Makefile.am | 3 | ||||
-rw-r--r-- | include/cppunit/portability/SmartPtr.h | 6 | ||||
-rw-r--r-- | m4/ax_cxx_compile_stdcxx_11.m4 | 169 | ||||
-rw-r--r-- | src/cppunit/Protector.cpp | 5 |
10 files changed, 222 insertions, 25 deletions
diff --git a/Makefile.am b/Makefile.am index 62c0b33..6ecfcaa 100644 --- a/Makefile.am +++ b/Makefile.am @@ -39,6 +39,7 @@ m4internal = \ m4/ax_cxx_have_sstream.m4 \ m4/ax_cxx_namespaces.m4 \ m4/ax_cxx_rtti.m4 \ + m4/ax_cxx_compile_stdcxx_11.m4 \ m4/ax_prefix_config_h.m4 \ m4/bb_enable_doxygen.m4 \ m4/README diff --git a/configure.ac b/configure.ac index 42fec84..019be20 100644 --- a/configure.ac +++ b/configure.ac @@ -87,6 +87,22 @@ AC_SUBST(LT_AGE) # Check for cflags # ================ +# ================================================================ +# Check if optional features should be enabled (requires c++11) +# ================================================================ + +AC_ARG_ENABLE(optional_features, + [AS_HELP_STRING([--enable-optional-features], [Build with some optional features requiring C++11 support.])], + [enable_optional="$enableval"], + [enable_optional=yes] +) + +AS_IF([test "x$enable_optional" != "xno"], [ + AX_CXX_COMPILE_STDCXX_11(noext, mandatory) +]) + +AM_CONDITIONAL([WITH_OPTIONAL_FEATURES], [test "x$enable_optional" != "xno"]) + # ===== # Debug # ===== @@ -166,9 +182,10 @@ AC_OUTPUT AC_MSG_NOTICE([ ============================================================================== Build configuration: - debug: ${enable_debug} - docs: ${enable_doc} - werror: ${enable_werror} - typeinfo-name: ${use_typeinfo} + debug: ${enable_debug} + docs: ${enable_doc} + werror: ${enable_werror} + typeinfo-name: ${use_typeinfo} + optional-features: ${enable_optional} ============================================================================== ]) diff --git a/examples/cppunittest/ExceptionTest.cpp b/examples/cppunittest/ExceptionTest.cpp index 31dce64..856f561 100644 --- a/examples/cppunittest/ExceptionTest.cpp +++ b/examples/cppunittest/ExceptionTest.cpp @@ -1,7 +1,7 @@ #include "CoreSuite.h" #include "ExceptionTest.h" +#include <cppunit/portability/SmartPtr.h> #include <cppunit/Exception.h> -#include <memory> CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ExceptionTest, @@ -79,7 +79,7 @@ ExceptionTest::testClone() { CPPUNIT_NS::SourceLine sourceLine( "fileName.cpp", 123 ); CPPUNIT_NS::Exception e( CPPUNIT_NS::Message("message"), sourceLine ); - std::auto_ptr<CPPUNIT_NS::Exception> other( e.clone() ); + CppUnitSmartPtr<CPPUNIT_NS::Exception> other( e.clone() ); checkIsSame( e, *other.get() ); } diff --git a/examples/cppunittest/HelperMacrosTest.cpp b/examples/cppunittest/HelperMacrosTest.cpp index b88dec6..0e2a8b8 100644 --- a/examples/cppunittest/HelperMacrosTest.cpp +++ b/examples/cppunittest/HelperMacrosTest.cpp @@ -5,7 +5,7 @@ #include "MockTestCase.h" #include "SubclassedTestCase.h" #include <cppunit/TestResult.h> -#include <memory> +#include <cppunit/portability/SmartPtr.h> /* Note: - no unit test for CPPUNIT_TEST_SUITE_REGISTRATION... @@ -132,7 +132,7 @@ HelperMacrosTest::tearDown() void HelperMacrosTest::testNoSubclassing() { - std::auto_ptr<CPPUNIT_NS::TestSuite> suite( BaseTestCase::suite() ); + CppUnitSmartPtr<CPPUNIT_NS::TestSuite> suite( BaseTestCase::suite() ); CPPUNIT_ASSERT_EQUAL( 1, suite->countTestCases() ); m_testListener->setExpectedStartTestCall( 1 ); m_testListener->setExpectNoFailure(); @@ -145,7 +145,7 @@ HelperMacrosTest::testNoSubclassing() void HelperMacrosTest::testSubclassing() { - std::auto_ptr<CPPUNIT_NS::TestSuite> suite( SubclassedTestCase::suite() ); + CppUnitSmartPtr<CPPUNIT_NS::TestSuite> suite( SubclassedTestCase::suite() ); CPPUNIT_ASSERT_EQUAL( 2, suite->countTestCases() ); m_testListener->setExpectedStartTestCall( 2 ); m_testListener->setExpectedAddFailureCall( 1 ); @@ -158,7 +158,7 @@ HelperMacrosTest::testSubclassing() void HelperMacrosTest::testFail() { - std::auto_ptr<CPPUNIT_NS::TestSuite> suite( FailTestFixture::suite() ); + CppUnitSmartPtr<CPPUNIT_NS::TestSuite> suite( FailTestFixture::suite() ); m_testListener->setExpectedStartTestCall( 1 ); m_testListener->setExpectNoFailure(); @@ -170,7 +170,7 @@ HelperMacrosTest::testFail() void HelperMacrosTest::testFailToFail() { - std::auto_ptr<CPPUNIT_NS::TestSuite> suite( FailToFailTestFixture::suite() ); + CppUnitSmartPtr<CPPUNIT_NS::TestSuite> suite( FailToFailTestFixture::suite() ); m_testListener->setExpectedStartTestCall( 1 ); m_testListener->setExpectedAddFailureCall( 1 ); @@ -182,7 +182,7 @@ HelperMacrosTest::testFailToFail() void HelperMacrosTest::testException() { - std::auto_ptr<CPPUNIT_NS::TestSuite> suite( ExceptionTestFixture::suite() ); + CppUnitSmartPtr<CPPUNIT_NS::TestSuite> suite( ExceptionTestFixture::suite() ); m_testListener->setExpectedStartTestCall( 1 ); m_testListener->setExpectNoFailure(); @@ -194,7 +194,7 @@ HelperMacrosTest::testException() void HelperMacrosTest::testExceptionNotCaught() { - std::auto_ptr<CPPUNIT_NS::TestSuite> suite( ExceptionNotCaughtTestFixture::suite() ); + CppUnitSmartPtr<CPPUNIT_NS::TestSuite> suite( ExceptionNotCaughtTestFixture::suite() ); m_testListener->setExpectedStartTestCall( 1 ); m_testListener->setExpectedAddFailureCall( 1 ); @@ -206,7 +206,7 @@ HelperMacrosTest::testExceptionNotCaught() void HelperMacrosTest::testCustomTests() { - std::auto_ptr<CPPUNIT_NS::TestSuite> suite( CustomsTestTestFixture::suite() ); + CppUnitSmartPtr<CPPUNIT_NS::TestSuite> suite( CustomsTestTestFixture::suite() ); m_testListener->setExpectedStartTestCall( 2 ); m_testListener->setExpectedAddFailureCall( 1 ); @@ -218,7 +218,7 @@ HelperMacrosTest::testCustomTests() void HelperMacrosTest::testAddTest() { - std::auto_ptr<CPPUNIT_NS::TestSuite> suite( AddTestTestFixture::suite() ); + CppUnitSmartPtr<CPPUNIT_NS::TestSuite> suite( AddTestTestFixture::suite() ); m_testListener->setExpectedStartTestCall( 7 ); m_testListener->setExpectedAddFailureCall( 0 ); diff --git a/examples/msvc6/HostApp/ExampleTestCase.cpp b/examples/msvc6/HostApp/ExampleTestCase.cpp index ec13319..727fd30 100644 --- a/examples/msvc6/HostApp/ExampleTestCase.cpp +++ b/examples/msvc6/HostApp/ExampleTestCase.cpp @@ -1,4 +1,5 @@ #include "ExampleTestCase.h" +#include <cppunit/portability/SmartPtr.h> CPPUNIT_TEST_SUITE_REGISTRATION( ExampleTestCase ); @@ -31,14 +32,14 @@ void ExampleTestCase::testAdd () void ExampleTestCase::testEquals () { - std::auto_ptr<long> l1 (new long (12)); - std::auto_ptr<long> l2 (new long (12)); + CppUnitSmartPtr<long> l1 (new long (12)); + CppUnitSmartPtr<long> l2 (new long (12)); CPPUNIT_ASSERT_EQUAL (12, 12); CPPUNIT_ASSERT_EQUAL (12L, 12L); CPPUNIT_ASSERT_EQUAL (*l1, *l2); - CPPUNIT_ASSERT (12L == 12L); - CPPUNIT_ASSERT_EQUAL (12, 13); - CPPUNIT_ASSERT_DOUBLES_EQUAL (12.0, 11.99, 0.5); + CPPUNIT_ASSERT (12L == 12L); + CPPUNIT_ASSERT_EQUAL (12, 13); + CPPUNIT_ASSERT_DOUBLES_EQUAL (12.0, 11.99, 0.5); } diff --git a/examples/qt/ExampleTestCases.cpp b/examples/qt/ExampleTestCases.cpp index edd67b0..03f1593 100644 --- a/examples/qt/ExampleTestCases.cpp +++ b/examples/qt/ExampleTestCases.cpp @@ -1,4 +1,5 @@ #include "ExampleTestCases.h" +#include <cppunit/portability/SmartPtr.h> CPPUNIT_TEST_SUITE_REGISTRATION( ExampleTestCases ); @@ -28,8 +29,8 @@ void ExampleTestCases::testAdd () void ExampleTestCases::testEquals () { - std::auto_ptr<long> l1 (new long (12)); - std::auto_ptr<long> l2 (new long (12)); + CppUnitSmartPtr<long> l1 (new long (12)); + CppUnitSmartPtr<long> l2 (new long (12)); CPPUNIT_ASSERT_EQUAL (12, 12); CPPUNIT_ASSERT_EQUAL (12L, 12L); diff --git a/include/cppunit/portability/Makefile.am b/include/cppunit/portability/Makefile.am index 73a0410..8311d00 100644 --- a/include/cppunit/portability/Makefile.am +++ b/include/cppunit/portability/Makefile.am @@ -7,4 +7,5 @@ libcppunitinclude_HEADERS = \ CppUnitStack.h \ CppUnitVector.h \ FloatingPoint.h \ - Stream.h + Stream.h \ + SmartPtr.h diff --git a/include/cppunit/portability/SmartPtr.h b/include/cppunit/portability/SmartPtr.h new file mode 100644 index 0000000..76e2183 --- /dev/null +++ b/include/cppunit/portability/SmartPtr.h @@ -0,0 +1,6 @@ +#ifndef CPPUNIT_PORTABILITY_CPPUNITSMARTPTR_H +#define CPPUNIT_PORTABILITY_CPPUNITSMARTPTR_H + +#define CppUnitSmartPtr std::unique_ptr + +#endif // CPPUNIT_PORTABILITY_CPPUNITDEQUE_H diff --git a/m4/ax_cxx_compile_stdcxx_11.m4 b/m4/ax_cxx_compile_stdcxx_11.m4 new file mode 100644 index 0000000..be8d437 --- /dev/null +++ b/m4/ax_cxx_compile_stdcxx_11.m4 @@ -0,0 +1,169 @@ +# ============================================================================ +# http://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx_11.html +# ============================================================================ +# +# SYNOPSIS +# +# AX_CXX_COMPILE_STDCXX_11([ext|noext],[mandatory|optional]) +# +# DESCRIPTION +# +# Check for baseline language coverage in the compiler for the C++11 +# standard; if necessary, add switches to CXXFLAGS to enable support. +# +# The first argument, if specified, indicates whether you insist on an +# extended mode (e.g. -std=gnu++11) or a strict conformance mode (e.g. +# -std=c++11). If neither is specified, you get whatever works, with +# preference for an extended mode. +# +# The second argument, if specified 'mandatory' or if left unspecified, +# indicates that baseline C++11 support is required and that the macro +# should error out if no mode with that support is found. If specified +# 'optional', then configuration proceeds regardless, after defining +# HAVE_CXX11 if and only if a supporting mode is found. +# +# LICENSE +# +# Copyright (c) 2008 Benjamin Kosnik <bkoz@redhat.com> +# Copyright (c) 2012 Zack Weinberg <zackw@panix.com> +# Copyright (c) 2013 Roy Stogner <roystgnr@ices.utexas.edu> +# Copyright (c) 2014, 2015 Google Inc.; contributed by Alexey Sokolov <sokolov@google.com> +# +# Copying and distribution of this file, with or without modification, are +# permitted in any medium without royalty provided the copyright notice +# and this notice are preserved. This file is offered as-is, without any +# warranty. + +#serial 10 + +m4_define([_AX_CXX_COMPILE_STDCXX_11_testbody], [[ + template <typename T> + struct check + { + static_assert(sizeof(int) <= sizeof(T), "not big enough"); + }; + + struct Base { + virtual void f() {} + }; + struct Child : public Base { + virtual void f() override {} + }; + + typedef check<check<bool>> right_angle_brackets; + + int a; + decltype(a) b; + + typedef check<int> check_type; + check_type c; + check_type&& cr = static_cast<check_type&&>(c); + + auto d = a; + auto l = [](){}; + // Prevent Clang error: unused variable 'l' [-Werror,-Wunused-variable] + struct use_l { use_l() { l(); } }; + + // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae + // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function because of this + namespace test_template_alias_sfinae { + struct foo {}; + + template<typename T> + using member = typename T::member_type; + + template<typename T> + void func(...) {} + + template<typename T> + void func(member<T>*) {} + + void test(); + + void test() { + func<foo>(0); + } + } +]]) + +AC_DEFUN([AX_CXX_COMPILE_STDCXX_11], [dnl + m4_if([$1], [], [], + [$1], [ext], [], + [$1], [noext], [], + [m4_fatal([invalid argument `$1' to AX_CXX_COMPILE_STDCXX_11])])dnl + m4_if([$2], [], [ax_cxx_compile_cxx11_required=true], + [$2], [mandatory], [ax_cxx_compile_cxx11_required=true], + [$2], [optional], [ax_cxx_compile_cxx11_required=false], + [m4_fatal([invalid second argument `$2' to AX_CXX_COMPILE_STDCXX_11])]) + AC_LANG_PUSH([C++])dnl + ac_success=no + AC_CACHE_CHECK(whether $CXX supports C++11 features by default, + ax_cv_cxx_compile_cxx11, + [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_11_testbody])], + [ax_cv_cxx_compile_cxx11=yes], + [ax_cv_cxx_compile_cxx11=no])]) + if test x$ax_cv_cxx_compile_cxx11 = xyes; then + ac_success=yes + fi + + m4_if([$1], [noext], [], [dnl + if test x$ac_success = xno; then + for switch in -std=gnu++11 -std=gnu++0x; do + cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx11_$switch]) + AC_CACHE_CHECK(whether $CXX supports C++11 features with $switch, + $cachevar, + [ac_save_CXXFLAGS="$CXXFLAGS" + CXXFLAGS="$CXXFLAGS $switch" + AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_11_testbody])], + [eval $cachevar=yes], + [eval $cachevar=no]) + CXXFLAGS="$ac_save_CXXFLAGS"]) + if eval test x\$$cachevar = xyes; then + CXXFLAGS="$CXXFLAGS $switch" + ac_success=yes + break + fi + done + fi]) + + m4_if([$1], [ext], [], [dnl + if test x$ac_success = xno; then + for switch in -std=c++11 -std=c++0x; do + cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx11_$switch]) + AC_CACHE_CHECK(whether $CXX supports C++11 features with $switch, + $cachevar, + [ac_save_CXXFLAGS="$CXXFLAGS" + CXXFLAGS="$CXXFLAGS $switch" + AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_11_testbody])], + [eval $cachevar=yes], + [eval $cachevar=no]) + CXXFLAGS="$ac_save_CXXFLAGS"]) + if eval test x\$$cachevar = xyes; then + CXXFLAGS="$CXXFLAGS $switch" + ac_success=yes + break + fi + done + fi]) + AC_LANG_POP([C++]) + if test x$ax_cxx_compile_cxx11_required = xtrue; then + if test x$ac_success = xno; then + AC_MSG_ERROR([*** A compiler with support for C++11 language features is required.]) + else + HAVE_CXX11=1 + AC_DEFINE(HAVE_CXX11,1, + [define if the compiler supports basic C++11 syntax]) + fi + else + if test x$ac_success = xno; then + HAVE_CXX11=0 + AC_MSG_NOTICE([No compiler with C++11 support was found]) + else + HAVE_CXX11=1 + AC_DEFINE(HAVE_CXX11,1, + [define if the compiler supports basic C++11 syntax]) + fi + + AC_SUBST(HAVE_CXX11) + fi +]) diff --git a/src/cppunit/Protector.cpp b/src/cppunit/Protector.cpp index 5c171ec..492bec1 100644 --- a/src/cppunit/Protector.cpp +++ b/src/cppunit/Protector.cpp @@ -2,6 +2,7 @@ #include <cppunit/Message.h> #include <cppunit/Protector.h> #include <cppunit/TestResult.h> +#include <cppunit/portability/SmartPtr.h> #include "ProtectorContext.h" #include <memory> @@ -21,7 +22,7 @@ void Protector::reportError( const ProtectorContext &context, const Exception &error ) const { - std::auto_ptr<Exception> actualError( error.clone() ); + CppUnitSmartPtr<Exception> actualError( error.clone() ); actualError->setMessage( actualMessage( actualError->message(), context ) ); context.m_result->addError( context.m_test, actualError.release() ); @@ -42,7 +43,7 @@ void Protector::reportFailure( const ProtectorContext &context, const Exception &failure ) const { - std::auto_ptr<Exception> actualFailure( failure.clone() ); + CppUnitSmartPtr<Exception> actualFailure( failure.clone() ); actualFailure->setMessage( actualMessage( actualFailure->message(), context ) ); context.m_result->addFailure( context.m_test, actualFailure.release() ); |