Boost logo

Boost :

From: Robert Ramey (ramey_at_[hidden])
Date: 2002-12-18 23:50:36


>From: Matthias Troyer <troyer_at_[hidden]>

>> the current serialization code doesn't rely on save being virtual
>> to function. It downcasts the base class pointer to the most
>> derived class and calls the save function on the recast pointer.
>> This casting is implemented by void_cast.

>How can you determine the most derived class? Do you try all classes
>that were registered to find the most derived one?

there should be code similar to the following in your copy of serialization_imp.hpp

template<class T>
inline static const std::type_info & base_type(T &t)
{
        return typeid(T);
}

const std::type_info & this_type = base_type(*t);
// retrieve the true type of the object pointed to
const std::type_info & true_type = typeid(*t);
if(this_type != true_type){
        // its a pointer to a more derived object
        // check if its been "registered" in this archive
        const type_info_extended_save * tix =
        type_info_extended_save::find(ar, true_type);
        // if it has been "registered" in the archive
        if(NULL != tix){
                // convert pointer to more derived type
                const void * vp = void_downcast(this_type, true_type, t);
                assert(NULL != vp);
                // and save this type
                tix->save(ar, vp);
                return ar;
        }
}

This uses typeid to get the true type of an object to be saved through the pointer.
void_cast is used to transform the address of the base class part of the object
to the derived class part of the object.

void_cast is uses a special registry built during program initialization from
the base_obect<T> invokations in the save/load functions.

This was indispensible to implement non-intrusive save/load which
was in turn indispensible to implement save/load of stl collections
and other templates.

Robert Ramey


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk