I'm about to implement serialization of my object graph (actually a tree) with Cereal.
Cereal does not support serializing raw pointers (the objects that these pointers point to). My current idea is to have a getType()
function on the objects to find out their type, then construct them with a factory according to their type.
After the correct object is being created, we can initialize it with the archive.
This is definitely not an ideal solution, since every class needs to return a unique type from its getType()
function.
How could one improve this architecture?
template <class Archive>
void save( Archive & ar, const unsigned int version ) const
{
ar << _obj->getType();
ar << *_obj;
}
template <class Archive>
void load( Archive & ar, const unsigned int version )
{
ObjType type;
ar >> type;
_obj = Factory::createWithType(type);
_obj->load(ar, version);
}
Aucun commentaire:
Enregistrer un commentaire