|
Boost : |
From: Robert Ramey (ramey_at_[hidden])
Date: 2003-11-19 12:19:44
Vladimir Prus wrote:
>> //// set the pointer to the serializer to the serializer
>> //// corresponding to a D1
>> bos_ptr = basic_oserializer::find(*true_type);
>> }
>> //// now we know that vp points to a D1
>> //// and bos_ptr points to a pointer serializer for a type D1
>> assert(NULL != bos_ptr);
>> ar.save_pointer(vp, *bos_ptr);
>I'm lost at this point. The 'save_pointer' somehow stores the *actual*
>object, so it has some way to invoke serialization for the actual type.
bos_ptr now points to the base class of oserializer for type D1. This base
class includes a virtual function that does the casting to D1 in this example:
template<class Archive, class T>
class oserializer : public basic_oserializer
{
public:
virtual void save_object_data(
boost::archive::basic_oarchive & ar,
const void *x
) const {
// make sure call is routed through the highest interface that might
// be specialized by the user.
serialize(
static_cast<Archive &>(ar),
* static_cast<T *>(const_cast<void *>(x)),
version()
);
}
[snip]
};
>Good, so if you pass void pointer to base object, the code which stores that
>data will assume it points to the most derived type, and crash.
I'm passing a void pointer to the most derived object. void_cast adjusts the
pointer to the base class to point to the most derived class.
>Now, am I right in assuming that you can detect the case where most derived type is
>not equal to static type only for polymorphic classes?
RTTI only detects differences for polymorphic classes (those with at least
one virtual function). If one wants to do this on other classes he will have
to use a custom extended_type_info. An example of this is in the tests.
> Is so, then you can use
> dynamic_cast<void*>(ptr);
You can? That would surprise me ! I don't find such a thing in any of my
references. void_cast basically (re?)implements this functionality. The
difference would be that void_cast doesn't necessarily depend upon RTTI.
Robert Ramey
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk