|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r75550 - in trunk: boost libs/scope_exit/test
From: Alexander.Nasonov_at_[hidden]
Date: 2011-11-19 07:21:37
Author: nasonov
Date: 2011-11-19 07:21:36 EST (Sat, 19 Nov 2011)
New Revision: 75550
URL: http://svn.boost.org/trac/boost/changeset/75550
Log:
Use C++-0X lambdas when they are available.
Text files modified:
trunk/boost/scope_exit.hpp | 54 ++++++++++++++++++++++++++++++++++++++-
trunk/libs/scope_exit/test/native.cpp | 44 +++++++++++++++++++++++++++++++
trunk/libs/scope_exit/test/tu_test.hpp | 4 --
3 files changed, 96 insertions(+), 6 deletions(-)
Modified: trunk/boost/scope_exit.hpp
==============================================================================
--- trunk/boost/scope_exit.hpp (original)
+++ trunk/boost/scope_exit.hpp 2011-11-19 07:21:36 EST (Sat, 19 Nov 2011)
@@ -1,4 +1,4 @@
-// Copyright Alexander Nasonov 2006-2009
+// Copyright Alexander Nasonov 2006-2009, 2011
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
@@ -9,6 +9,55 @@
#include <boost/config.hpp>
+#if !defined(BOOST_NO_LAMBDAS) \
+ && !defined(BOOST_NO_AUTO_DECLARATIONS) \
+ && !defined(BOOST_SCOPE_EXIT_FORCE_CXX98)
+#define BOOST_SCOPE_EXIT_AUX_CXX0X
+#endif
+
+#if defined(BOOST_SCOPE_EXIT_AUX_CXX0X)
+
+#include <boost/preprocessor/cat.hpp>
+#include <boost/preprocessor/seq/enum.hpp>
+
+namespace boost { namespace scope_exit { namespace aux {
+
+template<class F>
+struct guard
+{
+ F fn_;
+ guard(F f) : fn_(f) {}
+ ~guard() { fn_(); }
+};
+
+struct guard_maker
+{
+ template<class F>
+ guard<F> operator%(F f) const
+ {
+ return guard<F>(f);
+ }
+};
+
+} } }
+
+#define BOOST_SCOPE_EXIT_AUX_IMPL(id, seq) \
+ auto BOOST_PP_CAT(boost_se_fn_, id) = \
+ ::boost::scope_exit::aux::guard_maker() % \
+ [BOOST_PP_SEQ_ENUM(seq)]() mutable -> void
+
+#if defined(BOOST_MSVC)
+#define BOOST_SCOPE_EXIT(seq) BOOST_SCOPE_EXIT_AUX_IMPL(__COUNTER__, seq)
+#else
+#define BOOST_SCOPE_EXIT(seq) BOOST_SCOPE_EXIT_AUX_IMPL(__LINE__, seq)
+#endif
+
+#define BOOST_SCOPE_EXIT_TPL(seq) BOOST_SCOPE_EXIT(seq)
+
+#define BOOST_SCOPE_EXIT_END ;
+
+#else
+
#include <boost/detail/workaround.hpp>
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/facilities/empty.hpp>
@@ -263,5 +312,6 @@
#define BOOST_SCOPE_EXIT_TPL(seq) BOOST_SCOPE_EXIT(seq)
#endif
-#endif // #ifndef FILE_boost_scope_exit_hpp_INCLUDED
+#endif // #if defined(BOOST_SCOPE_EXIT_AUX_CXX0X)
+#endif // #ifndef FILE_boost_scope_exit_hpp_INCLUDED
Modified: trunk/libs/scope_exit/test/native.cpp
==============================================================================
--- trunk/libs/scope_exit/test/native.cpp (original)
+++ trunk/libs/scope_exit/test/native.cpp 2011-11-19 07:21:36 EST (Sat, 19 Nov 2011)
@@ -1,4 +1,4 @@
-// Copyright Alexander Nasonov 2007-2008
+// Copyright Alexander Nasonov 2007-2008, 2011
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
@@ -169,10 +169,52 @@
BOOST_CHECK(results[1] == false);
}
+void test_cxx0x()
+{
+#if defined(BOOST_SCOPE_EXIT_AUX_CXX0X)
+ int i = 0, j = 1;
+
+ {
+ BOOST_SCOPE_EXIT((=))
+ {
+ i = j = 1; // modify copies
+ };
+ }
+ BOOST_CHECK(i == 0);
+ BOOST_CHECK(j == 1);
+
+ {
+ BOOST_SCOPE_EXIT((&))
+ {
+ i = 1;
+ j = 2;
+ };
+ BOOST_CHECK(i == 0);
+ BOOST_CHECK(j == 1);
+ }
+ BOOST_CHECK(i == 1);
+ BOOST_CHECK(j == 2);
+
+ {
+ BOOST_SCOPE_EXIT((=)(&j))
+ {
+ i = 2; // modify a copy
+ j = 3;
+ };
+ BOOST_CHECK(i == 1);
+ BOOST_CHECK(j == 2);
+ }
+ BOOST_CHECK(i == 1);
+ BOOST_CHECK(j == 3);
+
+#endif
+}
+
test_suite* init_unit_test_suite( int, char* [] )
{
framework::master_test_suite().p_name.value = "Unit test for ScopeExit";
framework::master_test_suite().add( BOOST_TEST_CASE( &test_non_local ));
framework::master_test_suite().add( BOOST_TEST_CASE( &test_types ));
+ framework::master_test_suite().add( BOOST_TEST_CASE( &test_cxx0x ));
return 0;
}
Modified: trunk/libs/scope_exit/test/tu_test.hpp
==============================================================================
--- trunk/libs/scope_exit/test/tu_test.hpp (original)
+++ trunk/libs/scope_exit/test/tu_test.hpp 2011-11-19 07:21:36 EST (Sat, 19 Nov 2011)
@@ -18,9 +18,7 @@
return i;
}
-#if !defined(BOOST_SCOPE_EXIT_AUX_GCC)
-#error "BOOST_SCOPE_EXIT_AUX_GCC undefined!"
-#elif BOOST_SCOPE_EXIT_AUX_GCC == 0 || BOOST_SCOPE_EXIT_AUX_GCC >= 304
+#if !defined(BOOST_SCOPE_EXIT_AUX_GCC) || BOOST_SCOPE_EXIT_AUX_GCC >= 304
template<class Int>
Int template_f(Int i)
{
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk