如何保存和加载 C++以模块化方式应用程序状态

发布于 2024-08-04 10:06:36 字数 332 浏览 4 评论 0原文

我有一个分布式 C++ 应用程序,它由分布在 2 台机器上的 4 个进程组成。其中一个应用程序充当其余应用程序的“控制中心”。

我希望能够将当前状态保存到文件中并稍后再次加载。到底什么是“状态”,由系统中的每个模块单独定义。保存时,模块状态应合并为一个文件。加载时,每个模块都应该从文件中读回状态数据。

状态必须保存到人类可读的文本文件中,因为某些用户将对其进行编辑。所以二进制文件格式不是一个选择。此外,首选标准文件格式,例如 XML 或 YAML。

您如何建议实现我刚才描述的状态保存/加载的基本框架?我更喜欢执行此任务所需的最少数据序列化工作。此外,该框架应该允许轻松添加更多要在将来保存的数据。

I have a distributed C++ application, which is composed of 4 processes spread on 2 machines. One of the applications serves as "control center" for the rest of the applications.

I want to be able to save the current state to a file and load it again later. What exactly is a "state" is defined separately by each module in the system. When saving, the modules states should be combined to one file. When loading, each module should read the state data back from the file.

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

How would you recommend implement a basic framework for state saving/loading as I just described? I prefer to do the minimum data serialization work needed for this task. Also, the framework should allow to easily add more data to be saved in the future.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

℡Ms空城旧梦 2024-08-11 10:06:36

看看 boost.Serialize库。这是一个非常好的库,用于将对象流式传输到 (xml) 文件。

您的类不需要编写加载和保存函数,而只需编写序列化函数,并且该函数可以双向工作。

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
   }
}

Have a look at the boost.Serialize lib. It's a very nice lib for (un)streaming objects to an (xml) file.

Instead of writing a Load and Save function your class only need to write a serialize 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
   }
}
时光瘦了 2024-08-11 10:06:36

使用 XML。让每个类都有一个保存和加载功能。然后在 XML 中将其最高级别标记作为类的名称。这样,当您加载回来时,您可以查找要使用的类,然后创建一个类并调用其 Load 函数。

Use XML. Have each class have a Save and a Load function. Then in the XML you put its highest level tag as the name of the class. That way when you load back you can look up which class to use and then create one and calls its Load function.

万人眼中万个我 2024-08-11 10:06:36

我绝对站在 XML 一边。将每个进程保存的数据封装在进程标识符标记中将很有用。我们在应用程序中使用类似的系统来保存/加载插件包含的数据,并且它非常方便的最终扩展。

我的第一个方法是:

  1. 控制中心(CC)启动保存。
  2. CC向每个进程发送保存
    信号。
  3. 每个过程都形成自己的一部分
    XML 包括其进程标识符并将其发送到 CC
  4. CC 收集这些 XML 片段并
    将它们合并为一个大文件
    &将其保存到磁盘。

加载时:

  1. CC读取XML文件。
  2. 当遇到一个进程时
    标识符,发送适当的
    部分到相应的进程
    带有负载信号。
  3. 每个进程加载自己的部分并
    发送一个加载完成信号到
    抄送。
  4. 当所有流程完成后
    正在加载,CC向他们发送开始信号
    然后他们开始正常的工作流程。

I'm definitely on XML's side. Enclosing data saved by each process in a process identifier tag will be useful. We use a similar system in our application for saving/loading data contained by plugins and it is very convenient end expandable.

My first approach would be this:

  1. Control Center(CC) initiates save.
  2. CC sends each process the save
    signal.
  3. Each process forms its own part of
    XML including its process identifier and sends it to CC
  4. CC gathers these XML snippets and
    combines them into a one, large file
    & saves it to disk.

When loading:

  1. CC reads the XML file.
  2. When it encounters a process
    identifier, sends the appropriate
    part to the corresponding process
    with a load signal.
  3. Each process loads its own part and
    sends a load complete signal to the
    CC.
  4. When all processes are finished
    loading, CC sends them Start signal
    and they start normal workflow.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文