Boost logo

Boost Users :

From: Anders Sundman (anders.sundman_at_[hidden])
Date: 2007-02-02 05:05:32


A new, slightly different question on the same subject: How to treat
data with special "allocator" functions?

Ex:

In pseudo code, I would like to do something like this:

// Serialize
Fu* bar = allocFu();
boost::archive::text_oarchive oa(stream, flags);
oa << bar;
destroyFu(bar);

// De-serialize
Fu* foo = 0;
boost::archive::text_iarchive ia(stream, flags);
ia >> foo;
// Use foo
destroyFu(foo);

But how do I create the load/save functions for this:

template<class Archive>
void save(Archive & ar, const Fu* & t, unsigned int version) {
...
}
template<class Archive>
void load(Archive & ar, Fu* & t, unsigned int version) {
... (run t = allocFu(), among other things) ...
}
BOOST_SERIALIZATION_SPLIT_FREE(Fu*)

But the compiler trips on a static assertion with the comment:

if your program traps here, it indicates taht your doing one of the
following:
a) serializing an object of a type marked "track_never" through a pointer.
b) saving an non-const object of a type not markd "track_never)
Either of these conditions may be an indicator of an error usage of the
serialization library and should be double checked. See documentation
on object tracking.

I don't really understand the error message, and I can't seem to figure
out whats wrong with the code. Any idéas anyone?

// Anders Sundman

Anders Sundman skrev:
> Hi!
>
> About the boost::serialization library, what is the best way to handle
> dynamic data in classes? Any idéas, suggestions or caveats? I didn't
> manage to find anything about this in the docs.
>
> Consider the following sample class:
>
> struct Dyn {
> Dyn(int s) : data(0), size(s) {
> data = new char[size];
> }
>
> ~Dyn() { delete data; }
>
> char* data;
> int size;
>
> private:
> friend class boost::serialization::access;
> template< class Archive > void serialize( Archive &ar, unsigned int ver ) {
> // -> ar & data; What to do here?
> ar & size;
> }
> };
>
> What to do with the data pointer?
>
> Is there any way to determine (in the serialize function) if a
> serialization or "de-serialization" is taking place? And have different
> behaviours - either allocating a new char[x] or copying data to the archive?
>
> Is there any way to store dynamic data in an archive?
>
> Best regards,
> Anders Sundman


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