Boost logo

Boost :

From: JOAQUIN LOPEZ MU?Z (joaquin_at_[hidden])
Date: 2005-01-25 17:09:03


----- Mensaje original -----
De: Robert Ramey <ramey_at_[hidden]>
Fecha: Martes, Enero 25, 2005 10:33 pm
Asunto: [boost] Re: [Serialization] of pointers into a vector

> This is a currently known problem with the library. It only became
> apparrent with the implementation of serialization for indexed set.
>
> The problem arises because the objects are deserialized to a temporary
> variable then moved into the collection. So the pointers are not
> going to
> point to objects final resting place.
>

This can be circumvented if one's willing to accept
making intrusive changes to the element type. The
following is a sketch of the tecnhique (caveat, not
compiled):

template<typename Derived>
class retrackable
{
private:
  friend class boost::serialization::access;
  
  template<class Archive>
  void serialize(Archive&,const unsigned int)
  {
    // do nothing
  }
};

class object:public retrackable<object>
{
  ...

  template<class Archive>
  void serialize(Archive&,const unsigned int)
  {
    // serialize as usual
  }
}

class foo
{
private:
  std::vector<object> v;
  object* po; // po points to some element of v

  friend class boost::serialization::access;

  BOOST_SERIALIZATION_SPLIT_MEMBER()

  template<class Archive>
  void save(Archive& ar,const unsigned int version)const
  {
    ar<<v;
    for(std::vector<object>::const_iterator it=v.begin(),it_end=v.end();
        it!=it_end;++it){
      ar<<static_cast<const retrackable<object>&>(*it);
    }

    ar<<static_cast<retrackable<object>*>(po);
  }

  template<class Archive>
  void load(Archive& ar,const unsigned int version)
  {
    ar>>v;
    for(std::vector<object>::iterator it=v.begin(),it_end=v.end();
        it!=it_end;++it){
      ar>>static_cast<retrackable<object>&>(*it);
    }

    retrackable<object>* pro;
    ar>>pro;
    po=static_cast<object*>(pro);
  }
};

Of course, an extension to Boost.Serialization would
be better --something along a facility to instruct
the serialization lib to change the tracking address
of an object when this is moved around.

HTH,

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo


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