如果我序列化类的一个实例的集装箱,使用Boost库:
If I serialize an instance of the class Container, using Boost library:
class Container { public: Block** blocks; int blocksNumber; } class Block { public: int blockType; unsigned char* data; }然后填写所有必要的数据,以得到一个完整的容器:
And then filling all the necessary data to get a complete Container:
Container container = new Container(); container.blocksNumber=5; Block** blocks = (Block**)malloc(sizeof(Block*)*container.blocksNumber); blocks[0] = (Block*)malloc(sizeof(Block)); //..... Filling all the necessary data... and constructing the container所以我的问题是:实例的序列化格式的容器包含所有分配的数据?换句话说,如果我反序列化容器的序列化格式,将我能够阅读的数据内容?谢谢了很多!
So my question is: Would the serialized format of the instance container contain all the allocated data? In other words, if I deserialize the serialized format of container, would I be able to read the content of data? Thank you a lot!
推荐答案我不打算为什么你这里带来不安全Ç大炮浪费任何时间考虑。你打算使用升压序列化,现代化通用的C ++库。
I'm not going to waste any time contemplating why you bring unsafe C cannons in here. You're looking to use Boost Serialization, a modern generic C++ library.
下面是它应该是什么样子:
Here's what it should look like:
struct Block { int blockType; std::vector<uint8_t> data; }; struct Container { std::vector<std::vector<Block>> block_lists; };看到一个完整的工作示例:
See a complete working sample:
住在Coliru
#include <boost/serialization/vector.hpp> class Block { public: int blockType; std::vector<uint8_t> data; private: friend class boost::serialization::access; template <typename Ar> void serialize(Ar& ar, unsigned) { ar & blockType; ar & data; } }; class Container { public: std::vector<std::vector<Block>> block_lists; private: friend class boost::serialization::access; template <typename Ar> void serialize(Ar& ar, unsigned) { ar & block_lists; } }; #include <boost/archive/text_oarchive.hpp> int main() { Container const letshavesome = { { // block_lists { Block { 1, { 0x11, 0x12, 0x13, 0x14 } }, Block { 2, { 0x21, 0x22, 0x23, 0x24 } }, Block { 3, { 0x31, 0x32, 0x33, 0x34 } }, }, { Block { 4, { 0x41, 0x42, 0x43, 0x44 } }, Block { 5, { 0x51, 0x52, 0x53, 0x54 } }, Block { 6, { 0x61, 0x62, 0x63, 0x64 } }, }, } }; boost::archive::text_oarchive oa(std::cout); oa << letshavesome; }输出
22 ::系列化存档11 0 0 0 0 2 0 0 0 3 0 0 0 1 0 4 17 18 19 20 2 4 0 33 34 35 36 3 4 0 49 50 51 52 3 0 4 4 0 65 66 67 68 5 4 0 81 82 83 84 6 4 0 97 98 99 100
更多推荐
提升:包含指针类实例的序列化
发布评论