Boost logo

Boost Users :

From: Robert Ramey (ramey_at_[hidden])
Date: 2008-06-21 14:07:01


The way to do this is as follows:

define your "special type" as a "wrapper" with its own special rules
implemented in it
serialize function. e.g.

template<class T>
struct special_numeric_type {
    // data
   T &| m_data; // holds every kind of ata
    ... serialize(Archive, ar, ....
    template<class U>
    special_numeric_type(U & u)
            // better be convertible to a type T !!!
            m_data(&u)
    {}
};

BOOST...?..IS_WRAPPER(special_numeric_type)

serialize with
    float x;
    complex<double> y;
    ar & make_special_numeric_type(x);
    ar & make_special_numeric_type(x);
...

Now your can implement any kind of fancy process you want inside
the wrapper. This will apply to those variables - and only those
variables you want to take with wrapper. So you have total
control without mucking up the rest of your program.

You can look into NVP for examples.

Robert Ramey.

Ivan Gonzalez wrote:
> Dear all,
>
> I'm using the serialization library to serialize some classes to a
> binary archive. These classes are basically arrays of some numerical
> type. As an example say they are doubles. Later I would like to read
> one of these binary archives into the same structure but using
> another numerical type, to which you could promote the first one. Say
> for example std::complex<double>. If I was using a simply a text
> file, I could read the double, without caring about its original
> type, into the new type because it will be automatically promoted.
> You can not do this with the binary file.
>
> So my question is, is there any way to extract information about the
> class stored in the binary archive, in this case the numerical type
> of the array?
>
> I thought in using the version number of the class to label the
> original type, is this the only way?
>
> In coding:
>
> template<class T>
> struct SimpleClass{
>
> typedef T num_type;
> num_type data_;
>
> template<class Archive>
> void serialize(Archive ar, const unsigned int version)
> {
> ar & data_;
> }
> };
>
> Now given a binary archive containing a instance of
> SimpleClass<double>, how to read it to an instance of
> SimpleClass<std::complex<double> > ? If I could get directly from the
> file the type SimpleClass::num_type then I could write a constructor
> in the SimpleClass to promote one to the other.
>
> Thanks in advance.
>
> Best,
>
> Ivan


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