Boost logo

Boost Users :

From: Robert Ramey (ramey_at_[hidden])
Date: 2006-01-14 11:58:37

Loïc Joly wrote:
> Hello,
> I have to serialize some classes that look like this :
> struct A
> {
> vector<Base*> v;
> Base* get(int i);
> };
> struct B1: A
> {
> Derived1* get(int i);
> };
> ...
> struct B1000 : A
> {
> Derived1000* get(int i);
> };
> (well, not exactly 1000, but definitively more than a couple, and no,
> I cannot change this design).

LOL - one really has wonder about such a design! Looks like it calls
for a template if you ask me.

> I would like not to add serialization code to all of thoses classes,
> just to the base class, since all those classes are almost the same.
> Is this possible ?

I don't see how this would be possible.

What could be made to work is to make large macro which looks like:

#define BXX(bname) \
    template<class Archive> \
    void serialize(Archive &ar, bname & t, const unsigned int version){ \
        ar & base_object<A>(t);\

and include BXX(B1) in the file which includes struct B1 etc.

Of course if you start down this road then you might as well include the
whole struct in the macro.

Of course once you do that, you might as well make the template

template<int N>
struct B {
    Derived<N> * get(int i);

which is of course is really a macro with type awareness sauce.

> If I do not define anything for Bn classes, I get some
> archive_exception::unregistered_class exception.

As I would expect.

Robert Ramey

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at