c++ - 如何使用boost使用shared_ptr成员序列化对象

标签 c++ serialization boost deserialization

有摘要I1并派生C1 .

有摘要I2并派生C2 .

I1shared_ptr<I2> .如何使用 boost serializaton 使它们可序列化?我正在尝试这样做,但我的应用程序出现异常。

#include <sstream>
#include <boost/shared_ptr.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/shared_ptr.hpp>

struct I1
{
    I1() {}
    virtual ~I1() = 0 {}

    template<class Archive>
    void serialize(Archive &ar, const unsigned int version)
    {
    }
};

struct C1 : I1
{
    virtual ~C1() {}

    template<class Archive>
    void serialize(Archive &ar, const unsigned int version)
    {
        ar & boost::serialization::base_object<I1>(*this);
    }
};

struct I2
{
    virtual ~I2() = 0 {}

    template<class Archive>
    void serialize(Archive &ar, const unsigned int version)
    {
        ar & p;
    }

    boost::shared_ptr<I1> p;
};

struct C2 : I2
{
    C2() { p = boost::shared_ptr<I1>(new C1); }
    virtual ~C2() { }

    template<class Archive>
    void serialize(Archive &ar, const unsigned int version)
    {
        ar & boost::serialization::base_object<I2>(*this);
    }
};

int main()
{
    C2 c2;

    std::string s;
    std::stringstream ss(s);

    boost::archive::binary_oarchive oa(ss);
    oa.register_type<I1>();
    oa.register_type<C1>();
    oa.register_type<I2>();
    oa.register_type<C2>();

    oa << c2;

    boost::archive::binary_iarchive ia(ss);
    //ia.register_type<I1>(); // cannot instantiate abstract class
    ia.register_type<C1>();
    //ia.register_type<I2>(); // cannot instantiate abstract class
    ia.register_type<C2>();

    ia >> c2;
}

最佳答案

boost 序列化文档说 here BOOST_CLASS_EXPORT 的有趣之处:

... BOOST_CLASS_EXPORT ...

Hence, the need for export is implied by the usage of a derived class that is manipulated via a pointer or reference to its base class.

您的 p 指针正是这样做的。将这些宏添加到您的代码中还可以摆脱来自 main 的丑陋的显式 register_type() 调用,这也很好 :)

所以,这段代码似乎可以在 VS2014 中编译和工作:

#include <sstream>
#include <boost/shared_ptr.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/export.hpp>

struct I1
{
    I1() {}
    virtual ~I1() = 0 {}

    template<class Archive>
    void serialize(Archive &ar, const unsigned int version)
    {
    }
};

BOOST_CLASS_EXPORT(I1)

struct C1 : I1
{
    virtual ~C1() {}

    template<class Archive>
    void serialize(Archive &ar, const unsigned int version)
    {
        ar & boost::serialization::base_object<I1>(*this);
    }
};

BOOST_CLASS_EXPORT(C1)

struct I2
{
    virtual ~I2() = 0 {}

    template<class Archive>
    void serialize(Archive &ar, const unsigned int version)
    {
        ar & p;
    }

    boost::shared_ptr<I1> p;
};

BOOST_CLASS_EXPORT(I2)

struct C2 : I2
{
    C2() { p = boost::shared_ptr<I1>(new C1); }
    virtual ~C2() { }

    template<class Archive>
    void serialize(Archive &ar, const unsigned int version)
    {
        ar & boost::serialization::base_object<I2>(*this);
    }
};

BOOST_CLASS_EXPORT(C2)

int main()
{
    C2 c2;

    std::string s;
    std::stringstream ss(s);

    boost::archive::binary_oarchive oa(ss);
    oa << c2;

    boost::archive::binary_iarchive ia(ss);
    ia >> c2;
}

不过,有趣的是,Boost 文档中的声明显然不适用于所有编译器,并且互联网上的许多代码示例在 VS2014 中都不起作用。

关于c++ - 如何使用boost使用shared_ptr成员序列化对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27110035/

相关文章:

c++ - 如何比较结构

java - 加密程序

c++ - boost::smart_ptr 的内部结构

python - 从 Python 调用 C++ 函数并将 OpenCV Mat 转换为 Numpy 数组

c++ - 为什么在调用重载赋值运算符时调用复制构造函数?

c++ - 如何在 C++ 中创建一个包含不同类型函数指针的容器?

javascript - 将 contenteditable 字段序列化为 JSON 对象以保存到模型的最佳方法是什么?

date - POF 序列化/反序列化与日期故障?

serialization - 是否可以扩展类并仍将DSON Generator用于Dart?

c++ - 最低共同祖先( boost 图)