Ticket #3123: serialization.diff

File serialization.diff, 6.0 KB (added by Sohail Somani, 13 years ago)

Patch to support loading and saving const shared_ptr

  • boost/serialization/shared_ptr.hpp

     
    2626#include <boost/detail/workaround.hpp>
    2727#include <boost/shared_ptr.hpp>
    2828
     29#include <boost/type_traits/remove_const.hpp>
     30
    2931#include <boost/serialization/split_free.hpp>
    3032#include <boost/serialization/nvp.hpp>
    3133#include <boost/serialization/version.hpp>
     
    5557        };
    5658        // don't track shared pointers
    5759        template<class T>
    58         struct tracking_level< ::boost::shared_ptr<T> > { 
     60        struct tracking_level< ::boost::shared_ptr<T> > {
    5961            typedef mpl::integral_c_tag tag;
    6062            #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206))
    6163            typedef BOOST_DEDUCED_TYPENAME mpl::int_< ::boost::serialization::track_never> type;
     
    104106    ar << boost::serialization::make_nvp("px", t_ptr);
    105107}
    106108
    107 template<class Archive, class T>
     109template<class Archive, class MaybeConstT>
    108110inline void load(
    109111    Archive & ar,
    110     boost::shared_ptr<T> &t,
     112    boost::shared_ptr<MaybeConstT> &t,
    111113    const unsigned int file_version
    112114){
    113115    // The most common cause of trapping here would be serializing
    114116    // something like shared_ptr<int>.  This occurs because int
    115117    // 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
    117122    T* r;
    118123    #ifdef BOOST_SERIALIZATION_SHARED_PTR_132_HPP
    119124    if(file_version < 1){
     
    130135        ar.append(sp);
    131136        r = sp.get();
    132137    }
    133     else   
     138    else
    134139    #endif
    135140    {
    136141        ar >> boost::serialization::make_nvp("px", r);
    137142    }
    138     ar.reset(t,r);
     143    boost::shared_ptr<T> nct;
     144    ar.reset(nct,r);
     145    t = nct;
    139146}
    140147
    141148template<class Archive, class T>
  • libs/serialization/test/test_shared_ptr.cpp

     
    11/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
    22// test_shared_ptr.cpp
    33
    4 // (C) Copyright 2002 Robert Ramey- http://www.rrsd.com - David Tonge  . 
     4// (C) Copyright 2002 Robert Ramey- http://www.rrsd.com - David Tonge  .
    55// Use, modification and distribution is subject to the Boost Software
    66// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
    77// http://www.boost.org/LICENSE_1_0.txt)
     
    1414
    1515#include <boost/config.hpp>
    1616#if defined(BOOST_NO_STDC_NAMESPACE)
    17 namespace std{ 
     17namespace std{
    1818    using ::remove;
    1919}
    2020#endif
     
    104104}
    105105
    106106void save2(
    107     const char * testfile, 
    108     const boost::shared_ptr<A>& first, 
     107    const char * testfile,
     108    const boost::shared_ptr<A>& first,
    109109    const boost::shared_ptr<A>& second
    110110){
    111111    test_ostream os(testfile, TEST_STREAM_FLAGS);
     
    114114    oa << BOOST_SERIALIZATION_NVP(second);
    115115}
    116116
     117void 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
    117128void load2(
    118     const char * testfile, 
    119     boost::shared_ptr<A>& first, 
     129    const char * testfile,
     130    boost::shared_ptr<A>& first,
    120131    boost::shared_ptr<A>& second)
    121132{
    122133    test_istream is(testfile, TEST_STREAM_FLAGS);
     
    125136    ia >> BOOST_SERIALIZATION_NVP(second);
    126137}
    127138
     139void 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
    128150// Run tests by serializing two shared_ptrs into an archive,
    129151// clearing them (deleting the objects) and then reloading the
    130152// objects back from an archive.
     
    145167    std::remove(testfile);
    146168}
    147169
     170void 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
    148187void save3(
    149     const char * testfile, 
    150     boost::shared_ptr<A>& first, 
     188    const char * testfile,
     189    boost::shared_ptr<A>& first,
    151190    boost::shared_ptr<A>& second,
    152191    boost::weak_ptr<A>& third
    153192){
     
    159198}
    160199
    161200void load3(
    162     const char * testfile, 
    163     boost::shared_ptr<A>& first, 
     201    const char * testfile,
     202    boost::shared_ptr<A>& first,
    164203    boost::shared_ptr<A>& second,
    165204    boost::weak_ptr<A>& third
    166205){
     
    175214}
    176215
    177216void save_and_load3(
    178     boost::shared_ptr<A>& first, 
     217    boost::shared_ptr<A>& first,
    179218    boost::shared_ptr<A>& second,
    180219    boost::weak_ptr<A>& third
    181220){
     
    214253    boost::shared_ptr<A> spa1 = spa;
    215254    save_and_load2(spa, spa1);
    216255
     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
    217261    // test a weak pointer
    218262    spa = boost::shared_ptr<A>(new A);
    219263    spa1 = spa;
    220264    boost::weak_ptr<A> wp = spa;
    221265    save_and_load3(spa, spa1, wp);
    222    
     266
    223267    // Try to save and load pointers to Bs, to a text archive
    224268    spa = boost::shared_ptr<A>(new B);
    225269    spa1 = spa;