如何使用 boost 序列化 3D 数组?

发布于 2024-11-08 17:44:20 字数 284 浏览 4 评论 0原文

好吧,我最近切换到 boost,并且我部分理解了序列化简单对象或简单类(boost 教程让我感到困惑),但是如何序列化包含类成员的 3D 数组?

我有一个名为 TileList 的数组 (std::vector),其中包含属于 Tile 类的对象,而 Tile 类只包含两个变量:int TileID 和 int TilePassability。

我尝试像序列化教程中的非侵入式方法和 STL 容器方法那样进行操作,但我只是得到一堆错误作为回报。有人可以解释如何正确地做到这一点吗?谢谢。

Okay, I recently switched to boost and I partitialy understand serializing simple objects or simple classes (the boost tutorial confuses me), but how can I serialize a 3D array which contains class members?

I have an array (std::vector) called TileList, which contains objects which are of a class Tile and class Tile contains nothing else than two variables, int TileID and int TilePassability.

I tried doing it as the Serialization tutorial does in the non obtrusive method and the STL Container method, but I just get a stack of errors in return. Can somebody explain how to do it properly? Thank you.

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

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

发布评论

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

评论(1

荆棘i 2024-11-15 17:44:20

所以我知道你有这样的东西(这不是真正的 3D 数组,如果我误读了你,请发布你想要序列化的确切代码):

struct Tile {
    int id, passability;
};

std::vector<Tile> tileList;

因为 std::vector 已经可以通过标准 Boost.Serialization 序列化设施,你只需要使Tile可序列化。最简单的方法是添加一个执行加载和保存操作的 serialize 成员。

struct Tile {
   int id, passability;

   template <typename Archive>
   void serialize(Archive& ar, const unsigned version) {
       ar & id;
       ar & passability;
   }
};

就这样,您的类型现在可以序列化了。需要考虑的特殊事项包括类成员是私有的——在这种情况下,您需要为 Boost.Serialization 添加友元声明,即

class Tile {
    int id, passability;
    friend class boost::serialization::access;

    template <typename Archive>
    void serialize(Archive& ar, const unsigned version) {
        ar & id;
        ar & passability;
    }
public:
    Tile(int, int);
};

serialize 成员必须是一个模板(好吧,不是真的;但直到您知道库更好一点,它必须是),因此它的整个主体必须​​包含在类声明中(除非您使用显式实例化,但这也不适合初学者)。

您还可以将其拆分为 saveload 成员,但这在您开始使用版本控制之前没有用处。它看起来像这样:

struct Tile {
    int id, passability;

    BOOST_SERIALIZATION_SPLIT_MEMBER()
    template <typename Archive>
    void save(Archive& ar, const unsigned version) { ... }
    template <typename Archive>
    void load(Archive& ar, const unsigned version) { ... }
};

这使得向量所持有的类型可序列化。序列化向量本身只是archive &瓦片列表

So I understand you have something like this (that's not really a 3D array, post the exact code you want to serialise if I misread you):

struct Tile {
    int id, passability;
};

std::vector<Tile> tileList;

Because std::vector is already serialisable through standard Boost.Serialization facilities, you only need to make Tile serialisable. The easiest way is to add a serialize member that does both the loading and saving.

struct Tile {
   int id, passability;

   template <typename Archive>
   void serialize(Archive& ar, const unsigned version) {
       ar & id;
       ar & passability;
   }
};

Aand that's about it, your type is now serialisable. Special things to be considered include class members being private — you need to add friend declaration for Boost.Serialization in this case, i.e.

class Tile {
    int id, passability;
    friend class boost::serialization::access;

    template <typename Archive>
    void serialize(Archive& ar, const unsigned version) {
        ar & id;
        ar & passability;
    }
public:
    Tile(int, int);
};

serialize member have to be a template (well, not really; but until you know the library a bit better, it have to be), so its entire body must be included within the class declaration (unless you use explicit instantiation, but that's also not something for beginners).

You can also split it into save and load members, but that's not useful until you start using versioning. It would look like this:

struct Tile {
    int id, passability;

    BOOST_SERIALIZATION_SPLIT_MEMBER()
    template <typename Archive>
    void save(Archive& ar, const unsigned version) { ... }
    template <typename Archive>
    void load(Archive& ar, const unsigned version) { ... }
};

That makes the type held by the vector serialisable. Serialising a vector itself is just archive & tileList.

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