Replace Boost.Bind by std::bind and lambdas

This commit is contained in:
Alexander Grund 2020-07-09 21:02:10 +02:00
parent 35e43d58a6
commit a426e02759
No known key found for this signature in database
GPG key ID: AA48A0760367A42B
9 changed files with 28 additions and 41 deletions

View file

@ -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 // 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 // See: https://svn.boost.org/trac10/ticket/10785
#if defined(BOOST_CLANG) && (BOOST_VERSION >= 105700) #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 #else
MOCK_EXPECT( m.method ).once().calls( boost::lambda::var( done ) = true ); MOCK_EXPECT( m.method ).once().calls( boost::lambda::var( done ) = true );
#endif #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
} }
//] //]

View file

@ -24,8 +24,8 @@ namespace
//[ invoke_functor_solution //[ invoke_functor_solution
#define BOOST_AUTO_TEST_MAIN #define BOOST_AUTO_TEST_MAIN
#include <boost/test/auto_unit_test.hpp> #include <boost/test/auto_unit_test.hpp>
#include <boost/bind/apply.hpp>
#include <turtle/mock.hpp> #include <turtle/mock.hpp>
#include <functional>
namespace namespace
{ {
@ -38,7 +38,7 @@ namespace
BOOST_AUTO_TEST_CASE( how_to_invoke_a_functor_passed_as_parameter_of_a_mock_method ) BOOST_AUTO_TEST_CASE( how_to_invoke_a_functor_passed_as_parameter_of_a_mock_method )
{ {
mock_class mock; 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 ); function( mock );
} }
//] //]

View file

@ -572,7 +572,7 @@ bool custom_constraint( int expected, int actual )
BOOST_AUTO_TEST_CASE( demonstrates_adding_a_custom_constraint_with_boost_bind ) BOOST_AUTO_TEST_CASE( demonstrates_adding_a_custom_constraint_with_boost_bind )
{ {
mock_class c; 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 ).moves( 42 ); // returns by moving the value
MOCK_EXPECT( c.method ).throws( std::runtime_error( "error !" ) ); 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( &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 MOCK_EXPECT( c.method ).calls( []( int i ) { return i; } ); // uses a C++11 lambda
} }
//] //]

View file

@ -10,7 +10,6 @@
#define MOCK_ACTION_HPP_INCLUDED #define MOCK_ACTION_HPP_INCLUDED
#include "../config.hpp" #include "../config.hpp"
#include <boost/bind.hpp>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <type_traits> #include <type_traits>
@ -50,7 +49,7 @@ namespace detail
template< typename Exception > template< typename Exception >
void throws( Exception e ) void throws( Exception e )
{ {
a_ = boost::bind( &do_throw< Exception >, e ); a_ = [e]() -> Result { throw e; };
} }
protected: protected:
@ -61,21 +60,10 @@ namespace detail
template< typename Y > template< typename Y >
void set( const std::reference_wrapper< Y >& r ) void set( const std::reference_wrapper< Y >& r )
{ {
a_ = boost::bind( &do_ref< Y >, &r.get() ); a_ = [r]() -> Result { return r.get(); };
} }
private: 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_; functor_type f_;
action_type a_; action_type a_;
}; };
@ -98,10 +86,8 @@ namespace detail
template< typename Value > template< typename Value >
void moves( Value&& v ) void moves( Value&& v )
{ {
this->set( auto vRef = std::ref( store( std::move( v ) ) );
boost::bind( this->set([vRef](){ return std::move(vRef.get()); });
&move< std::remove_reference_t< Value > >,
std::ref( store( std::move( v ) ) ) ) );
} }
private: private:
@ -163,12 +149,8 @@ namespace detail
public: public:
action() action()
{ {
this->set( boost::bind( &do_nothing ) ); this->set( [](){} );
} }
private:
static void do_nothing()
{}
}; };
#ifdef MOCK_AUTO_PTR #ifdef MOCK_AUTO_PTR

View file

@ -12,7 +12,6 @@
#include <turtle/constraints.hpp> #include <turtle/constraints.hpp>
#include <boost/test/auto_unit_test.hpp> #include <boost/test/auto_unit_test.hpp>
#include <boost/utility/result_of.hpp> #include <boost/utility/result_of.hpp>
#include <boost/bind.hpp>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <type_traits> #include <type_traits>
@ -37,10 +36,10 @@ BOOST_FIXTURE_TEST_CASE( a_function_can_be_passed_as_functor, mock_error_fixture
std::function< void() > functor = f; 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; mock::detail::function< void() > f;
std::function< void() > functor = boost::bind( std::ref( f ) ); std::function< void() > functor = std::bind( std::ref( f ) );
} }
// invocations // invocations
@ -688,10 +687,10 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_calls_the_custom_functor_with
CHECK_CALLS( 1 ); 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; 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 ) ); BOOST_CHECK_EQUAL( 42, f( 17 ) );
CHECK_CALLS( 1 ); CHECK_CALLS( 1 );
} }
@ -917,7 +916,7 @@ BOOST_FIXTURE_TEST_CASE( function_is_thread_safe, mock_error_fixture )
mock::detail::function< int() > f; mock::detail::function< int() > f;
boost::thread_group group; boost::thread_group group;
for( int i = 0; i < 100; ++i ) 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(); group.join_all();
CHECK_CALLS( 100 ); CHECK_CALLS( 100 );
} }

View file

@ -71,11 +71,14 @@ BOOST_AUTO_TEST_CASE( std_bind_first_is_functor )
is_functor( std::bind1st( std::ptr_fun( &f2 ), "" ) ); 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( &f0 ) );
is_functor( boost::bind( &f1, _1 ) ); is_functor( boost::bind( &f1, _1 ) );
is_functor( boost::bind( &f2, "", _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 ) BOOST_AUTO_TEST_CASE( boost_lambda_is_functor )

View file

@ -646,7 +646,7 @@ BOOST_FIXTURE_TEST_CASE( mock_functor_creation_is_thread_safe, mock_error_fixtur
{ {
boost::thread_group group; boost::thread_group group;
for( int i = 0; i < 100; ++i ) 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(); group.join_all();
CHECK_CALLS( 100 ); CHECK_CALLS( 100 );
} }
@ -665,7 +665,7 @@ BOOST_FIXTURE_TEST_CASE( mock_class_is_thread_safe, mock_error_fixture )
my_mock m; my_mock m;
boost::thread_group group; boost::thread_group group;
for( int i = 0; i < 100; ++i ) 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(); group.join_all();
CHECK_CALLS( 100 ); CHECK_CALLS( 100 );
} }

View file

@ -25,6 +25,7 @@
#include <boost/lambda/lambda.hpp> #include <boost/lambda/lambda.hpp>
#endif #endif
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include <functional>
#include <vector> #include <vector>
#include <deque> #include <deque>
#include <list> #include <list>
@ -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_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( 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 #ifndef BOOST_MSVC // this produces an ICE with all versions of MSVC

View file

@ -10,7 +10,7 @@
#include <turtle/mock.hpp> #include <turtle/mock.hpp>
#include <boost/test/auto_unit_test.hpp> #include <boost/test/auto_unit_test.hpp>
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
#include <boost/bind.hpp> #include <functional>
namespace namespace
{ {
@ -162,7 +162,8 @@ namespace
BOOST_FIXTURE_TEST_CASE( MOCK_CONST_METHOD_EXT_macro_defines_a_bindable_method, mock_error_fixture ) BOOST_FIXTURE_TEST_CASE( MOCK_CONST_METHOD_EXT_macro_defines_a_bindable_method, mock_error_fixture )
{ {
my_mock m; 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 ) BOOST_FIXTURE_TEST_CASE( MOCK_VERIFY_macro, mock_error_fixture )