Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static members and boost serialization

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?

like image 350
Pieter Avatar asked Mar 20 '09 13:03

Pieter


2 Answers

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.

like image 177
arul Avatar answered Oct 20 '22 13:10

arul


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.

like image 43
Éric Malenfant Avatar answered Oct 20 '22 13:10

Éric Malenfant