command pattern serialization in c++

末鹿安然 提交于 2019-12-12 14:39:48

问题


I want to do the folloing in C++:

  1. create a command object
  2. serialize it
  3. (send it to another computer)
  4. deserialize
  5. 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


回答1:


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.




回答2:


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 an int tag with a name 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.




回答3:


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++.)



来源:https://stackoverflow.com/questions/5237639/command-pattern-serialization-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!