如何序列化派生模板类与Boost.serialize? [英] How to serialize derived template classes with Boost.serialize?

查看:114
本文介绍了如何序列化派生模板类与Boost.serialize?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想序列化/反序列化以下类:

I'd like to serialize/unserialize following classes:

class Feature{
...
virtual string str()=0;
};

template<typename T>
class GenericFeature : public Feature{
T value;
...
virtual string str();
};

我读boost.serialize文档和赛义德,你必须注册类。
 我可以在构造函数中登记。但是,将与加载问题,因为注册将是动态的,不是静态的(我的理解,你必须注册之前,序列化/反序列化类)。

I read boost.serialize docs and the sayed that you must register classes. I can register them in constructor. But there will be problems with loading, as registration will be dynamic, not static(As I understood, you must register classes prior to serialization/deserialization).

如何保存/加载这些类型的类呢?

How to save/load these type of classes?

推荐答案

先告诉提振该功能是抽象的,并不总是需要的:

First tell boost that Feature is abstract, is not always needed:

BOOST_SERIALIZATION_ASSUME_ABSTRACT(Feature);

该序列化方法看起来应该或多或少是这样的:

The serialization method should look more or less like this:

template<class Archive> 
void Feature::serialize(Archive & ar, const unsigned int version)
{
   ar & BOOST_SERIALIZATION_NVP(some_member);
}


template<typename T,class Archive> 
void GenericFeature<T>::serialize(Archive & ar, const unsigned int version)
{
   ar & boost::serialization::base_object<Feature>(*this);  //serialize base class
   ar & BOOST_SERIALIZATION_NVP(some_other_member);
}

现在棘手的问题是注册在序列化/反序列化类:

Now the tricky point is to register class in serialize/deserialize:

boost::archive::text_iarchive inputArchive(somesstream);

boost::archive::text_oarchive outputArchive(somesstream);

//something to serialize
Feature* one = new GenericFeature<SomeType1>();
Feature* two = new GenericFeature<SomeType2>();
Feature* three = new GenericFeature<SomeType3>();

//register our class, must be all of posible template specyfication    
outputArchive.template register_type< GenericFeature<SomeType1> >();
outputArchive.template register_type< GenericFeature<SomeType2> >();
outputArchive.template register_type< GenericFeature<SomeType3> >();

// now simply serialize ;-]
outputArchive << one << two << three;

// register class in deserialization
// must be the same template specification as in serialize
// and in the same correct order or i'm get it wrong ;-D   
inputArchive.template register_type< GenericFeature<SomeType1> >();
inputArchive.template register_type< GenericFeature<SomeType2> >();
inputArchive.template register_type< GenericFeature<SomeType3> >();

Feature* another_one;
Feature* another_two;
Feature* another_three;

// and deserialize ;-]
inputArchive >> another_one >> another_two >> another_three;

如果你需要在某个地方隐藏明确登记并使其更加自动化,有想法,使该注册一个派生类特殊的仿函数模板,创建所有avaible并放在一个单独的列表,该类特征的一个静态方法将登记商场。然而,问题将是您需要注册的所有版本的存档,现在我不知道如果多态的存档将做的工作或没有。

If you need to hide explicit registering somewhere and make it more automatic, there is idea to make special functor template that register one derived class, create all avaible and put in a single list, that one static method of class Feature would register them all. However the problem will be that you need registration for all version of archive, right now i dont know if polymorphic archive will do the job or not.

这篇关于如何序列化派生模板类与Boost.serialize?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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