Fix sp_typeinfo to match the interface of std::type_info.

[SVN r58127]
This commit is contained in:
Peter Dimov
2009-12-03 20:31:01 +00:00
parent e78efdbb96
commit 577528812a
3 changed files with 102 additions and 4 deletions

View File

@@ -19,20 +19,66 @@
#if defined( BOOST_NO_TYPEID )
#include <boost/current_function.hpp>
#include <functional>
namespace boost
{
namespace detail
{
typedef void* sp_typeinfo;
class sp_typeinfo
{
private:
sp_typeinfo( sp_typeinfo const& );
sp_typeinfo& operator=( sp_typeinfo const& );
char const * name_;
public:
explicit sp_typeinfo( char const * name ): name_( name )
{
}
bool operator==( sp_typeinfo const& rhs ) const
{
return this == &rhs;
}
bool operator!=( sp_typeinfo const& rhs ) const
{
return this != &rhs;
}
bool before( sp_typeinfo const& rhs ) const
{
return std::less< sp_typeinfo const* >()( this, &rhs );
}
char const* name() const
{
return name_;
}
};
template<class T> struct sp_typeid_
{
static char v_;
static sp_typeinfo ti_;
static char const * name()
{
return BOOST_CURRENT_FUNCTION;
}
};
template<class T> char sp_typeid_< T >::v_;
template<class T> sp_typeinfo sp_typeid_< T >::ti_( sp_typeid_< T >::name() );
template<class T> struct sp_typeid_< T & >: sp_typeid_< T >
{
};
template<class T> struct sp_typeid_< T const >: sp_typeid_< T >
{
@@ -50,7 +96,7 @@ template<class T> struct sp_typeid_< T const volatile >: sp_typeid_< T >
} // namespace boost
#define BOOST_SP_TYPEID(T) (&boost::detail::sp_typeid_<T>::v_)
#define BOOST_SP_TYPEID(T) (boost::detail::sp_typeid_<T>::ti_)
#else

View File

@@ -63,5 +63,6 @@ import testing ;
[ run enable_shared_from_raw_test.cpp ]
[ compile-fail auto_ptr_lv_fail.cpp ]
[ run atomic_count_test2.cpp ]
[ run sp_typeinfo_test.cpp ]
;
}

51
test/sp_typeinfo_test.cpp Normal file
View File

@@ -0,0 +1,51 @@
//
// sp_typeinfo_test.cpp
//
// Copyright (c) 2009 Peter Dimov
//
// 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
//
#include <boost/detail/sp_typeinfo.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <iostream>
int main()
{
BOOST_TEST( BOOST_SP_TYPEID( int ) == BOOST_SP_TYPEID( int ) );
BOOST_TEST( BOOST_SP_TYPEID( int ) != BOOST_SP_TYPEID( long ) );
BOOST_TEST( BOOST_SP_TYPEID( int ) != BOOST_SP_TYPEID( void ) );
boost::detail::sp_typeinfo const & ti = BOOST_SP_TYPEID( int );
boost::detail::sp_typeinfo const * pti = &BOOST_SP_TYPEID( int );
BOOST_TEST( *pti == ti );
BOOST_TEST( ti == ti );
BOOST_TEST( !( ti != ti ) );
BOOST_TEST( !ti.before( ti ) );
char const * nti = ti.name();
std::cout << nti << std::endl;
boost::detail::sp_typeinfo const & tv = BOOST_SP_TYPEID( void );
boost::detail::sp_typeinfo const * ptv = &BOOST_SP_TYPEID( void );
BOOST_TEST( *ptv == tv );
BOOST_TEST( tv == tv );
BOOST_TEST( !( tv != tv ) );
BOOST_TEST( !tv.before( tv ) );
char const * ntv = tv.name();
std::cout << ntv << std::endl;
BOOST_TEST( ti != tv );
BOOST_TEST( !( ti == tv ) );
BOOST_TEST( ti.before( tv ) != tv.before( ti ) );
return boost::report_errors();
}