Boost logo

Boost :

From: Emily Winch (emily_at_[hidden])
Date: 2002-02-08 06:25:56


> 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" ?

[ snipped code ]

I was thinking something like this (scribbled psuedocode):

enum poly_style { static_poly, dynamic_poly };

template<typename E, poly_style poly>
E* deserialise_object(stream& src){
  IF< poly = static_poly,
      static_deserialise<E>,
      dynamic_deserialise<E>
>::type()(src);
}

template<typename E>
E* static_deserialise(stream& src){

  // Read the object's type name
  std::string type_name;
  src >> type_name;
        
  // maybe we assert that the typename corresponds to typeid(E).name()

  E* object = new E;
  src >> *object;
  return object;
}
      
template<typename E>
E* dynamic_deserialise(stream& src){

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

  E* 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 object;
}

... and similarly for the serialisation code
The format written by the static_poly code needs to be readable by the
dynamic_poly code.
This way you don't even need to build the dynamic_poly code if you aren't
using it at the moment.

Emily


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