Boost logo

Boost Users :

Subject: Re: [Boost-users] [serialization] std::shared_ptr with Boost >= 1.53
From: beet (r.berlich_at_[hidden])
Date: 2015-05-04 05:35:40


Dear Robert,

see below for a simple test program that illustrates what I'm trying to
do. This compiles with Boost 1.58, but fails on 1.53 with the error message

/opt/boost153/include/boost/serialization/access.hpp:118:11: error: no
member
      named 'serialize' in 'std::__1::shared_ptr<test>'
        t.serialize(ar, file_version);

I need to support Boost versions starting with 1.53. Thus I have two
options:

- do serialization of std::shared_ptr myself
- include the 1.58 boost/serialization/shared_ptr.hpp for older
Boost-versions.

What is the best way ?

Thanks and Best Regards,
Beet

/*******************************************************/
// Compile with
// g++ --std=c++11 -o sharedptr sharedptr.cpp -I /opt/boost158/include/
// -L/opt/boost158/lib -lboost_serialization -lboost_system
// on MacOS, Linux

#include <iostream>
#include <memory>
#include <sstream>

#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/shared_ptr.hpp>

class test {
        ///////////////////////////////////////////////////////////////////////
        friend class boost::serialization::access;

        template<typename Archive>
        void serialize(Archive & ar, const unsigned int) {
                using boost::serialization::make_nvp;
                ar
                & BOOST_SERIALIZATION_NVP(secret_);
        }
        ///////////////////////////////////////////////////////////////////////
        
public:
        test(): secret_(1) {}
        
private:
        int secret_;
};

int main() {
  std::vector<std::shared_ptr<test> > t_vec, t_vec2;
  std::ostringstream oss;

  t_vec.push_back(std::shared_ptr<test>(new test()));

  // save data to archive
  {
    boost::archive::xml_oarchive oa(oss);
    // write class instance to archive
    oa & BOOST_SERIALIZATION_NVP(t_vec);
    // archive and stream closed when destructors are called
    // Data is now in oss
  }

  // load data from archive
  {
    std::istringstream iss(oss.str());
    boost::archive::xml_iarchive ia(iss);
    // read class state from archive
    ia & BOOST_SERIALIZATION_NVP(t_vec2);
    // archive and stream closed when destructors are called
  }

  return 0;
}

/*******************************************************/

Am 04.05.15 um 01:34 schrieb Robert Ramey:
> beet-2 wrote
>> Dear Robert,
>>
>> thanks a lot! Do I understand it correctly, that I could easily create
>> my own serializer for std::shared_ptr by just (de)serializing the
>> contained "raw" pointer and wrapping it into a std::shared_ptr in the
>> case of de-serialization ?
>
> No. You have to keep track of other shared pointers point
>
> I had assumed from the question that the situation was something like:
>
> // version 1
> myclass {
> boost::shared_ptr m_p
> ...
> }
> // version 2
> myclass {
> std::shared_ptr m_p
> ...
> };
>
> and the question was asking how the latest version code would de-serialize
> older version (version 1) archives (data files)
>
> Robert Ramey
>
>
>
> --
> View this message in context: http://boost.2283326.n4.nabble.com/serialization-std-shared-ptr-with-Boost-1-53-tp4674950p4675017.html
> Sent from the Boost - Users mailing list archive at Nabble.com.
>


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net