惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

TaoSecurity Blog
TaoSecurity Blog
Jina AI
Jina AI
雷峰网
雷峰网
月光博客
月光博客
The GitHub Blog
The GitHub Blog
WordPress大学
WordPress大学
B
Blog RSS Feed
美团技术团队
C
CXSECURITY Database RSS Feed - CXSecurity.com
小众软件
小众软件
Security Latest
Security Latest
Microsoft Azure Blog
Microsoft Azure Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cybersecurity and Infrastructure Security Agency CISA
Last Week in AI
Last Week in AI
A
Arctic Wolf
Latest news
Latest news
Attack and Defense Labs
Attack and Defense Labs
I
Intezer
F
Fortinet All Blogs
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
Webroot Blog
Webroot Blog
S
Secure Thoughts
Help Net Security
Help Net Security
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
V
Visual Studio Blog
P
Proofpoint News Feed
博客园 - 【当耐特】
P
Privacy International News Feed
V
Vulnerabilities – Threatpost
Stack Overflow Blog
Stack Overflow Blog
Know Your Adversary
Know Your Adversary
云风的 BLOG
云风的 BLOG
Hacker News: Ask HN
Hacker News: Ask HN
L
LINUX DO - 最新话题
H
Help Net Security
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
T
Tailwind CSS Blog
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tenable Blog
Cloudbric
Cloudbric
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog

博客园 - 小水坝

使用双分派解决领域实体和外部机制通信问题 搞定thrift双向消息 读《软件需求最佳实践》有感 【thrift】vc中使用thrift中文字符串乱码问题解决 __declspec(dllimport)的小秘密(转) 跨线程send message 【boost】ptree 读写中文的问题 动态创建TeeChart的简便方法 【MFC】动态创建CMFCToolbar图标不显示问题 【boost】使用装饰者模式改造boost::thread_group 【VC】VC工具栏图标合并工具(非tbcreator和visual toolbar) 【boost】使用lambda表达式和generate_n生成顺序序列 【boost】BOOST_LOCAL_FUNCTION体验 【boost】MFC dll中使用boost thread的问题 【转帖】C++编译原理 资料 IE6,7下password框长度变短问题 dwz局部表格分页 dwz中combox的value问题 dwz中使用flot,js报表等js插件
【boost】使用serialization库序列化子类
小水坝 · 2013-11-26 · via 博客园 - 小水坝

boost.serialization库是一个非常强大又易用的序列化库,用于对象的保存与持久化等。

使用base_object可以在序列化子类的同时也序列化父类,以此获得足够的信息来从文件或网络数据中反序列化出子类。

最近在工作中却遇到这样一个问题,代码示例如下

struct Field
{
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & boost::serialization::make_nvp("FieldImpl", serializeString);
    }

    string serializeString;
};

template<typename ValueType>
struct SubField : Field
{
    typedef ValueType type;

    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & boost::serialization::make_nvp("CommonField", boost::serialization::base_object<Field>(*this));
        ar & boost::serialization::make_nvp("data", data);
    }
    
    type data;
};

有超类Field与模板子类SubField(可能还会有其他子类),现在需要通过超类指针多态序列化子类

首先想到的办法是将超类serialize方法签名为virtual,通过多态调用子类serialize方法。但是这却是不可能的,因为serialize方法是一个模板函数,不能为vitual方法,所以这条路不通。

无奈之下只好直接尝试一下:

shared_ptr<SubField<int> > spSub(new SubField<int>());
    spSub->data = 10;
    spSub->serializeString = "sub";
    shared_ptr<Field> spField(new Field());
    spField = boost::static_pointer_cast<Field>(spSub);

    std::stringstream ss;
    boost::archive::xml_oarchive ar(ss,  boost::archive::archive_flags::no_header);
    
    ar << boost::serialization::make_nvp("Field", spField);
    std::cout << ss.str();

不出所料,运行时抛出了异常。
重新阅读手册及serialization文档,意外发现几个关键词,BOOST_SERIALIZATION_ASSUME_ABSTRACT和BOOST_CLASS_EXPORT,继续求助sof后,终于找到了问题得解决办法,原文链接如下:

http://stackoverflow.com/questions/1332602/how-to-serialize-derived-template-classes-with-boost-serialize

http://stackoverflow.com/questions/8370727/error-serializing-an-abstract-class-with-boost

第一种是使用BOOST_SERIALIZATION_ASSUME_ABSTRACT配合archive的template register_type方法

第二种是直接使用BOOST_CLASS_EXPORT,导出所有继承链中的class

对于第二种方法直接使用

BOOST_CLASS_EXPORT(Field)
BOOST_CLASS_EXPORT(SubField<int>)

问题解决,输出为: