Nico
Nico

Reputation: 330

how to retrieve true_type from an archive?

Context

I use the Boost serialization library to save and load objects of a system. I defined practices around this lib, and so I always serialize from a base class (every class serializable inherits from ISerializable). As a consequence, the true_type (i.e. the most derived type) is different from the this_type (i.e., ISerializable) and the true_type is stored in the archives.

My Question

How to retrieve this true_type (as the string written in the archive), only from the archive object?

Details

Let's have this class tree:

ISerializable <|-- B <|-- D

If I do:

B* b = new D();
b->SaveToFile(path); // <= this will do the serialization `ar & this`
                           (`this` being a `ISerializable*`)

I obtain an archive where it is written the true_type "D" (whatever the type of the archive : txt, bin or xml).

With the b object and this code :

const boost::serialization::extended_type_info & true_type
        = * boost::serialization::type_info_implementation<ISerializable>::type
            ::get_const_instance().get_derived_extended_type_info(*b);

I have what I want in true_type.get_key(), i.e : "D". I can verify that "D" is written in every archive storing b. My question again: how, only with an archive object (construct from the archive file without error), can I retrieve this key?

Upvotes: 3

Views: 160

Answers (1)

Georg Bremer
Georg Bremer

Reputation: 86

It should be something like this:

B * b;
ar >> b;//loading archive
const boost::serialization::extended_type_info & true_type
    = * boost::serialization::type_info_implementation<ISerializable>::type
        ::get_const_instance().get_derived_extended_type_info(*b);

Because the saved type is D, the loadad type will be D, too.

Upvotes: 1

Related Questions