jt234
jt234

Reputation: 672

c++11 shared_ptr + Boost::Serialization

I am in need of a Boost::Serialization specialization for std::shared_ptr.

I am writing a program that sends objects over Boost::Asio and many of those objects use shared_ptrs internally. Using a workaround to only serialize the objects they are referring to is quite uncomfortable.

I have been trying to make my own, looking at the implementation provided for Boost::shared_ptr but I lack the understanding of how those objects should be handled.

Changing the namespaces in Boost::shared_ptr serialization made it possible to serialize the pointer but deserialization doesn't work for me.

As a last resort, I could(and maybe should) switch over to using std::unique_ptrs for my objects, because, at the moment, it seems they own them, but that would lead me to the same problem, only with unique_ptr.

Upvotes: 1

Views: 538

Answers (2)

jt234
jt234

Reputation: 672

I am not sure if this is the best way to do this but it seems to work.

Changed the namespace and added this load method to shared_ptr specialization header copied from Boost::shared_ptr specialization:

template<class Archive, class T>
inline void load(
    Archive & ar,
    std::shared_ptr< T > &t,
    const unsigned int /* file_version */
){
    T * t_ptr;
    ar >> boost::serialization::make_nvp("px", t_ptr);
    t = std::shared_ptr<T>(t_ptr);
}

Upvotes: 0

BigBoss
BigBoss

Reputation: 6914

You say that you use std::shared_ptr internally. So why you don't use boost::shared_ptr in place of it, it is completely supported by boost::serialization and has same functionality of std::shared_ptr?

Upvotes: 1

Related Questions