How to save and load the state of a C ++ application in a modular way

I have a distributed C ++ application that consists of 4 processes spread across two machines. One of the applications serves as a "control center" for the rest of the applications.

I want to save the current state to a file and load it later. What exactly is a "state" is determined separately by each module in the system. When saving, the state of the modules must be combined into one file. When loaded, each module must read state data from a file.

The state should be saved in a human readable text file as it will be edited by some users. So the binary file format is not an option. In addition, a standard file format such as XML or YAML is preferred.

How would you recommend using a basic structure for saving / loading state as I just described? I prefer to do the minimal data serialization required for this task. In addition, the structure should make it easy to add more data for future storage.

+2


source to share


3 answers


Have a look at boost.Serialize lib. This is a very nice lib for (un) stream objects for a file ( xml ).

Instead of writing a Load and Save function, your class only needs to write a serialization function, and this function will work both ways.



class X
{
   friend class boost::serialization::access;
   template<class Archive>
   void serialize(Archive & ar, const unsigned int version)
   {
       ar & make_nvp("degrees(=xml_tagname)", degrees);
       ar & make_nvp("minutes(=xml_tagname)", minutes);;
       ar & BOOST_SERIALIZATION_NVP(seconds);  // =auto xml tag
   }
}

      

+7


source


Use XML. Each class has a "Save and Load" function. Then, in XML, you put your top level tag as the class name. This way when you load back you can look at which class to use and then create it and call it the load function.



+2


source


I am definitely on the XML side. The application data saved by each process in the process ID tag will be helpful. We use a similar system in our application to save / load data contained in plugins, and this is very convenient for extension.

My first approach would be like this:

  • Control Center (CC) initiates save.
  • CC sends each process a save signal.
  • Each process generates its own piece of XML, including its process ID, and submits it to CC
  • CC collects these XML snippets and combines them into one large & amp; saves it to disk.

While loading:

  • CC reads an XML file.
  • When it encounters a process, the identifier sends the corresponding part to the corresponding process with a load signal.
  • Each process loads its part and sends a full load signal to the CC.
  • When all processes have finished loading, CC sends them a start signal and they start a normal workflow.
+1


source







All Articles