Send a deep copy of an object across a process border in C ++

I have a situation where I have a C ++ class object that needs to be sent across the borders of the process (process 1 to process 2) using Linux pipes. I searched on the Internet how to do serialization in C ++. I found boost , but this requires some changes to the class. In my situation, I cannot change the class.

There are many pointers in this class, and nesting continues up to 3 levels (class 1 has a pointer 1 of type Class 2-> Class 2 has a pointer 2 of type Class 3 → Class 3 has a pointer 3 of class 4 → Class 4). Is there a way to send this object using pipes so that it can be recreated in the second process?

Thanks.

+4
source share
3 answers

You need to serialize the class somehow. How exactly is your choice, but you can do it in JSON or XML , or some kind of binary format that you decide. Without seeing more details about your class, there is nothing more to add.

Another option would be to use Shared memory segments to store the class, but this is due to problems with pointer math, concurrency, and other complications.

+2
source

Have you considered the Memento pattern app? You can create a class or classes to handle the details of how to serialize an object (either in text or in binary).

A class created to save objects will also know how to create new objects from the serialization format that you select in the next process.

+1
source

You will need to do serialization because you cannot copy-construct by pipe or something like that. If you cannot change the class, then your only choice is to write an external function or a class that uses your public top-level class API to get all the parts and serialize this data. Then at the other end you will have to restore it from the stream.

+1
source

Source: https://habr.com/ru/post/1441705/


All Articles