C ++中的命令模式序列化 [英] command pattern serialization in c++

查看:114
本文介绍了C ++中的命令模式序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C ++中进行以下操作:

I want to do the folloing in C++:

  1. 创建命令对象
  2. 序列化
  3. (将其发送到另一台计算机)
  4. 反序列化
  5. 执行

两种情况:

  • 发送方和接收方均获胜7 电脑
  • 发件人是* nix,接收方是获胜者 7
  • sender and receiver are both win 7 computers
  • sender is *nix and receiver is win 7

我找到了有关序列化的教程: http://www.functionx.com/cpp/articles/serialization.htm .这是要走的路吗?在python中,我可以这样做:

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()

在此代码中,重点是pickle.dump和pickle.load.什么是C ++对应物?维基百科说c ++不支持序列化吗?那么上面的链接是什么?

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

推荐答案

我还建议使用像boost.serialization这样的稳定​​库来序列化数据.

I would also recommend using a stable library like boost.serialization for serializing data.

如果您不熟悉序列化,则意味着将对象转换为适合于传输或存储的数据表示形式,并从该数据表示形式重建它们.对于所谓的 POD(普通旧数据对象),难度并不是很大.您可以通过注意数据对齐,将缓冲区作为数据传输并在传输后回退.和字节排序(字节序).如果对象引用其他对象,将变得更加复杂,然后使用精心设计的库确实很有意义. Boost的序列化还支持版本控制,因此您可以更新格式并保持向上和向后兼容的读写器(当然要花些功夫)

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)

这里是一个很好的介绍.

这篇关于C ++中的命令模式序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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