从boost序列化存档构造对象 [英] Construct object from boost serialization archive

查看:171
本文介绍了从boost序列化存档构造对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能直接从档案中构造对象?

这样的事情...

  //非工作伪code
结构美孚{
    BOOST_SERIALIZATION_SPLIT_MEMBER();
    的std ::矢量<&INT GT;数据;    美孚(){
        //做计算填充数据
        data.push_back(1); data.push_back(2);
    }    模板<类归档和GT;
    美孚(归档和放大器; AR){
        //由rading归档填充数据
    }    模板<类归档和GT;
    无效保存(归档和放大器; AR,const的无符号整型版)常量{
        //数据的正常序列
        AR<<数据;
    }
};INT主(INT ARGC,为const char * argv的[])
{
    //反序列化
    提高::档案:: text_iarchive桨(的std :: CIN);
    富富(桨);    返回0;
}


解决方案

您可以使用反序列化构造

 的#include<升压/存档/ text_oarchive.hpp>
#包括LT&;升压/存档/ text_iarchive.hpp>#包括LT&;&的fstream GT;类Point
{
上市:
    点()=默认值;
    点(的boost ::存档:: text_iarchive&安培;存档)
    {
        归档>> *这个;
    }    浮X = 1 .;
    浮Y = 2;私人的:
    友元类的boost ::系列化::访问;    模板<类TArchive>
    无效连载(TArchive&安培;归档,const的无符号整型版)
    {
        归档和放大器; X;
        归档和放大器; ÿ;
    }
};
诠释的main()
{
    点p;
    p.x = 5;
    p.y = 6;    的std ::的ofstream的OutputStream(test.archive);
    提高::档案:: text_oarchive的outputArchive(OutputStream中);
    outputArchive<<磷;
    outputStream.close();    性病:: ifstream的InputStream的(test.archive);
    提高::档案:: text_iarchive inputArchive(InputStream的);    点pointRead(inputArchive);    性病::法院LT&;< pointRead.x<< << pointRead.y<<的std :: ENDL;    返回0;
}

Is it possible to construct objects from directly from the archive?

Something like this...

// Non-working pseudo code
struct Foo {
    BOOST_SERIALIZATION_SPLIT_MEMBER();
    std::vector<int> data;

    Foo() {
        // populate "data" by doing calculation
        data.push_back(1); data.push_back(2);
    }

    template<class Archive>
    Foo( Archive & ar ) {
        // populate "data" by rading the archive
    }

    template<class Archive>
    void save(Archive & ar, const unsigned int version) const {
        // Normal serialization of data
        ar << data;
    }
};

int main(int argc, const char *argv[])
{
    // deserialize
    boost::archive::text_iarchive oar(std::cin);
    Foo foo(oar);

    return 0;
}

解决方案

You can use a deserializing constructor:

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

#include <fstream>

class Point
{
public:
    Point() = default;
    Point(boost::archive::text_iarchive& archive)
    {
        archive >> *this;
    }

    float x = 1.;
    float y = 2.;

private:
    friend class boost::serialization::access;

    template<class TArchive>
    void serialize(TArchive & archive, const unsigned int version)
    {
        archive & x;
        archive & y;
    }
};


int main()
{
    Point p;
    p.x = 5;
    p.y = 6;

    std::ofstream outputStream("test.archive");
    boost::archive::text_oarchive outputArchive(outputStream);
    outputArchive << p;
    outputStream.close();

    std::ifstream inputStream("test.archive");
    boost::archive::text_iarchive inputArchive(inputStream);

    Point pointRead(inputArchive);

    std::cout << pointRead.x << " " << pointRead.y << std::endl;

    return 0;
}

这篇关于从boost序列化存档构造对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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