Most functors as constraints are detected automatically

git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@6 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
mat007 2009-08-28 20:22:26 +00:00
parent 22d9120dc8
commit 0d0b921d83
6 changed files with 135 additions and 5 deletions

View file

@ -11,8 +11,10 @@
#include "placeholder.hpp"
#include "constraint.hpp"
#include "is_functor.hpp"
#include "format.hpp"
#include <boost/function.hpp>
#include <boost/utility/enable_if.hpp>
#include <stdexcept>
#include <ostream>
@ -27,15 +29,28 @@ namespace detail
boost::function< bool( Arg ) > functor_type;
public:
template< typename F >
explicit check( const F& f,
BOOST_DEDUCED_TYPENAME boost::enable_if<
BOOST_DEDUCED_TYPENAME detail::is_functor< F >::type
>::type* = 0 )
: functor_( f )
, desc_ ( "?" )
{
if( !functor_ )
std::invalid_argument( "invalid functor" );
}
template< typename T >
explicit check( const T& t )
explicit check( const T& t,
BOOST_DEDUCED_TYPENAME boost::disable_if<
BOOST_DEDUCED_TYPENAME detail::is_functor< T >::type
>::type* = 0 )
: functor_( equal( t ).functor_ )
, desc_ ( detail::format( t ) )
{
if( !functor_ )
std::invalid_argument( "invalid functor" );
}
template< typename Constraint >
explicit check( const placeholder< Constraint >& c )
: functor_( c.functor_ )

View file

@ -0,0 +1,48 @@
//
// Copyright Mathieu Champlon 2009
//
// 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_IS_FUNCTOR_HPP_INCLUDED
#define MOCK_IS_FUNCTOR_HPP_INCLUDED
#include <boost/function_types/is_callable_builtin.hpp>
namespace mock
{
namespace detail
{
typedef char true_type[2];
typedef char false_type[1];
template< typename T >
true_type& has_result_type_helper( T*, BOOST_DEDUCED_TYPENAME T::result_type* = 0 ) {}
template< typename T >
false_type& has_result_type_helper( T, ... ) {}
template< typename T >
struct has_result_type
{
static T* t();
enum { value = sizeof( has_result_type_helper( t() ) ) == sizeof( true_type ) };
};
template< typename T, bool B = has_result_type< T >::value >
struct is_functor
{
typedef BOOST_DEDUCED_TYPENAME boost::function_types::is_callable_builtin< T >::type type;
enum { value = BOOST_DEDUCED_TYPENAME type::value };
};
template< typename T >
struct is_functor< T, true >
{
typedef boost::true_type type;
enum { value = true };
};
}
}
#endif // #ifndef MOCK_IS_FUNCTOR_HPP_INCLUDED