boost序列化和register_type [英] boost serialization and register_type

查看:328
本文介绍了boost序列化和register_type的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个应用程序中遇到问题。我需要使用boost序列化序列化一些数据包。根据文档,可以分别使用.hpp和.cpp文件中的 BOOST_CLASS_EXPORT_KEY BOOST_CLASS_EXPORT_IMPLEMENT ,以便能够使用多元基础指针序列化派生类。



所以这里是我有的:



.hpp:包含我的类声明并最终确定 BOOST_CLASS_EXPORT_KEY(mynamespace :: mypacket)



.cpp:包含我的类定义和 BOOST_CLASS_EXPORT_IMPLEMENT(mynamespace :: mypacket)



一切运行良好,直到这一点,但是当需要序列化我得到一个bad_alloc错误



我通过在我需要的存档上显式调用方法 register_type< mypacket>()要使用。



但这是我的问题:提升的EXPORT *是为了避免调用register_type方法还是我做错了?我有点像在我的代码中做同样的工作,但是,如果我们仍然需要使用register_type,那么我不会看到使用导出键+实现的任何优势!



我在这里和其他地方阅读了一些其他帖子,似乎我不是唯一一个尝试这个问题的人,但我还没有找到任何答案。

解决方案

我想出如何避免在归档文件中调用register_type。对于那些可能感兴趣的人,需要进行模板序列化专业化和导出key +工具。



所以这里是你的.hpp应该是这样的: / p>


  • 类声明(mynamespace :: myclass)

  • 类导出: BOOST_CLASS_EXPORT_KEY (mynamespace :: myclass)



在cpp:




  • class definiton

  • 类导出: BOOST_CLASS_EXPORT_IMPLEMENT(mynamespace :: myclass) / li>
  • AND:对于每个类,需要使用的存档的serialize()成员专门化:



template void mynamespace :: mypacket :: serialize(boost :: archive :: text_iarchive& arch,const unsigned int version);



template void mynamespace :: mypacket :: serialize(boost :: archive :: text_oarchive& arch,const unsigned int version);



其中boost :: archive :: text_(i / o)archi你应该用你正在使用的任何类型的升级档案替换。



希望它有助于某人(这显然是在 boost documentation ,但我一直错过了,直到今天... )


I have a problem in an application i'm doing. I need to serialize some packets using boost serialization. According to the documentation, one can use BOOST_CLASS_EXPORT_KEY and BOOST_CLASS_EXPORT_IMPLEMENT respectively into .hpp and .cpp files to be able to use polymorphic base pointer to serialize derived class.

So here is what I have:

.hpp: containaing my class declaration and finaly the BOOST_CLASS_EXPORT_KEY(mynamespace::mypacket)

.cpp: containing my class definition and the BOOST_CLASS_EXPORT_IMPLEMENT(mynamespace::mypacket)

Everything runs fine till this point but when needing to serialize I get a bad_alloc error.

I worked arround this problem by explicitly calling the method register_type<mypacket>() on the archive i need to use.

But here is my question : Is the EXPORT* of boost meant to avoid calls to register_type method or am I doing something wrong? I kind of feel like doing twice the same work in my code, but more than that I don't see any advantage of using export key + implement if we still have to use register_type on archive after!

I read some other posts here and elsewhere, it seems I'm not the only one to experiment the problem, but I have not found any answer yet.

解决方案

i figured out how to avoid calling register_type on archive. For those who might be interested, it is needed to do template serialization specialization as well as exporting key + implement.

So here is what your .hpp should look like :

  • class declaration (mynamespace::myclass)
  • class export : BOOST_CLASS_EXPORT_KEY(mynamespace::myclass)

And in the cpp:

  • class definiton
  • class export : BOOST_CLASS_EXPORT_IMPLEMENT(mynamespace::myclass)
  • AND : serialize() member specialization on the archive you need to use, for each class :

template void mynamespace::mypacket::serialize(boost::archive::text_iarchive& arch, const unsigned int version);

template void mynamespace::mypacket::serialize(boost::archive::text_oarchive& arch, const unsigned int version);

Where boost::archive::text_(i/o)archive should be replaced with whatever kind of boost archive you are using.

In hope it will help someone someday (this is clearly written in the boost documentation, but i must have missed it till today...)

这篇关于boost序列化和register_type的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆