Boost logo

Boost Users :

From: Robert Ramey (ramey_at_[hidden])
Date: 2006-07-10 00:43:15


Joel de Guzman wrote:
> Hi,
>
> Here's a use case that I can't seem to find a way to
> do using boost.serialization.
>
> Say with version 1, I want to serialize X Y and Z. These
> are hierarchical and expensive data structures. Now, with
> version 2, I do not need to create Y objects anymore. Yet,
> I still have to open version 1 files for backward compatibility.
> So, when loading version 1 files, I want to skip reading Y
> objects. How do I do that without having to create a temporary
> Y object that will be discarded later?

Of course this is the easiest way. Remember that after your user
loads version 1 if he saves it again it will be saved in version 2
without the extraneous Y. So it will be just a temporary problem
except in some unusual cases. For the case whereby a version
1 archive is read again and again you would have to make
some special provision to update it.

> (I'm particularly interested with XML archives. If I had control
> over the xml parser, that would simply mean ignoring everything
> from the current tag until the next matching tag is found).

I believe you do have the control you desire. Here's a sketch:

suppose object A includes memory variables x, y and z

specialize template for xml input archives and type A

void load (boost::archive::xml_iarchive & ia, A & a, const unsigned
version){
    ia & BOOST_SERIALIZATION_NVP(x);
    if(version == 1){
        // the next data should look like <x ...>...</x>
        // skip over this data using your favorite xml parser - or use the
one
        // that the serialization library uses - built with spirit - note
that this
        // is not exposed by the library and is hidden from users as an
        // implementation detail
    }
    ia & BOOST_SERIALIZATION_NVP(z);
}

Robert Ramey

 


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