Replace Boost.TypeInfo

This commit is contained in:
Alexander Grund 2020-07-11 14:58:37 +02:00
parent 6702d68940
commit 9af9f0e2ea
No known key found for this signature in database
GPG key ID: AA48A0760367A42B
4 changed files with 24 additions and 26 deletions

View file

@ -15,15 +15,6 @@
#include <boost/algorithm/string/erase.hpp> #include <boost/algorithm/string/erase.hpp>
#include <boost/algorithm/string/trim.hpp> #include <boost/algorithm/string/trim.hpp>
#include <boost/version.hpp> #include <boost/version.hpp>
#if BOOST_VERSION >= 107000
# include <boost/core/typeinfo.hpp>
# define MOCK_TYPEID( t ) BOOST_CORE_TYPEID(t)
# define MOCK_TYPEINFO boost::core::typeinfo
#else
# include <boost/detail/sp_typeinfo.hpp>
# define MOCK_TYPEID( t ) BOOST_SP_TYPEID(t)
# define MOCK_TYPEINFO boost::detail::sp_typeinfo
#endif
#include <stdexcept> #include <stdexcept>
#include <memory> #include <memory>
#include <typeinfo> #include <typeinfo>
@ -33,8 +24,6 @@
#include <cstdlib> #include <cstdlib>
#endif #endif
#define MOCK_TYPE_NAME( t ) mock::detail::type_name( MOCK_TYPEID(t) )
namespace mock namespace mock
{ {
namespace detail namespace detail
@ -42,7 +31,7 @@ namespace detail
class type_name class type_name
{ {
public: public:
explicit type_name( const MOCK_TYPEINFO& info ) explicit type_name( const std::type_info& info )
: info_( &info ) : info_( &info )
{} {}
friend std::ostream& operator<<( std::ostream& s, const type_name& t ) friend std::ostream& operator<<( std::ostream& s, const type_name& t )
@ -51,8 +40,7 @@ namespace detail
return s; return s;
} }
private: private:
void serialize( std::ostream& s, static void serialize( std::ostream& s, const std::type_info& info )
const MOCK_TYPEINFO& info ) const
{ {
const char* name = info.name(); const char* name = info.name();
#ifdef __GNUC__ #ifdef __GNUC__
@ -69,7 +57,7 @@ namespace detail
typedef std::string::size_type size_type; typedef std::string::size_type size_type;
void serialize( std::ostream& s, std::string name ) const static void serialize( std::ostream& s, std::string name )
{ {
const size_type nm = rfind( name, ':' ) + 1; const size_type nm = rfind( name, ':' ) + 1;
const size_type tpl = name.find( '<', nm ); const size_type tpl = name.find( '<', nm );
@ -80,7 +68,7 @@ namespace detail
list( s, name.substr( tpl + 1, name.rfind( '>' ) - tpl - 1 ) ); list( s, name.substr( tpl + 1, name.rfind( '>' ) - tpl - 1 ) );
s << '>'; s << '>';
} }
void list( std::ostream& s, const std::string& name ) const static void list( std::ostream& s, const std::string& name )
{ {
const size_type comma = rfind( name, ',' ); const size_type comma = rfind( name, ',' );
if( comma != std::string::npos ) if( comma != std::string::npos )
@ -90,7 +78,7 @@ namespace detail
} }
serialize( s, name.substr( comma + 1 ) ); serialize( s, name.substr( comma + 1 ) );
} }
std::string clean( std::string name ) const static std::string clean( std::string name )
{ {
boost::algorithm::trim( name ); boost::algorithm::trim( name );
boost::algorithm::erase_all( name, "class " ); boost::algorithm::erase_all( name, "class " );
@ -102,7 +90,7 @@ namespace detail
boost::algorithm::replace_all( name, "* ", "*" ); boost::algorithm::replace_all( name, "* ", "*" );
return name; return name;
} }
size_type rfind( const std::string& name, char c ) const static size_type rfind( const std::string& name, char c )
{ {
size_type count = 0; size_type count = 0;
for( size_type i = name.size() - 1; i > 0; --i ) for( size_type i = name.size() - 1; i > 0; --i )
@ -117,8 +105,18 @@ namespace detail
return std::string::npos; return std::string::npos;
} }
const MOCK_TYPEINFO* info_; const std::type_info* info_;
}; };
template< typename T >
type_name make_type_name()
{
return type_name( typeid(T) );
}
template< typename T >
type_name make_type_name( const T& )
{
return type_name( typeid(T) );
}
} }
} // mock } // mock

View file

@ -63,7 +63,7 @@
{ \ { \
mock::detail::configure( *this, t##_mock_, \ mock::detail::configure( *this, t##_mock_, \
instance.substr( 0, instance.rfind( BOOST_PP_STRINGIZE(t) ) ), \ instance.substr( 0, instance.rfind( BOOST_PP_STRINGIZE(t) ) ), \
MOCK_TYPE_NAME(*this), \ mock::detail::make_type_name(*this), \
BOOST_PP_STRINGIZE(t) ); \ BOOST_PP_STRINGIZE(t) ); \
return t##_mock_; \ return t##_mock_; \
} }

View file

@ -13,9 +13,9 @@
namespace namespace
{ {
template< typename T > template< typename T >
std::string to_string( const T& ) std::string to_string( const T& t)
{ {
return boost::lexical_cast< std::string >( MOCK_TYPE_NAME(T) ); return boost::lexical_cast< std::string >( mock::detail::make_type_name(t) );
} }
} }
@ -111,7 +111,7 @@ BOOST_AUTO_TEST_CASE( name_of_type_in_unnamed_inner_namespace_is_extracted )
BOOST_AUTO_TEST_CASE( name_of_local_type_is_extracted ) BOOST_AUTO_TEST_CASE( name_of_local_type_is_extracted )
{ {
struct my_local_type {}; struct my_local_type {};
BOOST_CHECK_EQUAL( "my_local_type", boost::lexical_cast< std::string >( MOCK_TYPE_NAME(my_local_type) ) ); BOOST_CHECK_EQUAL( "my_local_type", boost::lexical_cast< std::string >( mock::detail::make_type_name<my_local_type>() ) );
} }
namespace namespace

View file

@ -33,7 +33,7 @@ namespace
{ {
fixture() fixture()
{ {
mock::detail::configure( o, e, "instance", MOCK_TYPE_NAME(o), "name" ); mock::detail::configure( o, e, "instance", mock::detail::make_type_name(o), "name" );
} }
object o; object o;
mock::detail::function< void() > e; mock::detail::function< void() > e;
@ -73,7 +73,7 @@ BOOST_FIXTURE_TEST_CASE( an_object_is_assignable_by_sharing_its_state, mock_erro
mock::detail::function< void() > e; mock::detail::function< void() > e;
{ {
object o2; object o2;
mock::detail::configure( o2, e, "instance", MOCK_TYPE_NAME(o2), "name" ); mock::detail::configure( o2, e, "instance", mock::detail::make_type_name(o2), "name" );
e.expect().once(); e.expect().once();
o1 = o2; o1 = o2;
CHECK_ERROR( CHECK_ERROR(
@ -93,7 +93,7 @@ BOOST_FIXTURE_TEST_CASE( an_object_is_copiable_by_sharing_its_state, mock_error_
auto o2 = std::make_unique<object>(); auto o2 = std::make_unique<object>();
const object o1( *o2 ); const object o1( *o2 );
mock::detail::function< void() > e; mock::detail::function< void() > e;
mock::detail::configure( *o2, e, "instance", MOCK_TYPE_NAME(*o2), "name" ); mock::detail::configure( *o2, e, "instance", mock::detail::make_type_name(*o2), "name" );
e.expect().once(); e.expect().once();
CHECK_ERROR( CHECK_ERROR(
BOOST_CHECK( ! mock::verify( *o2 ) ), BOOST_CHECK( ! mock::verify( *o2 ) ),