mirror of
https://github.com/boostorg/smart_ptr.git
synced 2025-07-30 04:47:12 +02:00
@ -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
|
||||
|
||||
|
@ -62,5 +62,6 @@ import testing ;
|
||||
[ run esft_constructor_test.cpp ]
|
||||
[ compile-fail auto_ptr_lv_fail.cpp ]
|
||||
[ run atomic_count_test2.cpp ]
|
||||
[ run sp_typeinfo_test.cpp ]
|
||||
;
|
||||
}
|
||||
|
@ -9,6 +9,8 @@
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#include <boost/pointer_cast.hpp>
|
||||
|
||||
#include <boost/shared_ptr.hpp>
|
||||
@ -58,6 +60,8 @@ class derived_derived
|
||||
|
||||
// And now some simple check functions
|
||||
|
||||
#if !defined( BOOST_NO_RTTI )
|
||||
|
||||
template <class BasePtr>
|
||||
bool check_dynamic_pointer_cast(const BasePtr &ptr)
|
||||
{
|
||||
@ -74,6 +78,8 @@ bool check_dynamic_pointer_cast(const BasePtr &ptr)
|
||||
dynamic_cast<derived_derived*>(boost::get_pointer(ptr));
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
template <class BasePtr>
|
||||
bool check_static_pointer_cast(const BasePtr &ptr)
|
||||
{
|
||||
@ -107,7 +113,9 @@ int main()
|
||||
|
||||
boost::shared_ptr<base> ptr(new derived);
|
||||
|
||||
#if !defined( BOOST_NO_RTTI )
|
||||
BOOST_TEST( check_dynamic_pointer_cast( ptr ) );
|
||||
#endif
|
||||
BOOST_TEST( check_static_pointer_cast( ptr ) );
|
||||
BOOST_TEST( check_const_pointer_cast( ptr ) );
|
||||
}
|
||||
@ -117,7 +125,9 @@ int main()
|
||||
|
||||
boost::scoped_ptr<base> ptr(new derived);
|
||||
|
||||
#if !defined( BOOST_NO_RTTI )
|
||||
BOOST_TEST( check_dynamic_pointer_cast( ptr.get() ) );
|
||||
#endif
|
||||
BOOST_TEST( check_static_pointer_cast( ptr.get() ) );
|
||||
BOOST_TEST( check_const_pointer_cast( ptr.get() ) );
|
||||
}
|
||||
|
@ -63,10 +63,12 @@ void test()
|
||||
|
||||
px->f();
|
||||
|
||||
#if !defined( BOOST_NO_RTTI )
|
||||
boost::shared_ptr<Y> py2 = boost::dynamic_pointer_cast<Y>(px);
|
||||
BOOST_TEST(py.get() == py2.get());
|
||||
BOOST_TEST(!(py < py2 || py2 < py));
|
||||
BOOST_TEST(py.use_count() == 3);
|
||||
#endif
|
||||
}
|
||||
catch( boost::bad_weak_ptr const& )
|
||||
{
|
||||
|
@ -188,6 +188,7 @@ int main()
|
||||
test_eq(p, q);
|
||||
}
|
||||
|
||||
#if !defined( BOOST_NO_RTTI )
|
||||
shared_ptr<Y> p3 = dynamic_pointer_cast<Y>(p);
|
||||
shared_ptr<Y> p4 = dynamic_pointer_cast<Y>(p2);
|
||||
|
||||
@ -201,6 +202,7 @@ int main()
|
||||
test_is_Y(p3);
|
||||
test_eq2(p, p3);
|
||||
test_ne2(p2, p4);
|
||||
#endif
|
||||
|
||||
shared_ptr<void> p5(p);
|
||||
|
||||
@ -214,13 +216,17 @@ int main()
|
||||
|
||||
p.reset();
|
||||
p2.reset();
|
||||
#if !defined( BOOST_NO_RTTI )
|
||||
p3.reset();
|
||||
p4.reset();
|
||||
#endif
|
||||
|
||||
test_is_zero(p);
|
||||
test_is_zero(p2);
|
||||
#if !defined( BOOST_NO_RTTI )
|
||||
test_is_zero(p3);
|
||||
test_is_zero(p4);
|
||||
#endif
|
||||
|
||||
BOOST_TEST(p5.use_count() == 1);
|
||||
|
||||
@ -250,6 +256,7 @@ int main()
|
||||
test_is_nonzero(wp2.lock());
|
||||
}
|
||||
|
||||
#if !defined( BOOST_NO_RTTI )
|
||||
weak_ptr<Y> wp3 = dynamic_pointer_cast<Y>(wp2.lock());
|
||||
|
||||
BOOST_TEST(wp3.use_count() == 1);
|
||||
@ -259,12 +266,15 @@ int main()
|
||||
|
||||
BOOST_TEST(wp4.use_count() == 1);
|
||||
test_shared(wp2, wp4);
|
||||
#endif
|
||||
|
||||
wp1 = p2;
|
||||
test_is_zero(wp1.lock());
|
||||
|
||||
#if !defined( BOOST_NO_RTTI )
|
||||
wp1 = p4;
|
||||
wp1 = wp3;
|
||||
#endif
|
||||
wp1 = wp2;
|
||||
|
||||
BOOST_TEST(wp1.use_count() == 1);
|
||||
@ -279,7 +289,9 @@ int main()
|
||||
|
||||
BOOST_TEST(wp1.use_count() == 0);
|
||||
BOOST_TEST(wp2.use_count() == 0);
|
||||
#if !defined( BOOST_NO_RTTI )
|
||||
BOOST_TEST(wp3.use_count() == 0);
|
||||
#endif
|
||||
|
||||
// Test operator< stability for std::set< weak_ptr<> >
|
||||
// Thanks to Joe Gottman for pointing this out
|
||||
|
@ -2462,6 +2462,8 @@ void test()
|
||||
|
||||
} // namespace n_const_cast
|
||||
|
||||
#if !defined( BOOST_NO_RTTI )
|
||||
|
||||
namespace n_dynamic_cast
|
||||
{
|
||||
|
||||
@ -2527,6 +2529,8 @@ void test()
|
||||
|
||||
} // namespace n_dynamic_cast
|
||||
|
||||
#endif
|
||||
|
||||
namespace n_map
|
||||
{
|
||||
|
||||
@ -3200,10 +3204,12 @@ void test()
|
||||
BOOST_TEST(px.get() != 0);
|
||||
BOOST_TEST(py.use_count() == 2);
|
||||
|
||||
#if !defined( BOOST_NO_RTTI )
|
||||
boost::shared_ptr<Y> py2 = boost::dynamic_pointer_cast<Y>(px);
|
||||
BOOST_TEST(py.get() == py2.get());
|
||||
BOOST_TEST(!(py < py2 || py2 < py));
|
||||
BOOST_TEST(py.use_count() == 3);
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace n_spt_shared_from_this
|
||||
@ -3229,7 +3235,9 @@ int main()
|
||||
n_comparison::test();
|
||||
n_static_cast::test();
|
||||
n_const_cast::test();
|
||||
#if !defined( BOOST_NO_RTTI )
|
||||
n_dynamic_cast::test();
|
||||
#endif
|
||||
|
||||
n_map::test();
|
||||
|
||||
|
51
test/sp_typeinfo_test.cpp
Normal file
51
test/sp_typeinfo_test.cpp
Normal 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();
|
||||
}
|
Reference in New Issue
Block a user