From a426e027596fbc25f49c05f6818a0fbbe9a0573a Mon Sep 17 00:00:00 2001 From: Alexander Grund Date: Thu, 9 Jul 2020 21:02:10 +0200 Subject: [PATCH] Replace Boost.Bind by std::bind and lambdas --- doc/example/patterns_async_call.cpp | 4 ++-- doc/example/patterns_invoke_functor.cpp | 4 ++-- doc/example/reference.cpp | 4 ++-- include/turtle/detail/action.hpp | 28 +++++-------------------- test/detail/test_function.cpp | 11 +++++----- test/detail/test_is_functor.cpp | 5 ++++- test/test_integration.cpp | 4 ++-- test/test_log.cpp | 4 +++- test/test_mock.cpp | 5 +++-- 9 files changed, 28 insertions(+), 41 deletions(-) diff --git a/doc/example/patterns_async_call.cpp b/doc/example/patterns_async_call.cpp index e1b2a60..918c5d4 100644 --- a/doc/example/patterns_async_call.cpp +++ b/doc/example/patterns_async_call.cpp @@ -64,10 +64,10 @@ BOOST_AUTO_TEST_CASE( method_is_called ) // Note: Boost 1.57 introduced a bug preventing usage of the lambda with clang in C++98 // See: https://svn.boost.org/trac10/ticket/10785 #if defined(BOOST_CLANG) && (BOOST_VERSION >= 105700) - MOCK_EXPECT( m.method ).once().calls( boost::bind(&set_bool, done) ); + MOCK_EXPECT( m.method ).once().calls( std::bind(&set_bool, done) ); #else MOCK_EXPECT( m.method ).once().calls( boost::lambda::var( done ) = true ); #endif - check( done, boost::bind( &my_class::flush, &c ) ); // just wait on done, flushing from time to time + check( done, std::bind( &my_class::flush, &c ) ); // just wait on done, flushing from time to time } //] diff --git a/doc/example/patterns_invoke_functor.cpp b/doc/example/patterns_invoke_functor.cpp index d0c9f81..5a00f4a 100644 --- a/doc/example/patterns_invoke_functor.cpp +++ b/doc/example/patterns_invoke_functor.cpp @@ -24,8 +24,8 @@ namespace //[ invoke_functor_solution #define BOOST_AUTO_TEST_MAIN #include -#include #include +#include namespace { @@ -38,7 +38,7 @@ namespace BOOST_AUTO_TEST_CASE( how_to_invoke_a_functor_passed_as_parameter_of_a_mock_method ) { mock_class mock; - MOCK_EXPECT( mock.method ).calls( boost::bind( boost::apply< void >(), _1, 42 ) ); // whenever 'method' is called, invoke the functor with 42 + MOCK_EXPECT( mock.method ).calls( [](const auto &functor){ functor(42); } ); // whenever 'method' is called, invoke the functor with 42 function( mock ); } //] diff --git a/doc/example/reference.cpp b/doc/example/reference.cpp index d6aa41e..2c6d7e8 100644 --- a/doc/example/reference.cpp +++ b/doc/example/reference.cpp @@ -572,7 +572,7 @@ bool custom_constraint( int expected, int actual ) BOOST_AUTO_TEST_CASE( demonstrates_adding_a_custom_constraint_with_boost_bind ) { mock_class c; - MOCK_EXPECT( c.method ).with( boost::bind( &custom_constraint, 42, _1 ) ); + MOCK_EXPECT( c.method ).with( std::bind( &custom_constraint, 42, _1 ) ); } //] } @@ -722,7 +722,7 @@ BOOST_AUTO_TEST_CASE( demonstrates_configuring_actions ) MOCK_EXPECT( c.method ).moves( 42 ); // returns by moving the value MOCK_EXPECT( c.method ).throws( std::runtime_error( "error !" ) ); MOCK_EXPECT( c.method ).calls( &function ); // forwards 'method' parameter to 'function' - MOCK_EXPECT( c.method ).calls( boost::bind( &function, 42 ) ); // drops 'method' parameter and binds 42 as parameter to 'function' + MOCK_EXPECT( c.method ).calls( std::bind( &function, 42 ) ); // drops 'method' parameter and binds 42 as parameter to 'function' MOCK_EXPECT( c.method ).calls( []( int i ) { return i; } ); // uses a C++11 lambda } //] diff --git a/include/turtle/detail/action.hpp b/include/turtle/detail/action.hpp index 155e5cb..ac886a4 100644 --- a/include/turtle/detail/action.hpp +++ b/include/turtle/detail/action.hpp @@ -10,7 +10,6 @@ #define MOCK_ACTION_HPP_INCLUDED #include "../config.hpp" -#include #include #include #include @@ -50,7 +49,7 @@ namespace detail template< typename Exception > void throws( Exception e ) { - a_ = boost::bind( &do_throw< Exception >, e ); + a_ = [e]() -> Result { throw e; }; } protected: @@ -61,21 +60,10 @@ namespace detail template< typename Y > void set( const std::reference_wrapper< Y >& r ) { - a_ = boost::bind( &do_ref< Y >, &r.get() ); + a_ = [r]() -> Result { return r.get(); }; } private: - template< typename T > - static T& do_ref( T* t ) - { - return *t; - } - template< typename T > - static Result do_throw( T t ) - { - throw t; - } - functor_type f_; action_type a_; }; @@ -98,10 +86,8 @@ namespace detail template< typename Value > void moves( Value&& v ) { - this->set( - boost::bind( - &move< std::remove_reference_t< Value > >, - std::ref( store( std::move( v ) ) ) ) ); + auto vRef = std::ref( store( std::move( v ) ) ); + this->set([vRef](){ return std::move(vRef.get()); }); } private: @@ -163,12 +149,8 @@ namespace detail public: action() { - this->set( boost::bind( &do_nothing ) ); + this->set( [](){} ); } - - private: - static void do_nothing() - {} }; #ifdef MOCK_AUTO_PTR diff --git a/test/detail/test_function.cpp b/test/detail/test_function.cpp index d28b9a7..c0d4977 100644 --- a/test/detail/test_function.cpp +++ b/test/detail/test_function.cpp @@ -12,7 +12,6 @@ #include #include #include -#include #include #include #include @@ -37,10 +36,10 @@ BOOST_FIXTURE_TEST_CASE( a_function_can_be_passed_as_functor, mock_error_fixture std::function< void() > functor = f; } -BOOST_FIXTURE_TEST_CASE( a_function_can_be_passed_as_functor_using_boost_bind_and_boost_ref, mock_error_fixture ) +BOOST_FIXTURE_TEST_CASE( a_function_can_be_passed_as_functor_using_std_bind_and_std_ref, mock_error_fixture ) { mock::detail::function< void() > f; - std::function< void() > functor = boost::bind( std::ref( f ) ); + std::function< void() > functor = std::bind( std::ref( f ) ); } // invocations @@ -688,10 +687,10 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_calls_the_custom_functor_with CHECK_CALLS( 1 ); } -BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_calls_the_custom_functor_without_parameters_thanks_to_boost_bind, mock_error_fixture ) +BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_calls_the_custom_functor_without_parameters_thanks_to_std_bind, mock_error_fixture ) { mock::detail::function< int( int ) > f; - f.expect().calls( boost::bind( &custom_result ) ); + f.expect().calls( std::bind( &custom_result ) ); BOOST_CHECK_EQUAL( 42, f( 17 ) ); CHECK_CALLS( 1 ); } @@ -917,7 +916,7 @@ BOOST_FIXTURE_TEST_CASE( function_is_thread_safe, mock_error_fixture ) mock::detail::function< int() > f; boost::thread_group group; for( int i = 0; i < 100; ++i ) - group.create_thread( boost::bind( &iterate, std::ref( f ) ) ); + group.create_thread( [&f](){ iterate(f); } ); group.join_all(); CHECK_CALLS( 100 ); } diff --git a/test/detail/test_is_functor.cpp b/test/detail/test_is_functor.cpp index f45a1e4..e384f83 100644 --- a/test/detail/test_is_functor.cpp +++ b/test/detail/test_is_functor.cpp @@ -71,11 +71,14 @@ BOOST_AUTO_TEST_CASE( std_bind_first_is_functor ) is_functor( std::bind1st( std::ptr_fun( &f2 ), "" ) ); } -BOOST_AUTO_TEST_CASE( boost_bind_is_functor ) +BOOST_AUTO_TEST_CASE( bind_is_functor ) { is_functor( boost::bind( &f0 ) ); is_functor( boost::bind( &f1, _1 ) ); is_functor( boost::bind( &f2, "", _1 ) ); + is_functor( std::bind( &f0 ) ); + is_functor( std::bind( &f1, std::placeholders::_1 ) ); + is_functor( std::bind( &f2, "", std::placeholders::_1 ) ); } BOOST_AUTO_TEST_CASE( boost_lambda_is_functor ) diff --git a/test/test_integration.cpp b/test/test_integration.cpp index 9460fa8..f517a6a 100644 --- a/test/test_integration.cpp +++ b/test/test_integration.cpp @@ -646,7 +646,7 @@ BOOST_FIXTURE_TEST_CASE( mock_functor_creation_is_thread_safe, mock_error_fixtur { boost::thread_group group; for( int i = 0; i < 100; ++i ) - group.create_thread( boost::bind( &create_functor, i ) ); + group.create_thread( [i](){ create_functor( i ); } ); group.join_all(); CHECK_CALLS( 100 ); } @@ -665,7 +665,7 @@ BOOST_FIXTURE_TEST_CASE( mock_class_is_thread_safe, mock_error_fixture ) my_mock m; boost::thread_group group; for( int i = 0; i < 100; ++i ) - group.create_thread( boost::bind( &iterate, std::ref( m ) ) ); + group.create_thread( [&m](){ iterate(m); } ); group.join_all(); CHECK_CALLS( 100 ); } diff --git a/test/test_log.cpp b/test/test_log.cpp index 5b55bb6..f4d7c81 100644 --- a/test/test_log.cpp +++ b/test/test_log.cpp @@ -25,6 +25,7 @@ #include #endif #include +#include #include #include #include @@ -646,9 +647,10 @@ BOOST_AUTO_TEST_CASE( boost_phoenix_functor_yields_question_mark_when_serialized BOOST_CHECK_EQUAL( "?", to_string( boost::phoenix::arg_names::_1 < 42 ) ); } -BOOST_AUTO_TEST_CASE( boost_bind_functor_yields_question_mark_when_serialized ) +BOOST_AUTO_TEST_CASE( bind_functor_yields_question_mark_when_serialized ) { BOOST_CHECK_EQUAL( "?", to_string( boost::bind( &some_function ) ) ); + BOOST_CHECK_EQUAL( "?", to_string( std::bind( &some_function ) ) ); } #ifndef BOOST_MSVC // this produces an ICE with all versions of MSVC diff --git a/test/test_mock.cpp b/test/test_mock.cpp index 2f0d42f..d143b0f 100644 --- a/test/test_mock.cpp +++ b/test/test_mock.cpp @@ -10,7 +10,7 @@ #include #include #include -#include +#include namespace { @@ -162,7 +162,8 @@ namespace BOOST_FIXTURE_TEST_CASE( MOCK_CONST_METHOD_EXT_macro_defines_a_bindable_method, mock_error_fixture ) { my_mock m; - boost::bind( &my_mock::my_method, &m, 42 ); + const auto f = std::bind( &my_mock::my_method, &m, 42 ); + (void) f; } BOOST_FIXTURE_TEST_CASE( MOCK_VERIFY_macro, mock_error_fixture )