c++ 中的命令模式序列化
我想在 C++ 中执行以下操作:
- 创建一个命令对象
- 序列化它
- (将其发送到另一台计算机)
- 反序列化
- 执行
两种情况:
- 发送方和接收方都是 win 7 计算机
- 发送者是 *nix,接收者是 win 7
我找到了一个序列化教程: http://www.functionx.com/cpp/articles /序列化.htm。这是要走的路吗?在Python中我可以这样做:
def setAndPackCommand(self, object):
outFile = StringIO.StringIO()
pickledC = pickle.dump(object, outFile) # this packs object to outFile
stringToSend = outFile.getvalue() # decoding to string
def unpackAndExecute(self, stringToReceive):
inFile = StringIO.StringIO()
inFile.write(stringToReceive)
inFile.seek(0, 0)
receivedC = pickle.load(inFile)
receivedC.execute()
在这段代码中,要点是pickle.dump和pickle.load。 C++ 的对应项是什么?维基百科说c++不支持序列化?那上面的链接是什么?
二进制序列化是什么意思?内存转储到磁盘并且反序列化需要完全相同的计算机(无跨平台传输)?
br, 尤哈
I want to do the folloing in C++:
- create a command object
- serialize it
- (send it to another computer)
- deserialize
- execute
Two cases:
- sender and receiver are both win 7
computers - sender is *nix and receiver is win
7
I found a tutorial for searialization: http://www.functionx.com/cpp/articles/serialization.htm. Is this the way to go? In python I could do:
def setAndPackCommand(self, object):
outFile = StringIO.StringIO()
pickledC = pickle.dump(object, outFile) # this packs object to outFile
stringToSend = outFile.getvalue() # decoding to string
def unpackAndExecute(self, stringToReceive):
inFile = StringIO.StringIO()
inFile.write(stringToReceive)
inFile.seek(0, 0)
receivedC = pickle.load(inFile)
receivedC.execute()
In this code the main point are pickle.dump and pickle.load. What are the C++ counterparts? Wikipedia says that c++ does not support serialization? What is the link above then?
What does binary serialization mean? memory is dumped to disk and deserialization needs exactly the same computer (no cross-platform transfers)?
br,
Juha
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我还建议使用像 boost.serialization 这样的稳定库来序列化数据。
如果您不熟悉序列化,这意味着将对象转换为适合传输或存储的数据表示形式,并从该数据表示形式重建它们。对于所谓的 POD(普通旧数据对象)来说,难度并不是很大。您可以将缓冲区作为数据传输,并在传输后通过处理数据对齐将其转换回来和字节顺序(字节序)。如果对象引用其他对象,事情就会变得更加复杂,然后使用设计良好的库才真正有意义。 Boost 的序列化还支持版本控制,因此您可以更新格式并保持向后兼容的读取器和写入器(当然需要付出一些努力)
这里有一个很好的介绍。
I would also recommend using a stable library like
boost.serialization
for serializing data.If you are new to serialization, it means transforming objects into a data representation suitable for transmission or storage and rebuilding them from that data representation. The difficulty is not really big with so-called PODs (Plain Old Data objects). You can transmit the buffer as data and cast it back after the transfer by taking care of the data alignment and byte ordering (endianness). It becomes more complicated if objects reference other objects, and then it makes really sense to use a well designed library. Boost's serialization also supports versionning so you can update your format and keep up and backward compatible readers and writers (with some effort of course)
Here is a good introduction.
为了简要回答您的问题,维基百科是正确的 - C++ 本身不支持序列化。这并不意味着您不能推出自己的解决方案,正如您链接的文章中所示。
二进制序列化是指将对象写入二进制文件格式。与(例如)XML 序列化相反,其中对象被写入基于 XML 的格式:在前者中,您将获得一个二进制文件,其中(例如)
int
由 4 个字节的原始数据组成二进制数据。在后者中,您可能会得到一个带有name
属性的int
标记,其内容是整数的文本值,例如12345
.二进制序列化的一大优点是(在大多数情况下)它非常紧凑,并且在目标平台上与对象之间的转换非常简单。缺点是,正如您所建议的,它往往是非常特定于机器的,因此在您的情况下不是很有用。字节排序和字段对齐等问题往往因平台而异,因此不考虑这些问题的二进制格式很可能不可移植。也就是说,您可以添加代码来考虑这些差异,但这确实会增加解决方案的复杂性。
替代方案(基于文本的序列化、XML 序列化等)具有通常更跨平台且更易于手动编辑的优点,但通常不如二进制方法紧凑。
由于上述原因,我会避免在您的情况下进行二进制序列化(如果可能),并采用基于文本的方法。我喜欢的一篇文章是这篇,它描述了(除其他外)一种方法序列化允许您轻松指定您喜欢使用的任何类型的序列化方法。
To briefly answer your questions, Wikipedia is right - C++ doesn't natively support serialisation. That doesn't mean that you can't roll your own solution though as demonstrated in the article you linked.
Binary serialisation refers to writing an object to a binary file format. Contrast to (for example), XML serialisation where the object is written to an XML-based format: in the former, you get a binary file where (for example) an
int
consists of 4 bytes of raw binary data. In the latter, you might get anint
tag with aname
attribute and its contents being the text value of the integer, such as<int name="myInt">12345</int>
.The big advantage of binary serialisation is (in most cases) that it is very compact, and very simple to convert to/from the object on the target platform. The downside is that, as you've suggested, it tends to be very machine-specific and so not very useful in your situation. Issues such as byte ordering and field alignment tend to vary from platform to platform, and so a binary format where those are not taken into account will most likely not be portable. That said, you can add code to take into account these differences, but it does increase the complexity of the solution.
The alternatives (text-based serialisation, XML serialisation, etc) have the advantage of generally being more cross platform and easier to edit by hand, but are generally less compact than the binary approach.
For the reasons outlined above, I'd avoid binary serialisation in your case (if possible), and go with a text based approach. An article I like is this one, which describes (among other things) an approach to serialisation that allows you to easily specify any kind of serialisation method you prefer to use.
当然,C++ 程序可以进行序列化,只是不是开箱即用。查看 Boost.Serialization 库或 Google 的协议缓冲区。后者实现了快速且可移植的跨平台(二进制)序列化,但需要使用代码生成器。
(您链接到的教程演示了一种非常简单、不可移植的序列化方法。它还很好地演示了如何不在 C++ 中处理字符串。)
Of course C++ program can do serialization, just not out of the box. Check out the Boost.Serialization library or Google's protocol buffers. The latter implements fast and portable cross-platform (binary) serialization, but it requires the use of a code generator.
(The tutorial you link to demonstrates a very simplistic, unportable approach to serialization. It also demonstrates very well how not to handle strings in C++.)