Index: boost/serialization/shared_ptr.hpp =================================================================== --- boost/serialization/shared_ptr.hpp (revision 19280) +++ boost/serialization/shared_ptr.hpp (revision 19281) @@ -26,6 +26,8 @@ #include #include +#include + #include #include #include @@ -55,7 +57,7 @@ }; // don't track shared pointers template - struct tracking_level< ::boost::shared_ptr > { + struct tracking_level< ::boost::shared_ptr > { typedef mpl::integral_c_tag tag; #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206)) typedef BOOST_DEDUCED_TYPENAME mpl::int_< ::boost::serialization::track_never> type; @@ -104,16 +106,19 @@ ar << boost::serialization::make_nvp("px", t_ptr); } -template +template inline void load( Archive & ar, - boost::shared_ptr &t, + boost::shared_ptr &t, const unsigned int file_version ){ // The most common cause of trapping here would be serializing // something like shared_ptr. This occurs because int // is never tracked by default. Wrap int in a trackable type - BOOST_STATIC_ASSERT((tracking_level::value != track_never)); + BOOST_STATIC_ASSERT((tracking_level::value != track_never)); + + typedef typename boost::remove_const::type T; + T* r; #ifdef BOOST_SERIALIZATION_SHARED_PTR_132_HPP if(file_version < 1){ @@ -130,12 +135,14 @@ ar.append(sp); r = sp.get(); } - else + else #endif { ar >> boost::serialization::make_nvp("px", r); } - ar.reset(t,r); + boost::shared_ptr nct; + ar.reset(nct,r); + t = nct; } template Index: libs/serialization/test/test_shared_ptr.cpp =================================================================== --- libs/serialization/test/test_shared_ptr.cpp (revision 19280) +++ libs/serialization/test/test_shared_ptr.cpp (revision 19281) @@ -1,7 +1,7 @@ /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 // test_shared_ptr.cpp -// (C) Copyright 2002 Robert Ramey- http://www.rrsd.com - David Tonge . +// (C) Copyright 2002 Robert Ramey- http://www.rrsd.com - David Tonge . // Use, modification and distribution is subject to 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) @@ -14,7 +14,7 @@ #include #if defined(BOOST_NO_STDC_NAMESPACE) -namespace std{ +namespace std{ using ::remove; } #endif @@ -104,8 +104,8 @@ } void save2( - const char * testfile, - const boost::shared_ptr& first, + const char * testfile, + const boost::shared_ptr& first, const boost::shared_ptr& second ){ test_ostream os(testfile, TEST_STREAM_FLAGS); @@ -114,9 +114,20 @@ oa << BOOST_SERIALIZATION_NVP(second); } +void save2c( + const char * testfile, + const boost::shared_ptr& first, + const boost::shared_ptr& second +){ + test_ostream os(testfile, TEST_STREAM_FLAGS); + test_oarchive oa(os, TEST_ARCHIVE_FLAGS); + oa << BOOST_SERIALIZATION_NVP(first); + oa << BOOST_SERIALIZATION_NVP(second); +} + void load2( - const char * testfile, - boost::shared_ptr& first, + const char * testfile, + boost::shared_ptr& first, boost::shared_ptr& second) { test_istream is(testfile, TEST_STREAM_FLAGS); @@ -125,6 +136,17 @@ ia >> BOOST_SERIALIZATION_NVP(second); } +void load2c( + const char * testfile, + boost::shared_ptr& first, + boost::shared_ptr& second) +{ + test_istream is(testfile, TEST_STREAM_FLAGS); + test_iarchive ia(is, TEST_ARCHIVE_FLAGS); + ia >> BOOST_SERIALIZATION_NVP(first); + ia >> BOOST_SERIALIZATION_NVP(second); +} + // Run tests by serializing two shared_ptrs into an archive, // clearing them (deleting the objects) and then reloading the // objects back from an archive. @@ -145,9 +167,26 @@ std::remove(testfile); } +void save_and_load2c(boost::shared_ptr& first, boost::shared_ptr& second) +{ + const char * testfile = boost::archive::tmpnam(NULL); + BOOST_REQUIRE(NULL != testfile); + + save2c(testfile, first, second); + + // Clear the pointers, thereby destroying the objects they contain + first.reset(); + second.reset(); + + load2c(testfile, first, second); + + BOOST_CHECK(first && first == second); + std::remove(testfile); +} + void save3( - const char * testfile, - boost::shared_ptr& first, + const char * testfile, + boost::shared_ptr& first, boost::shared_ptr& second, boost::weak_ptr& third ){ @@ -159,8 +198,8 @@ } void load3( - const char * testfile, - boost::shared_ptr& first, + const char * testfile, + boost::shared_ptr& first, boost::shared_ptr& second, boost::weak_ptr& third ){ @@ -175,7 +214,7 @@ } void save_and_load3( - boost::shared_ptr& first, + boost::shared_ptr& first, boost::shared_ptr& second, boost::weak_ptr& third ){ @@ -214,12 +253,17 @@ boost::shared_ptr spa1 = spa; save_and_load2(spa, spa1); + // Try to save and load pointers to As, to a text archive + boost::shared_ptr spca = boost::shared_ptr(new A); + boost::shared_ptr spca1 = spca; + save_and_load2c(spca, spca1); + // test a weak pointer spa = boost::shared_ptr(new A); spa1 = spa; boost::weak_ptr wp = spa; save_and_load3(spa, spa1, wp); - + // Try to save and load pointers to Bs, to a text archive spa = boost::shared_ptr(new B); spa1 = spa;