Boost logo

Boost :

From: Vladimir Prus (ghost_at_[hidden])
Date: 2004-01-12 04:45:51


Robert Ramey wrote:

> The intention was that it be used for:
>
> struct A
> {
> int* content;
> int size;
>
> template<class A>
> void serialize(A& a, unsigned version)
> {
> a & size;
> a & make_binary_object(content, size*sizeof(int));
> }
> };

At least, the docs do not document this intention. I also think that this
implementation will crash at runtime, since 'serialize' is not allocating
memory for 'content'. Looking at binary_object.hpp, I see the following:

   template<class Archive>
    void load(Archive & ar, const unsigned int file_version){
        ar.load_binary(m_t, m_size);
    }

So it's not allocating memory either. Am I missing something?

>>Upon loading, the serialization lib tries to load 'size*sizeof(int)' of
>>data into 'content', but the problem is that neither 'size' nor 'content'
>>is initialized at the time. So, it loads random amount of data to random
>>memory location. What's worse, 'write_binary' and 'read_binary' member
>>functions has disappeared from archives.
>
> its renamed save_binary and load_binary Its in there but not in the docs.
> The reason for this is: that the above is more convenient than the
> following.
>
> struct A
> {
> int* content;
> int size;
>
> template<class A>
> void save(const A& a, const unsigned version) const
> {
> a << size;
> a.save_binary_objectcontent, size*sizeof(int));

I presume you meant

    a.save_binary(content, size*sizeof(int));

> }
> void load(A& a, const unsigned version) const
> {
> a & size;
> a & make_binary_object(content, size*sizeof(int));

I presume you meant

    a.load_binary(content, size*sizeof(int))

? How will allocate the memory in this case?

> template<class A>
> void serialize(A& a, unsigned version)
> {
> a & size;
> a & boost::serialization::nvp("object", make_binary_object(content,
> size*sizeof(int)));
> }
> };

Yea, I realize this advantage.

>>3. Is it possible to either modify make_binary_object or introduce a new
>>function which will do new[] on loading and restore data size as well?
>
> binary_object is a wrapper. so is nvp. Its serialization traits
> are set so that it generates no overhead and maintains
> compatibilty with all operators.

Ok, so I can write my own wrapper 'make_dynarray'. That's good, but I still
don't understand why 'make_binary_object' is usefull.

> Perhaps the concept of "serialization wrappers" might
> be added to the manual. I considered this but thought
> it might be considered by some to be so obvious as to
> be condesending. Also, the manual is getting pretty long.

I'm not sure 'serialization wrappers' should be documented. But, definitely,
'make_binary_object' should. As it stands, there's no documentation how it
should be used and what's the sematic of loading/saving this object.

- Volodya


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk