Boost logo

Boost Users :

From: Anders Sundman (anders.sundman_at_[hidden])
Date: 2007-02-01 14:54:37


Ok, my bad. I've found the docs:

http://www.boost.org/libs/serialization/doc/serialization.html#splitting

But the general question remains, what to do with dynamic data?

It's often the case that I find a T* and a size in classes. I've been
thinking about introducing some kind of convenience functions for this.
Perhaps something along these lines:

template<class T, class Archive>
void saveBuf(Archive & ar, const T * buf, size_t size) {
        bool isNull = (buf == 0);
        ar << size;
        ar << isNull;
        if (!isNull)
                ar.save_binary(buf, size * sizeof(T));
}

template<class T, class Archive>
void loadBuf(Archive & ar, T *& buf, size_t & size) {
        bool isNull;
        ar >> size;
        ar >> isNull;

        if (buf != 0) {
                delete buf;
                buf = 0;
        }

        if(!isNull) {
                buf = new T[size];
                ar.load_binary(buf, size * sizeof(T));
        }
        else {
                buf = 0;
        }
}

What do you thing about that? Any obvious problems? Are there perhaps
already something like this in the library?

// 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