Use std::reference_wrapper instead of boost

This commit is contained in:
Alexander Grund 2020-07-09 20:25:40 +02:00
parent 353849e9ad
commit 35e43d58a6
No known key found for this signature in database
GPG key ID: AA48A0760367A42B
16 changed files with 116 additions and 90 deletions

View file

@ -86,8 +86,8 @@ struct near_constraint
template< typename Actual >
bool operator()( Actual actual ) const
{
return std::abs( actual - boost::unwrap_ref( expected_ ) )
< boost::unwrap_ref( threshold_ );
return std::abs( actual - unwrap_ref( expected_ ) )
< unwrap_ref( threshold_ );
}
friend std::ostream& operator<<( std::ostream& s, const near_constraint& c )
@ -127,7 +127,7 @@ BOOST_AUTO_TEST_CASE( forty_one_plus_one_is_forty_two_plus_or_minus_one )
mock_view v;
calculator c( v );
int expected, threshold;
MOCK_EXPECT( v.display ).with( near( boost::cref( expected ), boost::cref( threshold ) ) );
MOCK_EXPECT( v.display ).with( near( std::cref( expected ), std::cref( threshold ) ) );
expected = 42;
threshold = 1;
c.add( 41, 1 );

View file

@ -44,7 +44,7 @@ BOOST_AUTO_TEST_CASE( method_is_called_two_times_with_the_same_value )
my_class c( mock );
int value;
MOCK_EXPECT( mock.method ).once().with( mock::retrieve( value ) ); // on first call retrieve the value, this expectation takes precedence because it can never fail
MOCK_EXPECT( mock.method ).once().with( boost::cref( value ) ); // on second call compare the previously retrieved value with the newly received one
MOCK_EXPECT( mock.method ).once().with( std::cref( value ) ); // on second call compare the previously retrieved value with the newly received one
c.process();
}
//]

View file

@ -740,7 +740,7 @@ BOOST_AUTO_TEST_CASE( demonstrates_configuring_actions_with_references )
{
mock_class c;
int i = 0;
MOCK_EXPECT( c.method ).returns( boost::ref( i ) ); // wrap i to store a reference
MOCK_EXPECT( c.method ).returns( std::ref( i ) ); // wrap i to store a reference
c.method() = 42; // really change i and not just the stored copy
BOOST_CHECK_EQUAL( 42, i ); // indeed
}

View file

@ -11,7 +11,7 @@
#include "config.hpp"
#include "log.hpp"
#include <boost/ref.hpp>
#include "detail/unwrap_reference.hpp"
#include <boost/preprocessor/stringize.hpp>
#include <boost/preprocessor/control/if.hpp>
#include <boost/preprocessor/variadic/to_array.hpp>
@ -19,6 +19,7 @@
#include <boost/preprocessor/repetition/enum_params.hpp>
#include <boost/preprocessor/repetition/enum.hpp>
#include <boost/preprocessor/array.hpp>
#include <functional>
#include <type_traits>
namespace mock
@ -148,7 +149,7 @@ namespace detail
expected##n( std::forward< T##n >(e##n) )
#define MOCK_CONSTRAINT_UNWRAP_REF(z, n, d) \
boost::unwrap_ref( expected##n )
mock::detail::unwrap_ref( expected##n )
#define MOCK_CONSTRAINT_FORMAT(z, n, d) \
BOOST_PP_IF(n, << ", " <<,) mock::format( c.expected##n )
@ -160,7 +161,7 @@ namespace detail
std::decay_t< const T##n >
#define MOCK_CONSTRAINT_CREF_PARAM(z, n, Args) \
const typename boost::unwrap_reference< Expected_##n >::type& \
const mock::detail::unwrap_reference_t< Expected_##n >& \
BOOST_PP_ARRAY_ELEM(n, Args)
#define MOCK_CONSTRAINT_ARG(z, n, Args) \

View file

@ -12,7 +12,7 @@
#include "config.hpp"
#include "constraint.hpp"
#include "detail/move_helper.hpp"
#include <boost/ref.hpp>
#include "detail/unwrap_reference.hpp"
#include <boost/version.hpp>
#include <boost/type_traits/make_void.hpp>
#if BOOST_VERSION >= 107000
@ -20,6 +20,7 @@
#else
#include <boost/test/floating_point_comparison.hpp>
#endif
#include <functional>
#include <memory>
#include <type_traits>
@ -131,24 +132,22 @@ namespace detail
std::enable_if_t<
has_equal_to<
Actual,
typename
boost::unwrap_reference< Expected >::type
unwrap_reference_t< Expected >
>::value
>* = 0 ) const
{
return actual == boost::unwrap_ref( expected_ );
return actual == unwrap_ref( expected_ );
}
template< typename Actual >
bool operator()( const Actual& actual,
std::enable_if_t<
!has_equal_to<
Actual,
typename
boost::unwrap_reference< Expected >::type
unwrap_reference_t< Expected >
>::value
>* = 0 ) const
{
return actual && *actual == boost::unwrap_ref( expected_ );
return actual && *actual == unwrap_ref( expected_ );
}
friend std::ostream& operator<<( std::ostream& s, const equal& e )
{
@ -161,7 +160,7 @@ namespace detail
struct same
{
explicit same( const Expected& expected )
: expected_( std::addressof( boost::unwrap_ref( expected ) ) )
: expected_( std::addressof( unwrap_ref( expected ) ) )
{}
template< typename Actual >
bool operator()( const Actual& actual ) const
@ -172,23 +171,21 @@ namespace detail
{
return os << "same( " << mock::format( *s.expected_ ) << " )";
}
const typename
boost::unwrap_reference< Expected >::type* expected_;
const unwrap_reference_t< Expected >* expected_;
};
template< typename Expected >
struct retrieve
{
explicit retrieve( Expected& expected )
: expected_( std::addressof( boost::unwrap_ref( expected ) ) )
: expected_( std::addressof( unwrap_ref( expected ) ) )
{}
template< typename Actual >
bool operator()( const Actual& actual,
std::enable_if_t<
!std::is_convertible<
const Actual*,
typename
boost::unwrap_reference< Expected >::type
unwrap_reference_t< Expected >
>::value
>* = 0 ) const
{
@ -200,8 +197,7 @@ namespace detail
std::enable_if_t<
!std::is_convertible<
const Actual*,
typename
boost::unwrap_reference< Expected >::type
unwrap_reference_t< Expected >
>::value
>* = 0 ) const
{
@ -212,8 +208,7 @@ namespace detail
bool operator()( Actual& actual,
std::enable_if_t<
std::is_convertible< Actual*,
typename
boost::unwrap_reference< Expected >::type
unwrap_reference_t< Expected >
>::value
>* = 0 ) const
{
@ -224,8 +219,7 @@ namespace detail
{
return s << "retrieve( " << mock::format( *r.expected_ ) << " )";
}
typename
boost::unwrap_reference< Expected >::type* expected_;
unwrap_reference_t< Expected >* expected_;
};
template< typename Expected >
@ -237,22 +231,21 @@ namespace detail
template< typename Actual >
bool operator()( Actual& actual ) const
{
actual = boost::unwrap_ref( expected_ );
actual = unwrap_ref( expected_ );
return true;
}
template< typename Actual >
bool operator()( Actual* actual,
std::enable_if_t<
std::is_convertible<
typename
boost::unwrap_reference< Expected >::type,
unwrap_reference_t< Expected >,
Actual
>::value
>* = 0 ) const
{
if( ! actual )
return false;
*actual = boost::unwrap_ref( expected_ );
*actual = unwrap_ref( expected_ );
return true;
}
friend std::ostream& operator<<( std::ostream& s, const assign& a )
@ -270,7 +263,7 @@ namespace detail
{}
bool operator()( const std::string& actual ) const
{
return actual.find( boost::unwrap_ref( expected_ ) )
return actual.find( unwrap_ref( expected_ ) )
!= std::string::npos;
}
friend std::ostream& operator<<( std::ostream& s, const contain& n )

View file

@ -11,7 +11,6 @@
#include "../config.hpp"
#include <boost/bind.hpp>
#include <boost/ref.hpp>
#include <functional>
#include <memory>
#include <type_traits>
@ -60,9 +59,9 @@ namespace detail
a_ = a;
}
template< typename Y >
void set( const boost::reference_wrapper< Y >& r )
void set( const std::reference_wrapper< Y >& r )
{
a_ = boost::bind( &do_ref< Y >, r.get_pointer() );
a_ = boost::bind( &do_ref< Y >, &r.get() );
}
private:
@ -88,10 +87,10 @@ namespace detail
template< typename Value >
void returns( const Value& v )
{
this->set( boost::ref( store( v ) ) );
this->set( std::ref( store( v ) ) );
}
template< typename Y >
void returns( const boost::reference_wrapper< Y >& r )
void returns( const std::reference_wrapper< Y >& r )
{
this->set( r );
}
@ -102,7 +101,7 @@ namespace detail
this->set(
boost::bind(
&move< std::remove_reference_t< Value > >,
boost::ref( store( std::move( v ) ) ) ) );
std::ref( store( std::move( v ) ) ) ) );
}
private:
@ -183,23 +182,23 @@ namespace detail
: v_( rhs.v_.release() )
{
if( v_.get() )
returns( boost::ref( v_ ) );
returns( std::ref( v_ ) );
}
template< typename Y >
void returns( Y* r )
{
v_.reset( r );
this->set( boost::ref( v_ ) );
this->set( std::ref( v_ ) );
}
template< typename Y >
void returns( std::auto_ptr< Y > r )
{
v_ = r;
this->set( boost::ref( v_ ) );
this->set( std::ref( v_ ) );
}
template< typename Y >
void returns( const boost::reference_wrapper< Y >& r )
void returns( const std::reference_wrapper< Y >& r )
{
this->set( r );
}

View file

@ -76,7 +76,7 @@ namespace detail
e_->returns( r );
}
template< typename Y >
void returns( const boost::reference_wrapper< Y >& r )
void returns( const std::reference_wrapper< Y >& r )
{
e_->returns( r );
}

View file

@ -0,0 +1,45 @@
// http://turtle.sourceforge.net
//
// Copyright Alexander Grund 2020
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef MOCK_UNWRAP_REFERENCE_HPP_INCLUDED
#define MOCK_UNWRAP_REFERENCE_HPP_INCLUDED
#include <functional>
#include <type_traits>
namespace mock
{
namespace detail
{
template<class T>
struct unwrap_reference
{
using type = T;
};
template<class T>
struct unwrap_reference<std::reference_wrapper<T>>
{
using type = T;
};
template<class T>
struct unwrap_reference<const std::reference_wrapper<T>>
{
using type = T;
};
template<class T>
using unwrap_reference_t = typename unwrap_reference<T>::type;
template<class T>
BOOST_FORCEINLINE unwrap_reference_t<T>& unwrap_ref( T& t ) noexcept
{
return t;
}
}
}
#endif // MOCK_UNWRAP_REFERENCE_HPP_INCLUDED

View file

@ -123,6 +123,11 @@ namespace detail
return s << mock::format( t.get() );
}
template< typename T >
stream& operator<<( stream& s, const std::reference_wrapper< T >& t )
{
return s << mock::format( t.get() );
}
template< typename T >
stream& operator<<( stream& s, const boost::shared_ptr< T >& t )
{
return s << mock::format( t.get() );

View file

@ -14,8 +14,8 @@
#include "constraints.hpp"
#include "detail/is_functor.hpp"
#include "detail/move_helper.hpp"
#include <boost/ref.hpp>
#include <cstring>
#include <functional>
#include <type_traits>
namespace mock
@ -30,7 +30,7 @@ namespace mock
bool operator()( std::add_lvalue_reference_t< const Actual > actual )
{
return mock::equal(
boost::unwrap_ref( expected_ ) ).c_( actual );
mock::detail::unwrap_ref( expected_ ) ).c_( actual );
}
friend std::ostream& operator<<(
std::ostream& s, const matcher& m )

View file

@ -40,7 +40,7 @@ BOOST_FIXTURE_TEST_CASE( a_function_can_be_passed_as_functor, mock_error_fixture
BOOST_FIXTURE_TEST_CASE( a_function_can_be_passed_as_functor_using_boost_bind_and_boost_ref, mock_error_fixture )
{
mock::detail::function< void() > f;
std::function< void() > functor = boost::bind( boost::ref( f ) );
std::function< void() > functor = boost::bind( std::ref( f ) );
}
// invocations
@ -410,7 +410,7 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_returns_the_set_value, mock_e
{
mock::detail::function< int() > f;
int i = 42;
f.expect().returns( boost::ref( i ) );
f.expect().returns( std::ref( i ) );
i = 43;
BOOST_CHECK_EQUAL( 43, f() );
CHECK_CALLS( 1 );
@ -439,7 +439,7 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_returns_the_set_value, mock_e
{
mock::detail::function< int&() > f;
int i = 42;
f.expect().returns( boost::ref( i ) );
f.expect().returns( std::ref( i ) );
i = 43;
BOOST_CHECK_EQUAL( 43, f() );
BOOST_CHECK_EQUAL( 12, f() = 12 );
@ -514,7 +514,7 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_returns_the_set_auto_ptr_valu
{
mock::detail::function< std::auto_ptr< int >() > f;
std::auto_ptr< int > ptr( new int( 3 ) );
f.expect().returns( boost::ref( ptr ) );
f.expect().returns( std::ref( ptr ) );
BOOST_CHECK_EQUAL( 3, *ptr );
BOOST_CHECK_EQUAL( 3, *f() );
BOOST_CHECK( ! ptr.get() );
@ -638,7 +638,7 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_returns_by_reference, mock_er
{
mock::detail::function< base&() > f;
derived b;
f.expect().returns( boost::ref( b ) );
f.expect().returns( std::ref( b ) );
BOOST_CHECK_NO_THROW( f() );
CHECK_CALLS( 1 );
}
@ -651,7 +651,7 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_returns_by_reference, mock_er
}
{
mock::detail::function< undefined&() > f;
f.expect().returns( boost::ref( get_undefined() ) );
f.expect().returns( std::ref( get_undefined() ) );
f.reset();
}
}
@ -917,7 +917,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, boost::ref( f ) ) );
group.create_thread( boost::bind( &iterate, std::ref( f ) ) );
group.join_all();
CHECK_CALLS( 100 );
}

View file

@ -48,7 +48,7 @@ BOOST_AUTO_TEST_CASE( equal_constraint )
BOOST_CHECK( ! mock::equal( std::string( "string" ) ).c_( "not string" ) );
{
std::string s;
BOOST_AUTO( c, mock::equal( boost::cref( s ) ) );
BOOST_AUTO( c, mock::equal( std::cref( s ) ) );
s = "string";
BOOST_CHECK( c.c_( "string" ) );
}
@ -94,11 +94,7 @@ BOOST_AUTO_TEST_CASE( same_constraint )
int i = 0;
int j = 0;
BOOST_CHECK_EQUAL( i, j );
mock::constraint<
mock::detail::same<
const boost::reference_wrapper< const int >
>
> c = mock::same( boost::cref( i ) );
auto c = mock::same( i );
BOOST_CHECK( ! c.c_( j ) );
BOOST_CHECK( c.c_( i ) );
}
@ -137,9 +133,9 @@ BOOST_AUTO_TEST_CASE( assign_constraint )
int j = 1;
mock::constraint<
mock::detail::assign<
boost::reference_wrapper< const int >
std::reference_wrapper< const int >
>
> c = mock::assign( boost::cref( j ) );
> c = mock::assign( std::cref( j ) );
BOOST_CHECK( c.c_( i ) );
BOOST_CHECK_EQUAL( 1, i );
j = 3;
@ -149,11 +145,7 @@ BOOST_AUTO_TEST_CASE( assign_constraint )
{
int i = 0;
int j = 1;
mock::constraint<
mock::detail::assign<
boost::reference_wrapper< const int >
>
> c = mock::assign( boost::cref( j ) );
auto c = mock::assign( std::cref( j ) );
BOOST_CHECK( c.c_( &i ) );
BOOST_CHECK_EQUAL( 1, i );
j = 3;
@ -164,11 +156,7 @@ BOOST_AUTO_TEST_CASE( assign_constraint )
const int* i = 0;
int k = 1;
int* j = &k;
mock::constraint<
mock::detail::assign<
boost::reference_wrapper< int* const >
>
> c = mock::assign( boost::cref( j ) );
auto c = mock::assign( std::cref( j ) );
BOOST_CHECK( c.c_( i ) );
BOOST_CHECK_EQUAL( j, i );
j = 0;
@ -230,13 +218,13 @@ BOOST_AUTO_TEST_CASE( retrieve_constraint )
{
int i = 0;
const int j = 1;
BOOST_CHECK( mock::retrieve( boost::ref( i ) ).c_( j ) );
BOOST_CHECK( mock::retrieve( i ).c_( j ) );
BOOST_CHECK_EQUAL( i, j );
}
{
const int* i = 0;
const int j = 1;
BOOST_CHECK( mock::retrieve( boost::ref( i ) ).c_( j ) );
BOOST_CHECK( mock::retrieve( i ).c_( j ) );
BOOST_CHECK_EQUAL( i, &j );
}
{
@ -332,11 +320,7 @@ BOOST_AUTO_TEST_CASE( contain_constraint_with_const_char_ptr )
BOOST_CHECK( ! mock::contain( "not found" ).c_( std::string( "this is a string" ) ) );
{
const char* s = 0;
mock::constraint<
mock::detail::contain<
boost::reference_wrapper< const char* const >
>
> c = mock::contain( boost::cref( s ) );
auto c = mock::contain( std::cref( s ) );
s = "string";
BOOST_CHECK( c.c_( "this is a string" ) );
BOOST_CHECK( c.c_( std::string( "this is a string" ) ) );
@ -356,9 +340,9 @@ BOOST_AUTO_TEST_CASE( contain_constraint_with_strings )
std::string s;
mock::constraint<
mock::detail::contain<
boost::reference_wrapper< const std::string >
std::reference_wrapper< const std::string >
>
> c = mock::contain( boost::cref( s ) );
> c = mock::contain( std::cref( s ) );
s = "string";
BOOST_CHECK( c.c_( "this is a string" ) );
BOOST_CHECK( c.c_( std::string( "this is a string" ) ) );

View file

@ -11,7 +11,6 @@
#include <turtle/mock.hpp>
#include <boost/test/auto_unit_test.hpp>
#include <boost/optional.hpp>
#include <boost/ref.hpp>
#include <cmath>
#include <functional>
@ -294,7 +293,7 @@ namespace
BOOST_FIXTURE_TEST_CASE( basic_mock_object_collaboration_usage, fixture )
{
MOCK_EXPECT( manager.get_observer ).returns( boost::ref( observer ) );
MOCK_EXPECT( manager.get_observer ).returns( std::ref( observer ) );
my_subject subject( manager );
MOCK_EXPECT( observer.notify ).once().with( 1 );
subject.increment();
@ -395,7 +394,7 @@ BOOST_FIXTURE_TEST_CASE( boost_optional_on_base_class_reference_as_return_type_i
{
boost_optional b;
my_mock_observer o;
MOCK_EXPECT( b.tag ).once().returns( boost::ref( o ) );
MOCK_EXPECT( b.tag ).once().returns( std::ref( o ) );
b.method();
CHECK_CALLS( 1 );
}
@ -460,7 +459,7 @@ BOOST_FIXTURE_TEST_CASE( boost_reference_wrapper_is_supported_in_value_constrain
{
MOCK_FUNCTOR( f, void( const std::string& ) );
std::string s;
MOCK_EXPECT( f ).once().with( boost::cref( s ) );
MOCK_EXPECT( f ).once().with( std::cref( s ) );
s = "string";
f( "string" );
CHECK_CALLS( 1 );
@ -666,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, boost::ref( m ) ) );
group.create_thread( boost::bind( &iterate, std::ref( m ) ) );
group.join_all();
CHECK_CALLS( 100 );
}

View file

@ -517,11 +517,11 @@ BOOST_AUTO_TEST_CASE( boost_assign_map_list_of_are_serialized )
BOOST_CHECK_EQUAL( "((12,\"12\"),(42,\"42\"))", to_string( boost::assign::map_list_of( 12, "12" )( 42, "42" ) ) );
}
BOOST_AUTO_TEST_CASE( boost_reference_wrappers_are_serialized )
BOOST_AUTO_TEST_CASE( std_reference_wrappers_are_serialized )
{
const int i = 3;
BOOST_CHECK_EQUAL( "3", to_string( boost::cref( i ) ) );
BOOST_CHECK_EQUAL( "\"string\"", to_string( boost::cref( "string" ) ) );
BOOST_CHECK_EQUAL( "3", to_string( std::cref( i ) ) );
BOOST_CHECK_EQUAL( "\"string\"", to_string( std::cref( "string" ) ) );
}
namespace

View file

@ -28,8 +28,8 @@ BOOST_AUTO_TEST_CASE( int_and_int_can_be_compared )
BOOST_AUTO_TEST_CASE( ref_to_int_and_int_can_be_compared )
{
const int i = 3;
BOOST_CHECK( match( 3, boost::cref( i ) ) );
BOOST_CHECK( ! match( 4, boost::cref( i ) ) );
BOOST_CHECK( match( 3, std::cref( i ) ) );
BOOST_CHECK( ! match( 4, std::cref( i ) ) );
}
namespace

View file

@ -44,7 +44,7 @@ namespace
BOOST_FIXTURE_TEST_CASE( mock_addition_operator, mock_error_fixture )
{
mock_class_with_operator m;
MOCK_EXPECT( m.addition ).once().returns( boost::ref( m ) );
MOCK_EXPECT( m.addition ).once().returns( std::ref( m ) );
m += 1;
CHECK_CALLS( 1 );
}