加速系列化按位串行化 [英] Boost serialization bitwise serializability

查看:134
本文介绍了加速系列化按位串行化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从is_bitwise_serializable特质期望序列化类像以下(不含序列化功能):

I expect from is_bitwise_serializable trait to serialize class like following (without serialize function):

class A { int a; char b; };
BOOST_IS_BITWISE_SERIALIZABLE(A);
A a{2, 'x'};
some_archive << a; // serializes a bitwisely

我在想,为什么有必要为bitwise_serializable类提供序列化功能?

I wonder, why is there a need to provide serialize function for bitwise_serializable class?

推荐答案

从文档:

一些简单的类可以通过直接复制类的所有位刚刚序列化。这是,特别是对于不包含任何指针构件的POD的数据类型的情况下,和它们既不版本也不进行跟踪。一些档案,如非便携二进制存档可以让我们的这个信息基本上加快系列化。

Some simple classes could be serialized just by directly copying all bits of the class. This is, in particular, the case for POD data types containing no pointer members, and which are neither versioned nor tracked. Some archives, such as non-portable binary archives can make us of this information to substantially speed up serialization.

要表示使用在头文件is_bitwise_serializable.hpp定义的类型特征按位序列的可能性:

To indicate the possibility of bitwise serialization the type trait defined in the header file is_bitwise_serializable.hpp is used:

下面是关键点:这个优化

Here are the key points: this optimization


  • 是归档类型可选的它确实适用

  • 并不适用于所有存档类型例如

  • is optional in the archive types where it does apply
  • doesn't apply to all archive types e.g.


  • 这需要在便携式不能复制出原始内存重新presentation(因为它是实现和平台depenedent)

  • a binary archive that needs to be portable could not be implemented by copying out the raw memory representation (because it is implementation and platform depenedent)

文本存档可能没有的欲望的优化这个(例如,它有不同的目标,如人类可读的XML,C你的<$他们可能不想EN code $ C>矢量&lt; A&GT; 作为一个大bas64连接codeD BLOB)

a text archive might not desire to optimize this (e.g. it has different goals to, like "human readable XML", they might not want to encode your vector<A> as a large bas64 encoded blob).

请注意,这也解释了 is_bit_wise_serializable&LT; T&GT; 不是部分专门为具有 is_pod&LT任何类型; T&GT; ::值==真正(这可以在技术上很容易做到):

Note that this also explains that is_bit_wise_serializable<T> is not partially specialized for any type that has is_pod<T>::value == true (This could technically be done easily):


  • 某些类可能不感兴趣的所有序列的状态(因此使用按位副本将采取更多的空间不仅仅是选择一个有趣的的(双关语意))

  • some classes might not be interested in serializing all their state (so using bitwise copy would take a lot more space than just selecting the interesting bits (pun intended))

您没问,具体地说,但这是工作的实施将是什么样子:

You didn't ask, specifically, but this is what the working implementation would look like:

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

struct A { int a; char b;
    template <typename Ar> void serialize(Ar& ar, unsigned) {
        ar & a;
        ar & b;
    }
};

BOOST_IS_BITWISE_SERIALIZABLE(A)

int main() {
    std::ostringstream oss;
    boost::archive::binary_oarchive oa(oss);

    A data { 1, 'z' };
    oa << data;
}

这篇关于加速系列化按位串行化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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