Serializing custom data object
Hey guys,
i would like to serialize a custom data class to save data when my app closes and reload it after it reopens. The data is stored in an std::vector<particle_c> (particle_c is my custom data class) and i have a few 100 - 1000 particles. Each particle has properties of int, QStrings, doubles, and some eigen3/vectors (for matrix calculations). I've been thinking about the right approach to save the data and ended up with serialization. Is this the right approach? i'm not quite sure how to serialize the two vector classes. I could probably rewrite the code to make it use QVectors if thats any help. Also read a little about the boost serialization library, could that also be an option?
thx for the input friends :)
Re: Serializing custom data object
The easiest way to do that is to use QDataStream as it can directly serialize QString and other Qt types.
You can either serialize each struct member separately in the loop over the vector or add QDataStream stream operators to the struct.
Cheers,
_
Re: Serializing custom data object
thx for the input!
so lets say i have delcare my data like this:
std::vector<particle_c> particles_all;
is this the way to implement the datastream operator for a vector?
Code:
{
for (int i=0; i<particles_all.size(),i++)
{
out << particles_all[i].mass << particles_all[i].state_rel_sun.pos(0)
<< quint32(particles_all[i].mass_option); //and loads of other stuff
return out;
}
}
do i have to declare each vartype e.g. quint32(particles_all.mass_option) or is it enough if its a qt known type and its declared in a header e.g (in header of particle_c class: double mass; )
Re: Serializing custom data object
Are you sure that std::vector has members called "mass", "state:rel_sun" or "mass_option"?
Cheers,
_
Re: Serializing custom data object
oh my bad, i edited the previous post. forgot the iterator of the vector. nice catch. wrote the code from the top of my head.
Otherwise tho the idea is ok to use the loop in the operator?
Re: Serializing custom data object
You should serialise the size of the vector before its content. When you come to deserialise the data this size value lets you size the vector properly and know when the vector stops and the next item you serialise starts.
Re: Serializing custom data object
Thx Chris, splitting the vector beforehand and then writing the operator for a custom object worked for me!