From ba349679f3957511d5188af9bf63cdf15bdf3f60 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sun, 6 Dec 2009 17:50:28 +0000 Subject: [PATCH] Merge [58123], [58127], [58128] to release. Fixes #3666. [SVN r58195] --- include/boost/detail/sp_typeinfo.hpp | 54 +++++++++++++++++++++++++--- test/Jamfile.v2 | 1 + test/pointer_cast_test.cpp | 10 ++++++ test/shared_from_this_test.cpp | 2 ++ test/shared_ptr_basic_test.cpp | 12 +++++++ test/shared_ptr_test.cpp | 8 +++++ test/sp_typeinfo_test.cpp | 51 ++++++++++++++++++++++++++ 7 files changed, 134 insertions(+), 4 deletions(-) create mode 100644 test/sp_typeinfo_test.cpp diff --git a/include/boost/detail/sp_typeinfo.hpp b/include/boost/detail/sp_typeinfo.hpp index e78c943..636fe27 100644 --- a/include/boost/detail/sp_typeinfo.hpp +++ b/include/boost/detail/sp_typeinfo.hpp @@ -19,20 +19,66 @@ #if defined( BOOST_NO_TYPEID ) +#include +#include + 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 struct sp_typeid_ { - static char v_; + static sp_typeinfo ti_; + + static char const * name() + { + return BOOST_CURRENT_FUNCTION; + } }; -template char sp_typeid_< T >::v_; +template sp_typeinfo sp_typeid_< T >::ti_( sp_typeid_< T >::name() ); + +template struct sp_typeid_< T & >: sp_typeid_< T > +{ +}; template struct sp_typeid_< T const >: sp_typeid_< T > { @@ -50,7 +96,7 @@ template struct sp_typeid_< T const volatile >: sp_typeid_< T > } // namespace boost -#define BOOST_SP_TYPEID(T) (&boost::detail::sp_typeid_::v_) +#define BOOST_SP_TYPEID(T) (boost::detail::sp_typeid_::ti_) #else diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index d30f1ab..056b011 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -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 ] ; } diff --git a/test/pointer_cast_test.cpp b/test/pointer_cast_test.cpp index 6e4620c..bd25dd1 100644 --- a/test/pointer_cast_test.cpp +++ b/test/pointer_cast_test.cpp @@ -9,6 +9,8 @@ // http://www.boost.org/LICENSE_1_0.txt) // +#include + #include #include @@ -58,6 +60,8 @@ class derived_derived // And now some simple check functions +#if !defined( BOOST_NO_RTTI ) + template bool check_dynamic_pointer_cast(const BasePtr &ptr) { @@ -74,6 +78,8 @@ bool check_dynamic_pointer_cast(const BasePtr &ptr) dynamic_cast(boost::get_pointer(ptr)); } +#endif + template bool check_static_pointer_cast(const BasePtr &ptr) { @@ -107,7 +113,9 @@ int main() boost::shared_ptr 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 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() ) ); } diff --git a/test/shared_from_this_test.cpp b/test/shared_from_this_test.cpp index 68d6098..b3d7838 100644 --- a/test/shared_from_this_test.cpp +++ b/test/shared_from_this_test.cpp @@ -63,10 +63,12 @@ void test() px->f(); +#if !defined( BOOST_NO_RTTI ) boost::shared_ptr py2 = boost::dynamic_pointer_cast(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& ) { diff --git a/test/shared_ptr_basic_test.cpp b/test/shared_ptr_basic_test.cpp index f699848..4d4179e 100644 --- a/test/shared_ptr_basic_test.cpp +++ b/test/shared_ptr_basic_test.cpp @@ -188,6 +188,7 @@ int main() test_eq(p, q); } +#if !defined( BOOST_NO_RTTI ) shared_ptr p3 = dynamic_pointer_cast(p); shared_ptr p4 = dynamic_pointer_cast(p2); @@ -201,6 +202,7 @@ int main() test_is_Y(p3); test_eq2(p, p3); test_ne2(p2, p4); +#endif shared_ptr 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 wp3 = dynamic_pointer_cast(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 diff --git a/test/shared_ptr_test.cpp b/test/shared_ptr_test.cpp index f697192..60ed906 100644 --- a/test/shared_ptr_test.cpp +++ b/test/shared_ptr_test.cpp @@ -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 py2 = boost::dynamic_pointer_cast(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(); diff --git a/test/sp_typeinfo_test.cpp b/test/sp_typeinfo_test.cpp new file mode 100644 index 0000000..c47d04e --- /dev/null +++ b/test/sp_typeinfo_test.cpp @@ -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 +#include +#include + +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(); +}