1

i am trying to send an object throught a named pipe but, i don't understand how to serialize my object :

class Order {
public:
  void                          addFile(std::string const &file);
  void                          setType(Parser::Information const &type);
  std::list<std::string>        getFileList() const;
  Parser::Information           getType() const;
  void                          clear();

private:
  std::vector<std::string>      fileList;
  Parser::Information           type;
};

i already managed to make my named pipe work with basic data types but, i don't understand how to send and receive a full object (without using boost serialization)

I tried to put the object's datas in a struct, but I didn't manage to send it through named pipes... probably because of the vector

Could someone share his knowledge with me please

4
  • In C++, a struct is very much like a class.
    – 2785528
    Apr 21, 2016 at 15:30
  • std::vector, and std::list, and std::string all have pointers. In other words, to transfer the contents of any of these, you will have to 'eliminate' the pointers, and send only data. Lots of ways to do it. Research term "persistant storage".
    – 2785528
    Apr 21, 2016 at 15:33
  • 1
    You need serialization for this. You need to either write code, or use an existing framework, to (de)serialize your objects (to and)from a byte stream.
    – Useless
    Apr 21, 2016 at 15:48
  • Maybe with std::bitset ? ;) Apr 21, 2016 at 15:52

1 Answer 1

2

You need to convert the structure into something that can fit into a single array of bytes that can be written to the pipe as a block of memory. A string is the simplest example.

A trivial (but inflexible) way to format your data as a string would be to use C++ string streams to write type, followed by a newline, then write the first entry in fileList, followed by a newline, then the second entry, a newline, etc. When the far end of the pipe receives this data, it would have to read the first line from the string, parse it into type, then read each of the next lines and add them to file list. (If your named pipe is across the network, you may want to encode the data as utf-8 to avoid character set problems.)

In practice, you want a more flexible file format that tags the values. A common solution is JSON, which can encode multiple structures, vectors, boolean, int, double and other values. JSON is always utf-8 and can handle nested structures. C++ isn't the easiest language from which to use JSON, but it's better than rolling your own solution. One library that can read and write JSON data is https://github.com/open-source-parsers/jsoncpp.

At the high end of serialization formats is binary encoding, which is much faster to parse than string-based data. However, binary data is not human readable, so it can be more difficult to debug. One example of a library that does binary encoding/decoding is Cap'n Proto at https://capnproto.org/.

Not the answer you're looking for? Browse other questions tagged or ask your own question.