c++谷物:序列化C风格的数组

时间:2022-07-15 22:28:29

Can/How do you serialize an array using the cereal library.

可以/如何使用谷物库序列化数组。

I.e.

即。

void save(Archive & ar, const unsigned int version) const
{
    unsigned int l  = g1_size_bin(g,POINT_COMPRESS);
    uint8_t data[l];
    memset(data, 0, l);
    g1_write_bin(data, l, g,POINT_COMPRESS);
    ar(l);
    ar(data); // what should be here
}

That doesn't work (nor would I expect it too). Nor does

这是行不通的(我也不希望如此)。也没有

ar(cereal::binary_data(data,l)); 

(which I would think would work, since it looks like the boost code one would use), which produces a compilation error :

(我认为这是可行的,因为它看起来像一个人会使用的boost代码),这会产生一个编译错误:

/usr/local/include/cereal/cereal.hpp:79:17: note: candidate template ignored: substitution failure : variably modified type 'unsigned char (&)[l]' cannot be used as a template argument BinaryData binary_data( T && data, size_t size )

/usr/local/include/cereal/cereal.hpp:79:17:注意:候选模板被忽略:替换失败:变量修改的类型“unsigned char (&)[l]”不能用作模板参数BinaryData binary_data binary_data(T & data, size_t size)

Nor does

也没有

ar.saveBinaryValue(data,l);

Since that method only appears to be supported for XML/Json and I want a binary archive.

由于该方法似乎只支持XML/Json,所以我需要二进制归档。

1 个解决方案

#1


6  

cereal::binary_data is the correct construct to use in this case assuming you want a binary representation of the POD array. This will only work for archives that support binary_data (binary and portable_binary). binary_data doesn't work for the text based archives because it is seen as an optimization for a more generic serialization method - see how vector is serialized for an example of this.

在本例中,如果您想要POD数组的二进制表示,那么binary_data是正确的结构。这将只适用于支持binary_data(二进制和portable_binary)的存档。binary_data不能用于基于文本的归档文件,因为它被视为对更通用的序列化方法的优化——请参见这个示例中的vector是如何序列化的。

Anyway, here's a working example of serializing a C style array:

总之,这里有一个序列化C样式数组的工作示例:

#include <cereal/archives/binary.hpp>
#include <iostream>

int main()
{
  std::stringstream ss;

  {
    cereal::BinaryOutputArchive ar(ss);
    std::uint8_t data[] = {1, 2, 3};
    ar( cereal::binary_data( data, sizeof(std::uint8_t) * 3 ) );
  }

  {
    cereal::BinaryInputArchive ar(ss);
    std::uint8_t data[3];
    ar( cereal::binary_data( data, sizeof(std::uint8_t) * 3 ) );

    for( int i : data )
      std::cout << i << " ";
  }

  return 0;
}

If you wanted to serialize a C style array to a text based archive, or if your array wasn't over POD types, you would need to iterate over each object and serialize it individually.

如果您希望将C样式数组序列化为基于文本的归档文件,或者如果您的数组没有超过POD类型,那么您将需要对每个对象进行迭代,并分别对其进行序列化。

#1


6  

cereal::binary_data is the correct construct to use in this case assuming you want a binary representation of the POD array. This will only work for archives that support binary_data (binary and portable_binary). binary_data doesn't work for the text based archives because it is seen as an optimization for a more generic serialization method - see how vector is serialized for an example of this.

在本例中,如果您想要POD数组的二进制表示,那么binary_data是正确的结构。这将只适用于支持binary_data(二进制和portable_binary)的存档。binary_data不能用于基于文本的归档文件,因为它被视为对更通用的序列化方法的优化——请参见这个示例中的vector是如何序列化的。

Anyway, here's a working example of serializing a C style array:

总之,这里有一个序列化C样式数组的工作示例:

#include <cereal/archives/binary.hpp>
#include <iostream>

int main()
{
  std::stringstream ss;

  {
    cereal::BinaryOutputArchive ar(ss);
    std::uint8_t data[] = {1, 2, 3};
    ar( cereal::binary_data( data, sizeof(std::uint8_t) * 3 ) );
  }

  {
    cereal::BinaryInputArchive ar(ss);
    std::uint8_t data[3];
    ar( cereal::binary_data( data, sizeof(std::uint8_t) * 3 ) );

    for( int i : data )
      std::cout << i << " ";
  }

  return 0;
}

If you wanted to serialize a C style array to a text based archive, or if your array wasn't over POD types, you would need to iterate over each object and serialize it individually.

如果您希望将C样式数组序列化为基于文本的归档文件,或者如果您的数组没有超过POD类型,那么您将需要对每个对象进行迭代,并分别对其进行序列化。