1

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);
}
keyboard
  • 2,137
  • 1
  • 20
  • 32
  • "How could one improve this architecture?" by reading the documentation and doing what they recommend. here: http://uscilab.github.io/cereal/polymorphism.html – Richard Hodges Apr 01 '16 at 23:23
  • I read this, it's not about raw pointers, but about shared_ptr, etc. Hence everything there will not work for my situation. – keyboard Apr 01 '16 at 23:43
  • If you want raw pointers, go with boost::serialization. It's more comprehensive. The cereal guys are explicit in not supporting raw pointers (which you no doubt know). Out of interest, why raw pointers? – Richard Hodges Apr 01 '16 at 23:49
  • Yes, I know. I actually considered boost::serialization and was down to use it. But since it's not officially supported for iOS / Android and it seems like a major hassle to get it to run on different platforms, I stopped trying after a few hours. I just couldn't find the right combination of hpp / cpps that would compile. So, cereal is easy to include, light-weight and just works....besides the pointers. I'm using raw pointers since I serialize stuff of the cocos2d-x game engine - which uses raw pointers combined with their own memory management (reference counting). – keyboard Apr 02 '16 at 00:16
  • I could potentially use smart_pointers for the code I need to serialize, but that would be kind of a contrast to the rest of my memory management in the whole app. In addition, I also don't really like the weird registration that cereal requires for polymorphism. I'd actually prefer having to use a factory with types over that as it seems cleaner to me. – keyboard Apr 02 '16 at 00:18
  • If your memory management does not use smart pointers, I fear for the success of your project – Richard Hodges Apr 02 '16 at 00:25
  • 2
    I'll make sure to drop you an App Store link once I'm done ;) Cocos2d-x copied Objective-C-style reference counting - before C++11 existed. It's not ideal, but it's working fine. – keyboard Apr 02 '16 at 01:05
  • As someone considering going down this path... How did it work out? Did you need to do something else, or (after the fact) do you see any potential better ways to do it? – jeffrey_t_b May 07 '18 at 22:28
  • 1
    @jeffrey_t_b sorry for seeing this so late. We did use Cereal now - and it works out quite fine. It's very fast at runtime, it's less hassle to get to compile than boost. Though prepare for MUCH increased compile times if your class hierarchy is large (ours is huge). This is all template magic. Also my IDE Xcode got pretty confused by it and slowed down significantly. So I'd say - if you have a huge class hierarchy and really need it - go for it. Otherwise maybe something easier and more lightweight is better. – keyboard May 18 '18 at 11:25

0 Answers0