简体   繁体   English

使用Boost和IOStreams序列化为静态数据

[英]Serialization into static data using Boost and IOStreams

I'm trying to take a complex, nested structure and store it directly in a .cpp file as static data. 我正在尝试采用复杂的嵌套结构,并将其作为静态数据直接存储在.cpp文件中。 The approach I'd like to take is to take my object (which already supports Boost serialization) and serialize it as a binary archive into a byte array . 我想采取的方法是将我的对象(已经支持Boost序列化)并将其作为二进制存档序列化为字节数组 I could then take that byte array, and walk through it to autogenerate the required .cpp code to hold the binary array. 然后,我可以获取该字节数组,并逐步遍历以自动生成所需的.cpp代码以保存二进制数组。 Then, I'd like to deserialize from that byte array back into the object . 然后,我想从该字节数组反序列化回object

So basically, at the end of the day I'd like something like this: 所以基本上,在一天结束时,我想要这样的事情:

unsigned char* my_obj = { 10, 48, 48, 30, 20 ... }

When I want to use that data, I'd just wrap it in the "byte stream" and pass it into Boost again to deserialize back into my actual object. 当我想使用这些数据时,只需将其包装在“字节流”中,然后再次将其传递给Boost,以反序列化回到我的实际对象中。

My question is: is there some easy way to pass byte arrays around as streams? 我的问题是:是否有一些简单的方法可以将字节数组作为流传递? Boost deals with istreams and ostreams for reading and writing the archives. Boost处理istream和ostream来读写档案。 I don't want to use a stringstream or a filestream, but rather what I suppose may be a custom stream that just acts as a giant byte array for whatever is passed to it. 我不想使用字符串流或文件流,但我想可能是一个自定义流,该流仅充当传递给它的任何内容的巨型字节数组。

I feel like there should be a great way to create this custom stream and have it seamlessly work with the Boost serialization... I'm just not really sure where to begin? 我觉得应该有一种很棒的方法来创建此自定义流,并使它与Boost序列化无缝配合...我只是不太确定从哪里开始?

unsigned char* my_obj = { 10, 48, 48, 30, 20 ... } 未签名的char * my_obj = {10,48,48,30,20 ...}

It is better to use: 最好使用:

unsigned char my_obj[] = { 10, 48, 48, 30, 20 ... }

So you will have array, which knows it's size, not just pointer to begin. 因此,您将拥有一个数组,该数组知道其大小,而不仅仅是开始的指针。 Also, add const if you don't plan to modify it. 另外,如果您不打算修改它,请添加const。


My question is: is there some easy way to pass byte arrays around as streams? 我的问题是:是否有一些简单的方法可以将字节数组作为流传递? Boost deals with istreams and ostreams for reading and writing the archives. Boost处理istream和ostream来读写档案。 I don't want to use a stringstream or a filestream, but rather what I suppose may be a custom stream that just acts as a giant byte array for whatever is passed to it. 我不想使用字符串流或文件流,但我想可能是一个自定义流,该流仅充当传递给它的任何内容的巨型字节数组。

Check Boost's array_source and array_sink . 检查Boost的array_source和array_sink

live demo : 现场演示

#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/stream.hpp>
#include <iostream>
#include <ostream>
using namespace boost;
using namespace std;

struct Data
{
    double value;

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

int main()
{
    Data d={3.1415926};
    char buffer[256];
    {
        iostreams::array_sink sink(buffer);
        iostreams::stream<iostreams::array_sink> stream(sink);
        archive::binary_oarchive out_archive(stream);
        out_archive << d;
    }
    Data restored = {0.0};
    {
        iostreams::array_source source(buffer);
        iostreams::stream<iostreams::array_source> stream(source);
        archive::binary_iarchive in_archive(stream);
        in_archive >> restored;
    }
    cout << boolalpha << ( restored.value == d.value ) << endl;
}

Output is: 输出为:

true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM