如何序列化共享/弱指针?

发布于 2024-08-15 22:18:37 字数 1460 浏览 4 评论 0原文

我有一个与 QSharedPointers 和 QWeakPointers 连接的复杂对象网络。有没有一种简单的方法可以使用 Boost.Serialization 保存/加载它们?到目前为止我有这个:

namespace boost {
    namespace serialization {
        template<class Archive, class T>
        void save(Archive& ar, QSharedPointer<T> const& ptr, unsigned version) {
            T* sharedPointer = ptr.data();
            ar & BOOST_SERIALIZATION_NVP(sharedPointer);
        }

        template<class Archive, class T>
        void load(Archive& ar, QSharedPointer<T>& ptr, unsigned version) {
            T* sharedPointer = 0;
            ar & BOOST_SERIALIZATION_NVP(sharedPointer);
            ptr = QSharedPointer<T>(sharedPointer);
        }

        template<class Archive, class T>
        void save(Archive& ar, QWeakPointer<T> const& ptr, unsigned version) {
            T* weakPointer = ptr.toStrongRef().data();
            ar & BOOST_SERIALIZATION_NVP(weakPointer);
        }

        template<class Archive, class T>
        void load(Archive& ar, QWeakPointer<T>& ptr, unsigned version) {
            T* weakPointer = 0;
            ar & BOOST_SERIALIZATION_NVP(weakPointer);
            ptr = QSharedPointer<T>(weakPointer);
        }
    }
}

这不起作用,因为共享指针总是从原始指针构造的,所以它们都认为引用计数是1。它还会立即释放弱指针。

经过一些努力,我可以将这些类转换为使用 boost::shared_ptrboost::weak_ptr。这会有什么好处吗?

I have a complex network of objects connected with QSharedPointers and QWeakPointers. Is there a simple way to save/load them with Boost.Serialization? So far I have this:

namespace boost {
    namespace serialization {
        template<class Archive, class T>
        void save(Archive& ar, QSharedPointer<T> const& ptr, unsigned version) {
            T* sharedPointer = ptr.data();
            ar & BOOST_SERIALIZATION_NVP(sharedPointer);
        }

        template<class Archive, class T>
        void load(Archive& ar, QSharedPointer<T>& ptr, unsigned version) {
            T* sharedPointer = 0;
            ar & BOOST_SERIALIZATION_NVP(sharedPointer);
            ptr = QSharedPointer<T>(sharedPointer);
        }

        template<class Archive, class T>
        void save(Archive& ar, QWeakPointer<T> const& ptr, unsigned version) {
            T* weakPointer = ptr.toStrongRef().data();
            ar & BOOST_SERIALIZATION_NVP(weakPointer);
        }

        template<class Archive, class T>
        void load(Archive& ar, QWeakPointer<T>& ptr, unsigned version) {
            T* weakPointer = 0;
            ar & BOOST_SERIALIZATION_NVP(weakPointer);
            ptr = QSharedPointer<T>(weakPointer);
        }
    }
}

This is not working because the shared pointers are always constructed from raw pointers so they all think the reference count is 1. It also immediately frees weak pointers.

With some effort I can convert the classes to use boost::shared_ptr and boost::weak_ptr. Will that do any good?

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

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

发布评论

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

评论(1

淡淡離愁欲言轉身 2024-08-22 22:18:37

问题是你真正想通过序列化指针来实现什么目的?您的预期产出是多少?请注意,指针指向内存中的某个位置——多个指针可能指向内存中的同一位置。

序列化地址将不起作用。您不能只写下确切的内存地址,因为无法保证下一次运行的对象能够占用相同的空间(另一个程序可能已经保留了该位置)。

在我们有指针的每个地方序列化指向的对象不会工作:

  1. 这将是我们需要序列化的更多数据
  2. 如果我们有弱指针创建循环依赖,我们就不会这样做稍后能够停止并恢复该连接。
  3. 反序列化时没有办法将相同的对象合并为一个

想了一下,你可以尝试 boost::serialization 的官方答案:

The question is what do you really want to achieve by serializing pointers? What is your expected output? Note that pointers point to a place in memory -- several may point to the same place in memory.

Serializing the address won't work. You can't just write down the exact memory address, because there's no way to guarantee that objects on the next run will be able to take the same space (another program may have already reserved that place).

Serializing the pointed object in each place where we have a pointer wont work:

  1. This would be a lot more data that we'd need to serialize
  2. If we had weak pointers creating a circular dependency we wouldn't be able to stop and retrieve that connection later.
  3. There is no way to merge the same objects into one when deserializing

Now that you think about it, you can try the official answer of boost::serialization:

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