diff --git a/cmake/modules/CheckCXX11Features.cmake b/cmake/modules/CheckCXX11Features.cmake index 8082137ada14102329c9ed89f1f40c4205be8297..78d749c0b290283b5bad30a5f7b405338f425a13 100644 --- a/cmake/modules/CheckCXX11Features.cmake +++ b/cmake/modules/CheckCXX11Features.cmake @@ -9,6 +9,7 @@ # HAS_ATTRIBUTE_DEPRECATED_MSG True if attribute deprecated("msg") is supported # HAVE_CONSTEXPR True if constexpr is supported # HAVE_KEYWORD_FINAL True if final is supported. +# HAVE_RANGE_BASED_FOR True if range-based for is supported and working. include(CMakePushCheckState) cmake_push_check_state() @@ -166,4 +167,15 @@ check_cxx_source_compiles(" " HAVE_KEYWORD_FINAL ) +# range-based for +check_cxx_source_compiles(" + int main(void) + { + int arr[3]; + for(int &val : arr) + val = 0; + } +" HAVE_RANGE_BASED_FOR +) + cmake_pop_check_state() diff --git a/dune/common/CMakeLists.txt b/dune/common/CMakeLists.txt index 8911c93facc37afea238393cbe752f3cfc98c850..dd5081c5a60e76c7d014bc28c7d632a8a66f9e69 100644 --- a/dune/common/CMakeLists.txt +++ b/dune/common/CMakeLists.txt @@ -62,6 +62,7 @@ install(FILES interfaces.hh ios_state.hh iteratorfacades.hh + iteratorrange.hh lcm.hh lru.hh mallocallocator.hh diff --git a/dune/common/Makefile.am b/dune/common/Makefile.am index b52a1dec2e6847be030579984495dbde43be9164..22934b23196e7625fc55197246cc0eadce0026c0 100644 --- a/dune/common/Makefile.am +++ b/dune/common/Makefile.am @@ -58,6 +58,7 @@ commoninclude_HEADERS = \ interfaces.hh \ ios_state.hh \ iteratorfacades.hh \ + iteratorrange.hh \ lcm.hh \ lru.hh \ mallocallocator.hh \ diff --git a/dune/common/iteratorrange.hh b/dune/common/iteratorrange.hh new file mode 100644 index 0000000000000000000000000000000000000000..96f91e94c68a844fb5d013610044d5acbe8c54f6 --- /dev/null +++ b/dune/common/iteratorrange.hh @@ -0,0 +1,53 @@ +// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- +// vi: set et ts=4 sw=2 sts=2: +#ifndef DUNE_COMMON_ITERATORRANGE_HH +#define DUNE_COMMON_ITERATORRANGE_HH + +namespace Dune { + + //! Simple range between a begin and an end iterator. + /** + * IteratorRange is mainly useful as a lightweight adaptor + * class when adding support for range-based for loops to + * existing containers that lack a standard begin(), end() + * pair of member functions. + * + * \tparam Iterator The type of iterator + */ + template<typename Iterator> + class IteratorRange + { + + public: + + //! The iterator belonging to this range. + typedef Iterator iterator; + + //! Constructs an iterator range on [begin,end). + IteratorRange(const Iterator& begin, const Iterator& end) + : _begin(begin) + , _end(end) + {} + + //! Returns an iterator pointing to the begin of the range. + iterator begin() const + { + return _begin; + } + + //! Returns an iterator pointing past the end of the range. + iterator end() const + { + return _end; + } + + private: + + const Iterator _begin; + const Iterator _end; + + }; + +} + +#endif // DUNE_COMMON_ITERATORRANGE_HH diff --git a/m4/CMakeLists.txt b/m4/CMakeLists.txt index 1de8462b8ef97a6f7698d36135785b0aff236d0a..e5e6d9fec1131d2189664fd549f0d0d0779330a6 100644 --- a/m4/CMakeLists.txt +++ b/m4/CMakeLists.txt @@ -11,6 +11,7 @@ install(PROGRAMS cxx0x_nullptr.m4 cxx11_constexpr.m4 cxx11_final.m4 + cxx11_range_based_for.m4 dune.m4 dune_all.m4 dune_autobuild.m4 diff --git a/m4/Makefile.am b/m4/Makefile.am index fddde4a672ed8ba465fee1dd27a652c45b4d6502..4e388cf9cbf843838d0fe79e74206567380e38c4 100644 --- a/m4/Makefile.am +++ b/m4/Makefile.am @@ -14,6 +14,7 @@ ALLM4S = \ cxx0x_nullptr.m4 \ cxx11_constexpr.m4 \ cxx11_final.m4 \ + cxx11_range_based_for.m4 \ dune.m4 \ dune_all.m4 \ dune_autobuild.m4 \ diff --git a/m4/cxx11_range_based_for.m4 b/m4/cxx11_range_based_for.m4 new file mode 100644 index 0000000000000000000000000000000000000000..7054543b4d61d72c39de2435bfb5a036597660c0 --- /dev/null +++ b/m4/cxx11_range_based_for.m4 @@ -0,0 +1,22 @@ +# tests for C++11 range-based for support +# the associated define is called HAVE_RANGE_BASED_FOR + +AC_DEFUN([DUNE_CXX11_RANGE_BASED_FOR],[ + AC_CACHE_CHECK([for C++11 range-based for], [dune_cv_cxx11_range_based_for_support], [ + AC_REQUIRE([AC_PROG_CXX]) + AC_REQUIRE([CXX11]) + AC_LANG_PUSH([C++]) + AC_COMPILE_IFELSE([ + AC_LANG_PROGRAM(,[[ + int arr[3]; + for(int &val : arr) + val = 0; + ]])], + [dune_cv_cxx11_range_based_for_support=yes], + [dune_cv_cxx11_range_based_for_support=no]) + AC_LANG_POP + ]) + if test "x$dune_cv_cxx11_range_based_for_support" = xyes; then + AC_DEFINE([HAVE_RANGE_BASED_FOR], [1], [Define to 1 if C++11 range-based for is supported]) + fi +]) diff --git a/m4/dune_common.m4 b/m4/dune_common.m4 index 2b8703d47cf38799d83eda7bbf161a1c95935327..b0f0ecfc1cfdf4758899d5ef9b65503f18a528ac 100644 --- a/m4/dune_common.m4 +++ b/m4/dune_common.m4 @@ -21,6 +21,7 @@ AC_DEFUN([DUNE_COMMON_CHECKS], AC_REQUIRE([CXX11]) AC_REQUIRE([NULLPTR_CHECK]) AC_REQUIRE([CXX11_CONSTEXPR_CHECK]) + AC_REQUIRE([DUNE_CXX11_RANGE_BASED_FOR]) AC_REQUIRE([DUNE_BOOST_BASE]) AC_REQUIRE([DUNE_LINKCXX]) AC_REQUIRE([DUNE_CHECKDEPRECATED])