Boost logo

Boost :

From: quendezus (quendez_at_[hidden])
Date: 2002-02-08 05:15:05


--- In boost_at_y..., Emily Winch <emily_at_b...> wrote:
>
> External polymorphism would give you the runtime polymorphism
without the
> intrusiveness. It would be nice to be able to turn that off,
though, for
> circumstances where the structure is known at compile time: and
easily,
> since "the structure will be known at compile time" sounds like one
of those
> things in requirements documents that gets changed when you already
wrote
> everything :)
>
> Emily
>

See below a pseudocode of a mechanism to switch between compile time
and run time serialization. The compile time code is executed if a
terminal type is given to serialize_object( ). The run time code is
executed if a base polymorphic type is given (and 'object' points on
a derived type). Somehow, the user can thus choose the mechanism he
wants to use through the type of the parameter. Does it correspond to
your initial idea of "turn that off" ?

Sylvain

//--------------------------------------

template <class E>
void serialize_object( stream& dest, const E& object )
        {
        // Write the object's type name
        dest << typeid( object ).name( );

        // If the real type is E, all is known
        // at compile time so we just serialize
        // the object
        if ( typeid( object ) == typeid( E ) )
                {
                dest << object;
                }

        // If E is only a base class of 'object',
        // use the dynamic mechanism to
        // serialize. This mechanism requires
        // a previous registration of the type.
        else
                {
                // Get the serializer for the final type
                serializer& s =
                        get_serializer( typeid( object ).name( ) );

                // Serialize the object (virtual method
                // of class 'serializer')
                s.serialize( dest,
                        dynamic_cast< const void* >( &object ) );
                }
        }

//--------------------------------------

template < class E >
E* deserialize_object( stream& src )
        {
        E* object = NULL;

        // Read the object's type name
        std::string type_name;
        src >> type_name;

        // If the type is E, all is known
        // at compile time so we just create
        // and deserialize the object
        if ( type_name == typeid( E ).name( ) )
                {
                object = new E;
                src >> *object;
                }

        // If E is only a base class of 'object',
        // use the dynamic mechanism to
        // serialize. This mechanism requires
        // a previous registration of the type.
        else
                {
                // Create the object
                object = factory.create< E >( type_name );

                // Get the serializer for the final type
                serializer& s =
                        get_serializer( type_name );

                // Deserialize the object (virtual method
                // of class 'serializer')
                s.deserialize( src,
                        dynamic_cast< void* >( &object ) );
                }

        // Return the object
        return object;
        }

//--------------------------------------


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