我如何序列化包含指针的基本类的类? [英] How do I serialize a class containing pointers to primitives?

查看:488
本文介绍了我如何序列化包含指针的基本类的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用boost的功能来序列化指针到基元(所以我不必解除引用和做一个深存储自己)。但是,当我尝试做这件事时,我得到了一堆错误。下面是一个类的简单示例,该类应该包含写入和读取类内容的 save 加载方法从文件。此程序不编译:

I am trying to use boost's functionality for serializing pointers to primitives (so that I don't have to de-reference and do a deep store myself). However, I get a pile of errors when I try to do it. Here is a simple example of a class that is supposed to contain save and load methods which write and read the class content from a file. This program does not compile:

#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>

#include <boost/serialization/shared_ptr.hpp>
#include <boost/shared_ptr.hpp>

#include <fstream>

class A
{
public:
    boost::shared_ptr<int> sp;
    int const * p;

    int const& get() {return *p;}

    void A::Save(char * const filename);
    static A * const Load(char * const filename);

        //////////////////////////////////
        // Boost Serialization:
        //
    private:
        friend class boost::serialization::access;
        template<class Archive>
        void serialize(Archive & ar,const unsigned int file_version)
        {
            ar & p & v;
        }
};

// save the world to a file:
void A::Save(char * const filename)
{
    // create and open a character archive for output
    std::ofstream ofs(filename);

    // save data to archive
    {
        boost::archive::text_oarchive oa(ofs);

        // write the pointer to file
        oa << this;
    }
}

// load world from file
A * const A::Load(char * const filename)
{
    A * a;

    // create and open an archive for input
    std::ifstream ifs(filename);

    boost::archive::text_iarchive ia(ifs);

    // read class pointer from archive
    ia >> a;

    return a;
}

int main()
{

}

请注意,我对解除引用指针的解决方案感兴趣;我想让boost为我照顾(许多这些类可能指向同一个底层对象)。

Note that I am not interested in a solution that dereferences the pointer; I want boost to take care of that for me (many of these classes might be pointing to the same underlying object).

推荐答案

http://www.boost.org/doc/ libs / 1_54_0 / libs / serialization / doc / index.html


默认情况下,
类序列化trait从来没有被跟踪。如果希望通过一个指针(例如长的用作
引用计数)跟踪
a共享的原始对象,它应该被包装在一个类/结构中,以便它是
一个可标识的类型。改变实现
级别的替代方案将影响在整个程序中序列化的所有长度
- 可能不是一个意图。

By default, data types designated primitive by Implementation Level class serialization trait are never tracked. If it is desired to track a shared primitive object through a pointer (e.g. a long used as a reference count), It should be wrapped in a class/struct so that it is an identifiable type. The alternative of changing the implementation level of a long would affect all longs serialized in the whole program - probably not what one would intend.

因此:

struct Wrapped {
    int value;
    private:
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar,const unsigned int file_version)
    {
        ar & value;
    }
};

boost::shared_ptr<Wrapped> sp;
Wrapped const * p;

这篇关于我如何序列化包含指针的基本类的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆