먼저 그 기능이 추상적 인 향상 말은 항상 필요하지 않습니다 : 이제 까다로운 점은 클래스를 등록하는 것입니다
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);
}
을 :
BOOST_SERIALIZATION_ASSUME_ABSTRACT(Feature);
직렬화 방법은 다음과 같이 더 많거나 적은 찾아야한다 직렬화/역 직렬화 중 :
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;
명시 적으로 등록하는 것을 잠시 숨길 필요가있는 경우 더 자동적으로 만들려면 하나의 파생 클래스를 등록하고, 모든 목록을 작성하고, 하나의 목록에 넣는 특별한 Functor 템플릿을 작성하는 것이 좋습니다.이 클래스는 Feature 클래스의 한 정적 메소드를 모두 등록합니다. 그러나 문제는 아카이브의 모든 버전에 대한 등록이 필요하며, 현재 다형성 아카이브가 작업을 수행하는지 여부는 알 수 없습니다.
첫 번째 코드 상자의 11 번째 행에 버그가 있다고 생각합니다. 실수가 아니라면 "boost :: serialization :: base_object"왼쪽에 "ar &"이 있어야합니다. 그래도 좋은 대답! –
@lionbest :'boost :: serialization :: base_object (* this); 대신'BOOST_SERIALIZATION_BASE_OBJECT_NVP'을 써야하지 않습니까? –
Cookie
기본 클래스가 두 개 이상있는 경우 이름을 지정할 수 있습니다. 이 매크로는 여기에 필요하지 않다고 생각하지만 확실한 매크로를 원할 수 있습니다. – Arpegius