views:

465

answers:

2

I'm using Boost.Serialization to archive the contents of a class. One of the member variables is a static std::vector.

Archiving and restoring goes fine, but I was kind of hoping the library would save static members only once, it appears that, judging by the filesize, the static members are fully saved for each archived instance.

This is rather easily circumvented by using set/getters for the static vector and serializing the static vector outside the class once.

But I'd rather have a self contained class. Is there a clean and easy way to achieve archiving the static contents of a class only once?

+1  A: 

Serialize the static vector before you serialize all of the class' instances.

If you serialize the vector like this:

template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
    ar & this->someVar;
    ar & this->AnotherVar;
    ar & staticVector;  
}

Then sure, the static vector does get serialized with each instance.

Should you need any further clarification, post your serialize function and the function that invokes it, please.

arul
I don't think that's so 'sure' to be honest. Since the static instance resides at the same memory address for every class instance, I was hoping the library would pick that up somehow and be smart enough not to write the vector every time to file.
Pieter
Serializing the vector first, and then all the instances, is the 'easily circumvented' I was talking about, as I said I'd rather have a self contained serialize() for my class...But if that can't be done serializing the vector first is what I'll stick too of course (that's how it works now...)
Pieter
It is sure, Boost serializes what you tell it to serialize, regardless of its memory location, it cannot gues that wildly.Where would be the static section stored, if that somehow magically worked? At the beginning, at the end, in the middle?One way would be to use a flag like 'serialized'.
arul
... in the 'serialize' function to signal whether or not the static data have been de/serialized.
arul
+1  A: 

I have very limited experience with Boost.Serialization, so please consider what follows accordingly:

IIRC, the treatment you want for your static member is what is done with pointers. So maybe serializing a pointer to the static member would work.

Self-critique: I'm not sure how that could be applied when de-serializing, though.

Éric Malenfant