Boost logo

Boost Users :

From: Robert Ramey (ramey_at_[hidden])
Date: 2007-02-01 15:23:04


Your solution below would likely work. These days I would expect find
the T* / size pair replaced with something like std::vector<T *> tarray
in which case you could have just used:

ar & tarray

But you're solution is a natural way to proceed under the circumstances.

BUT - it presumes that T can be serialized as a binary which is not
portable and not generally true.

A general solution would be to use a loop which

...
ar & t[i]

For examples, look at the implementation of serialization for collections
as included in the library.

Robert Ramey

Anders Sundman wrote:
> 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
>


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