保存复杂的脚本化对象的状态

发布于 2024-12-04 01:54:50 字数 770 浏览 1 评论 0原文

在 C++ 中,我将以下两个类公开(使用 Boost)给 Python:

struct Foo {
    // Empty
};

struct FooContainer {
    // I use boost::shared_ptr for compatibility with Boost.Python
    vector<boost::shared_ptr<Foo>> foos_;
};

在 Python 端,我可能会创建一种特殊类型的 Foo,它实际上会执行某些操作,而不仅仅是一个空类,然后将其添加到 FooContainer:

class Useful(Foo):
    def __init__(self, a, b):
        self.a = a
        self.b = b

x = Useful(3, 5);
# Add 'x' to a `FooContainer`

回到 C++ 端,FooContainer 现在有一些 Foo,但它不知道也不关心它们来自 Python。应用程序运行了一段时间,Foo 对象中的数据发生了变化...

然后我决定保存程序的状态,以便稍后加载它。但问题是 FooContainer 对它的 Foo 对象了解不多,它甚至不知道它们来自 Python,我不想用不属于它的数据污染我的 FooContainer(单-责任原则等等)。

您对我应该如何组织我的应用程序有什么建议,以便可以以清晰的方式保存和加载数据以及加载新数据(即不是来自我过去保存的状态)?

In C++ I have the following two classes that I expose (using Boost) to Python:

struct Foo {
    // Empty
};

struct FooContainer {
    // I use boost::shared_ptr for compatibility with Boost.Python
    vector<boost::shared_ptr<Foo>> foos_;
};

In the Python side I might create a special type of Foo that actually does something instead of being just an empty class, and then add it to a FooContainer:

class Useful(Foo):
    def __init__(self, a, b):
        self.a = a
        self.b = b

x = Useful(3, 5);
# Add 'x' to a `FooContainer`

Back in the C++ side, the FooContainer now has some Foos, but it doesn't know or care that they are from Python. The application runs for a while and the data in the Foo objects changes...

Then I decide I want to save the state of my program so I can load it at a later time. But the problem is that FooContainer doesn't know much about its Foo objects, it doesn't even know that they come from Python and I wouldn't want to pollute my FooContainer with data that doesn't really belong in it (single-responsibility principle and all that).

Do you have any advice on how I should organize my application such that saving and loading data, as well as loading fresh data (ie. not from a state that I saved in the past) can be done in a clear way?

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

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

发布评论

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

评论(2

爱要勇敢去追 2024-12-11 01:54:50

You can use boost::python/pickle, and save the data from python. I only have limited experience with the pickling suite, but it should work provided you override appropriate pickling methods in your classes derived in python (see my answer to this question).

银河中√捞星星 2024-12-11 01:54:50

您已经有了创建 Foos 的 Python 代码,我们将其称为 populateFoos,并且您的程序以某种方式调用它。

现在您需要的下一件事是执行保存和加载的 storeFoosloadFoos 函数。如果您想保持通用,请将它们定义为 storeFuncloadFunc (或 callback,具体取决于上下文)。

根据您的程序结构,您可能还需要在 python 中保留创建的(或与容器关联的)所有 foo 的列表。

You already have python code that creates the Foos, lets call it populateFoos and somehow you have your program call it.

Now the next thing you need is a storeFoos and loadFoos function that does the saving and loading. If you want to keep it generic define them as storeFunc and loadFunc (or callback, depending on the context).

Depending on your program structure you might also need to keep in python a list of all foos created (or associated to a container).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文