|
Boost Users : |
Subject: [Boost-users] [serialization] postprocessing object after deserialization
From: Václav Šmilauer (eudoxos_at_[hidden])
Date: 2010-04-19 06:03:04
Hi, I am using boost::serialization for hierarchy of classes; I need to
process attributes after deserialization, but I am facing some (perhaps
design) issues. The top class is Serializable, with serialization code
like this:
struct Serializable{
friend class boost::serialization::access;
virtual void preProcess(bool deserializing){}
virtual void postProcess(bool deserializing){}
template <class ArchiveT>
void serialize(ArchiveT & ar, unsigned int version){
// this sequence is always the same:
preProcess(ArchiveT::is_loading::value); // preprocess
/* no base class to be serialized */ // serialize base class
/* no attributes in Serializable itself */ // serialize attributes
postProcess(ArchiveT::is_loading::value); // postprocess
};
};
Each derived class then defines serialize function and optionally
overrides {pre,post}Process, which are meant for pre-computing some
non-serialized data from known attributes, for instance.
struct Derived: public Serializable{
friend class boost::serialization::access;
template <class ArchiveT>
void serialize(ArchiveT & ar, unsigned int version){
preProcess(ArchiveT::is_loading::value); // preprocess
ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Serializable); // base class
ar & BOOST_SERIALIZATION_NVP(attr1); // attributes
ar & BOOST_SERIALIZATION_NVP(attr2);
postProcess(ArchiveT::is_loading::value); // postprocess
}
};
The problem is that {pre,post}Process are not overridden in most cases
and will be called multiple times, perhaps with invalid data:
e.g. when deserializing derived class, postProcess will be called for
each parent class; since it is virtual, it will end up calling
Derived::postProcess _before_ attr1 and attr2 are deserialized from
archive. Later, it will be called again.
My questions are therefore:
1. If boost::serialization provides some hook functions for this purpose
(I didn't find anything in documentation and via google either)
2. If you can suggest some way to have the {pre,post}Process function
called for each class exactly once, at the right place(s).
Best regards, Vaclav
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net