Boost logo

Boost Users :

From: Bill Lear (rael_at_[hidden])
Date: 2006-06-22 10:26:24


On Thursday, June 22, 2006 at 10:55:01 (+0200) Vidal Roca writes:
>Sorry for the unclear question.
>As Bill Lear clarified, I mean the following (not archive formats):
>
>class Foo {
> template<class Archive>
> void serialize(Archive& ar, const unsigned int version);
>
> template<class Archive>
> void serialize_II(Archive& ar, const unsigned int version);
>};
>
>where serialize_II reads and writes the data structure in a different manner
>than serialize.
>
>It could be possible using versioning; but is there any other possibility?

Well, what you really want is runtime dispatch, then. Sounds like you
need a flag of some sort in your class, and then maybe if you want to
go overboard, a factory method, a set of derived classes, etc.

But, basically, using a simple flag:

class Foo {
    enum { Format1, Format2, Format3, ...};

    int format; // ...

    template <class Archive>
    void serialize1(Archive& ar, const unsigned int version) {
        // ...
    }

    template <class Archive>
    void serialize(Archive& ar, const unsigned int version) {
        switch (format) {
            case Format1: serialize1(ar, version); break;
            case Format2: serialize2(ar, version); break;
            case Format3: serialize3(ar, version); break;
            default: // ... barf
         }
    }
};

Build your runtime dispatch however you like. Here it's a class
variable, but could be a global variable, method, etc.

Not sure this is what you want.

Bill


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