cereal icon indicating copy to clipboard operation
cereal copied to clipboard

Serializer for std::type_index

Open thelink2012 opened this issue 9 years ago • 3 comments

It would be really interesting to have std::type_index serializer on the library, it should work just like the polymorphism serialization, same approach, and could in fact replace the polymorphic_id & polymorphic_name serialization done in smart pointers. The type register should be recycled from CEREAL_REGISTER_TYPE.

It could also have a utility function that calls a generic functor, allowing easy implementation of serializers that relies on RTTI (such as smart pointers with polymorphism themselves!).

template<typename Functor>
void cereal::functor_by_typeinfo(const std::type_index&, Functor);

The operator()() of the functor should be generic with the following signature:

template<typename T>
void operator()(cereal::type_tag<T>)

where type_tag is just a structure with a using type = T;

By having the functor in this way it could take advantage of generic lambdas:

cereal::SomeInputArchive archive;
std::type_index mytype = typeid(void);
boost::any any;

archive(_CEREAL_NVP("type", mytype));
cereal::functor_by_typeinfo(mytype, [&](auto tag) {
    decltype(tag)::type temp;
    archive(_CEREAL_NVP("data", temp));
    any = std::move(temp);
});

Very simple and powerful.

I've done a std::type_index and boost::any serializers in the past (see this mailing list topic), it was simple to do separate from the library but I just tried to built this into the core library and it's so much more complicated that I gave up. That could be because I was trying to just edit the polymorphism serialization code to take care of type indices but since the smart pointers serializes were written without this approach in mind it's complicated to do so.

So well, here I am just throwing a spec to be implemented as well to discuss it too.

thelink2012 avatar Mar 29 '15 04:03 thelink2012

This sounds pretty interesting - I'm going to look at the code you posted a while back again and try to think about this some time. Any ideas you have for general design of this would be welcome too

AzothAmmo avatar Mar 29 '15 04:03 AzothAmmo

I tried once again, but this time by creating new headers instead of editing the existing polymorphic header and I got some progress. But functor_by_typeinfo seems too abstract, I couldn't manage to bring it to life, not enough erasure ehh... having something with a similar functionality would be extremely great, maybe using the same method as binding to all archives? any ideas? Also a friendly bind to all archives would be great hum?

Oh and for reference, are serialized files generated by previous versions of the library supposed to be compatible with files serialized by newer versions?

thelink2012 avatar Mar 29 '15 18:03 thelink2012

Is there any update on this issue? I see a similar discussion for Boost on SO, but @thelink2012's idea seems better.

keceli avatar Mar 23 '21 15:03 keceli