PDA

View Full Version : serialization & data integrity



darksaga
28th August 2007, 01:05
hi all,

I have a little question about serialization, shown by the following example:


class MyClass
{
public:
MyClass(){}
~MyClass(){}
void save(QDataStream &out){
out << (qint32)_id;
out << (qint32)_sizeX;
out << (qint32)_sizeY;
out << (qint32)_sizeZ;
}
void load_Version1(QDataStream &in){
qint32 id, sizeX, sizeY, sizeZ;
in >> id;
in >> sizeX;
in >> sizeY;
in >> sizeZ;
_id = id;
_sizeX = sizeX;
_sizeY = sizeY;
_sizeZ = sizeZ;
}
void load_Version2(QDataStream &in){
in >> _id;
in >> _sizeX;
in >> _sizeY;
in >> _sizeZ;
}
private:
int _id;
int _sizeX;
int _sizeY;
int _sizeZ;
};


using load_Version1 to load my data should be safe in terms of data integrity, afaik

doing things like in load_Version1 is a little longish, imo, especially when the list of various types of private members grows

so, would it be ok & safe to use load_Version2 to shorten things???
personally I don't think it is safe...

if load_Version2 isn't ok, as i assume, is there another possibility to shorten the amount of code?
would something like
in >> (qint32)_id; work?

I hope you get, what I talk about :rolleyes:

greetz
darksaga

jpn
28th August 2007, 08:35
I'm my opinion it's just fine to read directly to member variables. QDataStream won't attempt to assign incompatible values that would lead to a crash or so. Although you might still want to check QDataStream::status() to see if the operation succeed.

darksaga
28th August 2007, 13:04
hmm,

I'd only like to know if the direct attempt is safe to use on diffrent platforms,

all the examples I found so far in the documentation (http://doc.trolltech.com/4.3/qdatastream.html) use the detour


qint32 id;
in >> id;
_id = id;


only example I found that uses the direct attempt is here (http://lists.trolltech.com/qt-interest/2004-06/thread00024-0.html)
here they use:


in >> (qint32) _id;


so, can anybody say for certain the direct assignment to member variables is safe?