Document and improve is_functor

This commit is contained in:
Alexander Grund 2020-07-12 13:04:14 +02:00
parent 446e70be53
commit 898a5a4524
No known key found for this signature in database
GPG key ID: AA48A0760367A42B
4 changed files with 18 additions and 19 deletions

View file

@ -30,7 +30,6 @@
#include <boost/preprocessor/comparison/greater.hpp>
#include <boost/test/utils/basic_cstring/basic_cstring.hpp>
#include <boost/test/utils/lazy_ostream.hpp>
#include <boost/call_traits.hpp>
#include <boost/optional.hpp>
#include <ostream>
#include <vector>

View file

@ -17,15 +17,13 @@ namespace mock
{
namespace detail
{
template< typename F, typename P, class = void >
struct is_callable : std::false_type
/// Trait to return true if F is a functor that can be called with a single argument Arg
template< typename F, typename Arg, class = void >
struct is_functor : std::false_type
{};
template< typename F, typename P >
struct is_callable< F, P, boost::void_t<decltype( std::declval<F>()( std::declval<P>() ) )> >: std::true_type
template< typename F, typename Arg >
struct is_functor< F, Arg, boost::void_t<decltype( std::declval<F>()( std::declval<Arg>() ) )> >: std::true_type
{};
template< typename T, typename P >
using is_functor = is_callable< T, P >;
}
} // mock

View file

@ -13,7 +13,6 @@
#include "stream.hpp"
#include "format.hpp"
#include <boost/detail/container_fwd.hpp>
#include <boost/function_types/is_callable_builtin.hpp>
#include <boost/none.hpp>
#include <memory>
#include <type_traits>
@ -51,6 +50,15 @@ namespace detail
s << (it == begin ? "" : ",") << mock::format( *it );
s << ')';
}
template<typename T>
struct is_callable_impl: std::false_type
{};
template<typename R, typename... Args>
struct is_callable_impl<R(Args...)>: std::true_type
{};
template<typename T>
struct is_callable: is_callable_impl< std::remove_cv_t<T> >
{};
}
#ifdef MOCK_AUTO_PTR
@ -182,19 +190,13 @@ namespace detail
}
template< typename T >
std::enable_if_t<
boost::function_types::is_callable_builtin< T >::value,
stream&
>
std::enable_if_t< detail::is_callable< T >::value, stream& >
operator<<( stream& s, T* )
{
return s << '?';
}
template< typename T >
std::enable_if_t<
!boost::function_types::is_callable_builtin< T >::value,
stream&
>
std::enable_if_t< !detail::is_callable< T >::value, stream& >
operator<<( stream& s, T* t )
{
*s.s_ << t;

View file

@ -28,12 +28,12 @@ namespace
template< typename T >
void is_functor( T )
{
static_assert( mock::detail::is_functor< T, int >::value, "Should be functor");
static_assert( mock::detail::is_functor< T, int >::value, "Should be a functor taking an int");
}
template< typename T >
void is_not_functor( T )
{
static_assert( !mock::detail::is_functor< T, int >::value, "Should not be a functor" );
static_assert( !mock::detail::is_functor< T, int >::value, "Should not be a functor taking an int" );
}
void f0() {}