Refactored to use Boost.Phoenix only if required (e.g. more than 9 parameters) and Boost.Bind instead because it uses a lot less of memory when compiling

git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@188 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
mat007 2011-01-14 17:09:18 +00:00
parent aaab74f054
commit bd648e0579
5 changed files with 150 additions and 24 deletions

View file

@ -200,6 +200,10 @@
RelativePath="..\..\src\libraries\turtle\is_functor.hpp" RelativePath="..\..\src\libraries\turtle\is_functor.hpp"
> >
</File> </File>
<File
RelativePath="..\..\src\libraries\turtle\lambda.hpp"
>
</File>
<File <File
RelativePath="..\..\src\libraries\turtle\mock.hpp" RelativePath="..\..\src\libraries\turtle\mock.hpp"
> >

View file

@ -9,14 +9,12 @@
#ifndef MOCK_ACTION_HPP_INCLUDED #ifndef MOCK_ACTION_HPP_INCLUDED
#define MOCK_ACTION_HPP_INCLUDED #define MOCK_ACTION_HPP_INCLUDED
#include "config.hpp" #include "lambda.hpp"
#include <boost/spirit/home/phoenix/statement/throw.hpp> #include <boost/type_traits/remove_reference.hpp>
#include <boost/spirit/home/phoenix/operator/self.hpp> #include <boost/type_traits/remove_const.hpp>
#include <boost/spirit/home/phoenix/core/nothing.hpp>
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <boost/function.hpp> #include <boost/function.hpp>
#include <boost/ref.hpp> #include <boost/ref.hpp>
#include <memory>
namespace mock namespace mock
{ {
@ -32,17 +30,20 @@ namespace detail
BOOST_DEDUCED_TYPENAME boost::remove_const< Result >::type BOOST_DEDUCED_TYPENAME boost::remove_const< Result >::type
>::type result_type; >::type result_type;
typedef BOOST_DEDUCED_TYPENAME
lambda< Signature > lambda;
public: public:
template< typename Value > template< typename Value >
void returns( Value v ) void returns( Value v )
{ {
r_.reset( new result_type( v ) ); r_.reset( new result_type( v ) );
f_ = boost::phoenix::val( boost::ref( *r_ ) ); f_ = lambda::make_val( boost::ref( *r_ ) );
} }
template< typename Y > template< typename Y >
void returns( const boost::reference_wrapper< Y >& r ) void returns( const boost::reference_wrapper< Y >& r )
{ {
f_ = *boost::phoenix::val( r.get_pointer() ); f_ = lambda::make_val( r );
} }
void calls( const functor_type& f ) void calls( const functor_type& f )
@ -55,7 +56,7 @@ namespace detail
template< typename Exception > template< typename Exception >
void throws( Exception e ) void throws( Exception e )
{ {
f_ = boost::phoenix::throw_( e ); f_ = lambda::make_throw( e );
} }
const functor_type& functor() const const functor_type& functor() const
@ -74,15 +75,18 @@ namespace detail
typedef BOOST_DEDUCED_TYPENAME typedef BOOST_DEDUCED_TYPENAME
boost::function< Signature > functor_type; boost::function< Signature > functor_type;
typedef BOOST_DEDUCED_TYPENAME
lambda< Signature > lambda;
public: public:
void returns( Result* r ) void returns( Result* r )
{ {
f_ = boost::phoenix::val( r ); f_ = lambda::make_val( r );
} }
template< typename Y > template< typename Y >
void returns( const boost::reference_wrapper< Y >& r ) void returns( const boost::reference_wrapper< Y >& r )
{ {
f_ = boost::phoenix::val( r ); f_ = lambda::make_val( r );
} }
void calls( const functor_type& f ) void calls( const functor_type& f )
@ -95,7 +99,7 @@ namespace detail
template< typename Exception > template< typename Exception >
void throws( Exception e ) void throws( Exception e )
{ {
f_ = boost::phoenix::throw_( e ); f_ = lambda::make_throw( e );
} }
const functor_type& functor() const const functor_type& functor() const
@ -113,9 +117,12 @@ namespace detail
typedef BOOST_DEDUCED_TYPENAME typedef BOOST_DEDUCED_TYPENAME
boost::function< Signature > functor_type; boost::function< Signature > functor_type;
typedef BOOST_DEDUCED_TYPENAME
lambda< Signature > lambda;
public: public:
action() action()
: f_( boost::phoenix::nothing ) : f_( lambda::make_nothing() )
{} {}
void calls( const functor_type& f ) void calls( const functor_type& f )
@ -128,7 +135,7 @@ namespace detail
template< typename Exception > template< typename Exception >
void throws( Exception e ) void throws( Exception e )
{ {
f_ = boost::phoenix::throw_( e ); f_ = lambda::make_throw( e );
} }
const functor_type& functor() const const functor_type& functor() const
@ -146,6 +153,9 @@ namespace detail
typedef BOOST_DEDUCED_TYPENAME typedef BOOST_DEDUCED_TYPENAME
boost::function< Signature > functor_type; boost::function< Signature > functor_type;
typedef BOOST_DEDUCED_TYPENAME
lambda< Signature > lambda;
public: public:
action() action()
: r_() : r_()
@ -153,7 +163,7 @@ namespace detail
{} {}
action( const action& rhs ) action( const action& rhs )
: r_( const_cast< action& >( rhs ).r_.release() ) : r_( const_cast< action& >( rhs ).r_.release() )
, f_( r_.get() ? boost::phoenix::val( boost::ref( r_ ) ) : rhs.f_ ) , f_( r_.get() ? lambda::make_val( boost::ref( r_ ) ) : rhs.f_ )
{} {}
template< typename Value > template< typename Value >
@ -172,7 +182,7 @@ namespace detail
template< typename Exception > template< typename Exception >
void throws( Exception e ) void throws( Exception e )
{ {
f_ = boost::phoenix::throw_( e ); f_ = lambda::make_throw( e );
r_.reset(); r_.reset();
} }
@ -186,19 +196,19 @@ namespace detail
void set( std::auto_ptr< Y > r ) void set( std::auto_ptr< Y > r )
{ {
r_ = r; r_ = r;
f_ = boost::phoenix::val( boost::ref( r_ ) ); f_ = lambda::make_val( boost::ref( r_ ) );
} }
template< typename Y > template< typename Y >
void set( const boost::reference_wrapper< Y >& r ) void set( const boost::reference_wrapper< Y >& r )
{ {
f_ = boost::phoenix::val( r ); f_ = lambda::make_val( r );
r_.reset(); r_.reset();
} }
template< typename Y > template< typename Y >
void set( Y* r ) void set( Y* r )
{ {
r_.reset( r ); r_.reset( r );
f_ = boost::phoenix::val( boost::ref( r_ ) ); f_ = lambda::make_val( boost::ref( r_ ) );
} }
mutable std::auto_ptr< Result > r_; mutable std::auto_ptr< Result > r_;

View file

@ -12,14 +12,18 @@
#include <boost/preprocessor/inc.hpp> #include <boost/preprocessor/inc.hpp>
#ifndef MOCK_MAX_ARGS #ifndef MOCK_MAX_ARGS
# define MOCK_MAX_ARGS 10 # define MOCK_MAX_ARGS 9
#endif #endif
#define MOCK_NUM_ARGS BOOST_PP_INC(MOCK_MAX_ARGS) #define MOCK_NUM_ARGS BOOST_PP_INC(MOCK_MAX_ARGS)
#ifndef PHOENIX_LIMIT #if defined(MOCK_USE_BOOST_PHOENIX) || (MOCK_MAX_ARGS > 9)
# ifndef PHOENIX_LIMIT
# define PHOENIX_LIMIT MOCK_MAX_ARGS # define PHOENIX_LIMIT MOCK_MAX_ARGS
#elif (PHOENIX_LIMIT < MOCK_MAX_ARGS) # elif (PHOENIX_LIMIT < MOCK_MAX_ARGS)
# error "PHOENIX_LIMIT is set too low" # error "PHOENIX_LIMIT is set too low"
# endif
#else
# define MOCK_USE_BOOST_BIND
#endif #endif
#ifndef BOOST_FUNCTION_MAX_ARGS #ifndef BOOST_FUNCTION_MAX_ARGS

View file

@ -0,0 +1,107 @@
//
// Copyright Mathieu Champlon 2011
//
// 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_LAMBDA_HPP_INCLUDED
#define MOCK_LAMBDA_HPP_INCLUDED
#include "config.hpp"
#ifdef MOCK_USE_BOOST_BIND
# include <boost/bind.hpp>
#else
# include <boost/spirit/home/phoenix/statement/throw.hpp>
# include <boost/spirit/home/phoenix/operator/self.hpp>
# include <boost/spirit/home/phoenix/core/nothing.hpp>
#endif
#include <boost/function.hpp>
namespace mock
{
namespace detail
{
#ifdef MOCK_USE_BOOST_BIND
template< typename Signature >
struct lambda
{
typedef BOOST_DEDUCED_TYPENAME
boost::function< Signature > functor_type;
template< typename T >
static functor_type make_val( T t )
{
return boost::bind( &do_identity< T >, t );
}
template< typename T >
static functor_type make_val( boost::reference_wrapper< T > t )
{
return boost::bind( &do_ref_identity< T >, t.get_pointer() );
}
template< typename T >
static functor_type make_throw( T t )
{
return boost::bind( &do_throw< T >, t );
}
static functor_type make_nothing()
{
return boost::bind( &do_nothing );
}
template< typename T >
static T do_identity( T t )
{
return t;
}
template< typename T >
static T& do_ref_identity( T* t )
{
return *t;
}
template< typename T >
static void do_throw( T t )
{
throw t;
}
static void do_nothing()
{
}
};
#else
template< typename Signature >
struct lambda
{
typedef BOOST_DEDUCED_TYPENAME
boost::function< Signature > functor_type;
template< typename T >
static functor_type make_val( T t )
{
return boost::phoenix::val( t );
}
template< typename T >
static functor_type make_val( boost::reference_wrapper< T > t )
{
return *boost::phoenix::val( t.get_pointer() );
}
template< typename T >
static functor_type make_throw( T t )
{
return boost::phoenix::throw_( t );
}
static functor_type make_nothing()
{
return boost::phoenix::nothing;
}
};
#endif
}
}
#endif // #ifndef MOCK_LAMBDA_HPP_INCLUDED

View file

@ -24,7 +24,8 @@ namespace
struct my_custom_mock struct my_custom_mock
{ {
MOCK_METHOD_EXT( my_method, 0, void(), my_method ) MOCK_METHOD_EXT( my_method, 0, void(), my_method )
MOCK_METHOD_EXT( my_method_with_max_number_of_args, MOCK_MAX_ARGS, void( int, int, int, int, int, int, int, int, int, int ), my_method_with_max_number_of_args ) BOOST_STATIC_ASSERT( MOCK_MAX_ARGS == 9 );
MOCK_METHOD_EXT( my_method_with_max_number_of_args, MOCK_MAX_ARGS, void( int, int, int, int, int, int, int, int, int ), my_method_with_max_number_of_args )
}; };
} }