Boost logo

Boost Users :

Subject: [Boost-users] [serialization] serializing a pointer without causing object tracking
From: Stefan Strasser (strasser_at_[hidden])
Date: 2013-03-21 12:52:01


Hi everyone,
Hi Robert, in case you'd like to answer this one, because I have a
rather specific question about your library. Thank you in advance!

I'd like to serialize an object and later reconstruct it from the
serialized data:

void save(T const &t){ ar << t; }
T* load(){
   T *t=new T(); //error
   ar >> *t;
}

T might not have a default constructor.
so load_construct_data should be used. apparently the way to do this is
to serialize through a pointer:

void save(T const *t){ ar << t; }
T *load(){
   T *t; ar >> t;
}

this works, but it causes the type T to be tracked, if its object
tracking is set to track_selectively.
I don't want that. but I can't set it to track_never either, because if
the type is serialized at another point in the program through a
pointer, the type should be tracked.

is there a solution to this? is there a serialization wrapper that says
"don't track this type just because of this pointer"?

what comes to mind as a solution is serializing as an object, and
calling load/save_construct_data manually. but then there is another
problem:

void save(T const &t){
     save_construct_data(t,version<T>::value);
     ar << t;
}
void load(T *unconstructed_t){
     load_construct_data(unconstructed_t, ??? );
     ar >> *t;
}

if the implementation level of T is object_class_info, the object
version is loaded from the preamble when the object itself is loaded.
there is no in between loading the version and loading the object, it's
entirely handled in the non-header part of the library.

so basically I have a solution when the implementation level is
object_serializable (no version info, call load_construct_data
manually), and when tracking is set to track_never (serialize as a pointer).
but not for the default case, object_class_info with track_selectively.
am I missing something?

Stefan


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net