Re: [boost] Re: [Serialization] of pointers into a vector

----- Mensaje original ----- De: Robert Ramey <ramey@rrsd.com> Fecha: Martes, Enero 25, 2005 10:33 pm Asunto: [boost] Re: [Serialization] of pointers into a vector
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

JOAQUIN LOPEZ MU?Z wrote:
I don't think it will be all that hard once its apparent what the problem is. (Of course that's what I always think - that's my problem) my current thinking is to implement something like template<class Archive, class T> common_iarchive<Archive>::move(T & dest, T & source) which would do the assignment and update the tracking information. Then use it in the de-serializaton of all the collections. Robert Ramey

Robert Ramey wrote:
Unless I'm mistaken, this also requires that all pointers to the elements of the container need to unserialized *after* unserializing the container itself. Unserializing pointers first will create the instances which will be moved to the container later. Moving will not update the original pointers, unless unserialization is retriggered for the objects containing the pointers or some other trick is used. Another issue is with vector's reallocation, but it appears that the library already handles this. -kaj
participants (3)
-
JOAQUIN LOPEZ MU?Z
-
Kaj Björklund
-
Robert Ramey