Ticket #3123: serialization.diff
File serialization.diff, 6.0 KB (added by , 13 years ago) |
---|
-
boost/serialization/shared_ptr.hpp
26 26 #include <boost/detail/workaround.hpp> 27 27 #include <boost/shared_ptr.hpp> 28 28 29 #include <boost/type_traits/remove_const.hpp> 30 29 31 #include <boost/serialization/split_free.hpp> 30 32 #include <boost/serialization/nvp.hpp> 31 33 #include <boost/serialization/version.hpp> … … 55 57 }; 56 58 // don't track shared pointers 57 59 template<class T> 58 struct tracking_level< ::boost::shared_ptr<T> > { 60 struct tracking_level< ::boost::shared_ptr<T> > { 59 61 typedef mpl::integral_c_tag tag; 60 62 #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206)) 61 63 typedef BOOST_DEDUCED_TYPENAME mpl::int_< ::boost::serialization::track_never> type; … … 104 106 ar << boost::serialization::make_nvp("px", t_ptr); 105 107 } 106 108 107 template<class Archive, class T>109 template<class Archive, class MaybeConstT> 108 110 inline void load( 109 111 Archive & ar, 110 boost::shared_ptr< T> &t,112 boost::shared_ptr<MaybeConstT> &t, 111 113 const unsigned int file_version 112 114 ){ 113 115 // The most common cause of trapping here would be serializing 114 116 // something like shared_ptr<int>. This occurs because int 115 117 // is never tracked by default. Wrap int in a trackable type 116 BOOST_STATIC_ASSERT((tracking_level<T>::value != track_never)); 118 BOOST_STATIC_ASSERT((tracking_level<MaybeConstT>::value != track_never)); 119 120 typedef typename boost::remove_const<MaybeConstT>::type T; 121 117 122 T* r; 118 123 #ifdef BOOST_SERIALIZATION_SHARED_PTR_132_HPP 119 124 if(file_version < 1){ … … 130 135 ar.append(sp); 131 136 r = sp.get(); 132 137 } 133 else 138 else 134 139 #endif 135 140 { 136 141 ar >> boost::serialization::make_nvp("px", r); 137 142 } 138 ar.reset(t,r); 143 boost::shared_ptr<T> nct; 144 ar.reset(nct,r); 145 t = nct; 139 146 } 140 147 141 148 template<class Archive, class T> -
libs/serialization/test/test_shared_ptr.cpp
1 1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 2 2 // test_shared_ptr.cpp 3 3 4 // (C) Copyright 2002 Robert Ramey- http://www.rrsd.com - David Tonge . 4 // (C) Copyright 2002 Robert Ramey- http://www.rrsd.com - David Tonge . 5 5 // Use, modification and distribution is subject to the Boost Software 6 6 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 7 7 // http://www.boost.org/LICENSE_1_0.txt) … … 14 14 15 15 #include <boost/config.hpp> 16 16 #if defined(BOOST_NO_STDC_NAMESPACE) 17 namespace std{ 17 namespace std{ 18 18 using ::remove; 19 19 } 20 20 #endif … … 104 104 } 105 105 106 106 void save2( 107 const char * testfile, 108 const boost::shared_ptr<A>& first, 107 const char * testfile, 108 const boost::shared_ptr<A>& first, 109 109 const boost::shared_ptr<A>& second 110 110 ){ 111 111 test_ostream os(testfile, TEST_STREAM_FLAGS); … … 114 114 oa << BOOST_SERIALIZATION_NVP(second); 115 115 } 116 116 117 void save2c( 118 const char * testfile, 119 const boost::shared_ptr<const A>& first, 120 const boost::shared_ptr<const A>& second 121 ){ 122 test_ostream os(testfile, TEST_STREAM_FLAGS); 123 test_oarchive oa(os, TEST_ARCHIVE_FLAGS); 124 oa << BOOST_SERIALIZATION_NVP(first); 125 oa << BOOST_SERIALIZATION_NVP(second); 126 } 127 117 128 void load2( 118 const char * testfile, 119 boost::shared_ptr<A>& first, 129 const char * testfile, 130 boost::shared_ptr<A>& first, 120 131 boost::shared_ptr<A>& second) 121 132 { 122 133 test_istream is(testfile, TEST_STREAM_FLAGS); … … 125 136 ia >> BOOST_SERIALIZATION_NVP(second); 126 137 } 127 138 139 void load2c( 140 const char * testfile, 141 boost::shared_ptr<const A>& first, 142 boost::shared_ptr<const A>& second) 143 { 144 test_istream is(testfile, TEST_STREAM_FLAGS); 145 test_iarchive ia(is, TEST_ARCHIVE_FLAGS); 146 ia >> BOOST_SERIALIZATION_NVP(first); 147 ia >> BOOST_SERIALIZATION_NVP(second); 148 } 149 128 150 // Run tests by serializing two shared_ptrs into an archive, 129 151 // clearing them (deleting the objects) and then reloading the 130 152 // objects back from an archive. … … 145 167 std::remove(testfile); 146 168 } 147 169 170 void save_and_load2c(boost::shared_ptr<const A>& first, boost::shared_ptr<const A>& second) 171 { 172 const char * testfile = boost::archive::tmpnam(NULL); 173 BOOST_REQUIRE(NULL != testfile); 174 175 save2c(testfile, first, second); 176 177 // Clear the pointers, thereby destroying the objects they contain 178 first.reset(); 179 second.reset(); 180 181 load2c(testfile, first, second); 182 183 BOOST_CHECK(first && first == second); 184 std::remove(testfile); 185 } 186 148 187 void save3( 149 const char * testfile, 150 boost::shared_ptr<A>& first, 188 const char * testfile, 189 boost::shared_ptr<A>& first, 151 190 boost::shared_ptr<A>& second, 152 191 boost::weak_ptr<A>& third 153 192 ){ … … 159 198 } 160 199 161 200 void load3( 162 const char * testfile, 163 boost::shared_ptr<A>& first, 201 const char * testfile, 202 boost::shared_ptr<A>& first, 164 203 boost::shared_ptr<A>& second, 165 204 boost::weak_ptr<A>& third 166 205 ){ … … 175 214 } 176 215 177 216 void save_and_load3( 178 boost::shared_ptr<A>& first, 217 boost::shared_ptr<A>& first, 179 218 boost::shared_ptr<A>& second, 180 219 boost::weak_ptr<A>& third 181 220 ){ … … 214 253 boost::shared_ptr<A> spa1 = spa; 215 254 save_and_load2(spa, spa1); 216 255 256 // Try to save and load pointers to As, to a text archive 257 boost::shared_ptr<const A> spca = boost::shared_ptr<A>(new A); 258 boost::shared_ptr<const A> spca1 = spca; 259 save_and_load2c(spca, spca1); 260 217 261 // test a weak pointer 218 262 spa = boost::shared_ptr<A>(new A); 219 263 spa1 = spa; 220 264 boost::weak_ptr<A> wp = spa; 221 265 save_and_load3(spa, spa1, wp); 222 266 223 267 // Try to save and load pointers to Bs, to a text archive 224 268 spa = boost::shared_ptr<A>(new B); 225 269 spa1 = spa;