|
Boost : |
From: Robert Ramey (ramey_at_[hidden])
Date: 2004-01-09 14:10:44
Vladmir Prus wrote:
>I've got a problem with the serialization library on my project. The docs for
>version 14 say say saving binary objects should be done with
'>make_binary_object'. Unfortunately, it does not work for the following case:
> struct A
> {
> int* content;
> int size;
>
> template<class A>
> void serialize(A& a, unsigned version)
> {
> a & make_binary_object(content, size*sizeof(int));
> }
> };
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));
}
};
>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));
}
void load(A& a, const unsigned version) const
{
a & size;
a & make_binary_object(content, size*sizeof(int));
}
BOOST_SERIALIZATION_MEMBER_SPLIT()
};
Especially when we want to make something XML compatible
where we can use:
struct A
{
int* content;
int size;
template<class A>
void serialize(A& a, unsigned version)
{
a & size;
a & boost::serialization::nvp("object", make_binary_object(content, size*sizeof(int)));
}
};
>So, my questions are:
>1. How do I read binary manually?
>2. Isn't make_binary_object too easy to use incorrectly? In fact, the only way
>how it can be used is when size if constant and data is part of object. In
>all other cases, you need to do dynamic allocation yourself, and after that,
>you don't need 'make_binary_object', you can just read the data given that
>there's answer to question 1 above.
>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.
Wrappers can be composed to resonable depth.
You're totally at liberty to make your own wrapper for your
own purposes.
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.
Robert Ramey
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk