[Serialization] How to force an archive to store true_type even if it is this_type?

Hello. When we serialize a polymorphic object, an archive stores the information about true_type (i.e. the concrete type of an object) only if it is not equal to this_type (i.e. the type of the serialized object). I.e. : the type of the serialized object is stored only if it is serialized from a base class. How can I force the archive to always store the type? Let's illustrate with a sample, I'm not sure the above text is so obvious...: Let's have 2 classes serializable: class Base { ... }; class Leaf : public Base { ... }; If I do : std::stringstream ios(std::ios_base::binary | std::ios_base::out | std::ios_base::in); * Base** b = new *Leaf*(); boost::archive::text_oarchive ar(ios); ar & b; Then the archive contains: 22 serialization::archive 9 0 4 *Leaf* 1 0 0 0 0 0 0 0 0 It contains the Leaf concrete type information. However if I do : * Leaf** b = new *Leaf*(); boost::archive::text_oarchive ar(ios); ar & b; Then the archive contains: 22 serialization::archive 9 0 1 0 0 0 0 0 0 0 0 It doesn't contain Lef type info. How can I produce an archive WITH the true_type all the type? I search 2 ways: - calling a method of archive type (register_type, save_pointer, ...) unsuccessfully - directly change the boost/archive/detail/oserializer.hpp file: in struct polymorphic, in function template<class T> static void save(Archive &ar, T & t) I comment out if(*this_type == *true_type){ const basic_pointer_oserializer * bpos = register_type(ar, t); ar.save_pointer(vp, bpos); return; } unsuccessfully. Thank you for help. Nicolas. PS : a word about why I'm trying to do that. When the true_type is "registered" (I'm not sure register is the right word), we can deserialize the object to any base class. Exemple: SERIALIZATION: Leaf* leaf = new Leaf(); ar & leaf; SERIALIZATION: Base* b = 0; ar & b; // would work only if Leaf has been saved in the archive as true_type For tools in a multi-team project, it' powerfull : data for tests has been serialized by a team, and the other team can deserialize, let's say an image, to the interface class IImage without asking: what was the true_tyep : an Image3D, an AImage2D, an Image2DRGB, an Image2DRAW, and so on.

nico wrote:
Hello.
When we serialize a polymorphic object, an archive stores the information about true_type (i.e. the concrete type of an object) only if it is not equal to this_type (i.e. the type of the serialized object).
I.e. : the type of the serialized object is stored only if it is serialized from a base class.
How can I force the archive to always store the type?
replace ar & t; with ar & static_cast<base_type &>(t); Robert Ramey
PS : a word about why I'm trying to do that. When the true_type is "registered" (I'm not sure register is the right word), we can deserialize the object to any base class. Exemple:
SERIALIZATION: Leaf* leaf = new Leaf(); ar & leaf;
SERIALIZATION: Base* b = 0; ar & b; // would work only if Leaf has been saved in the archive as true_type
In general, you should be sure that that type "saved" is always the same as the type "loaded".
For tools in a multi-team project, it' powerfull : data for tests has been serialized by a team, and the other team can deserialize, let's say an image, to the interface class IImage without asking: what was the true_tyep
an Image3D, an AImage2D, an Image2DRGB, an Image2DRAW, and so on.
To get the most leverage from your design, you should make modules which use polymorphic types ONLY use the base class (reference or pointer) and avoid using the derived type at all. For example: instead of using derived_type d(initialization...) derived_type *dptr = new derived_type(initialization...) use base_type & b = d(initialization...) base_type *bptr = new derived_type(initialization...); Better yet use base_type &b = factory(initilization....) where factory returns some a base_type & etc. .... Robert Ramey
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
participants (2)
-
nico
-
Robert Ramey