Results 1 to 3 of 3

Thread: serialization & data integrity

  1. #1
    Join Date
    Jan 2007
    Posts
    68
    Thanks
    9
    Thanked 8 Times in 8 Posts

    Default serialization & data integrity

    hi all,

    I have a little question about serialization, shown by the following example:
    Qt Code:
    1. class MyClass
    2. {
    3. public:
    4. MyClass(){}
    5. ~MyClass(){}
    6. void save(QDataStream &out){
    7. out << (qint32)_id;
    8. out << (qint32)_sizeX;
    9. out << (qint32)_sizeY;
    10. out << (qint32)_sizeZ;
    11. }
    12. void load_Version1(QDataStream &in){
    13. qint32 id, sizeX, sizeY, sizeZ;
    14. in >> id;
    15. in >> sizeX;
    16. in >> sizeY;
    17. in >> sizeZ;
    18. _id = id;
    19. _sizeX = sizeX;
    20. _sizeY = sizeY;
    21. _sizeZ = sizeZ;
    22. }
    23. void load_Version2(QDataStream &in){
    24. in >> _id;
    25. in >> _sizeX;
    26. in >> _sizeY;
    27. in >> _sizeZ;
    28. }
    29. private:
    30. int _id;
    31. int _sizeX;
    32. int _sizeY;
    33. int _sizeZ;
    34. };
    To copy to clipboard, switch view to plain text mode 

    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
    Qt Code:
    1. in >> (qint32)_id;
    To copy to clipboard, switch view to plain text mode 
    work?

    I hope you get, what I talk about

    greetz
    darksaga

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: serialization & data integrity

    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.
    J-P Nurmi

  3. #3
    Join Date
    Jan 2007
    Posts
    68
    Thanks
    9
    Thanked 8 Times in 8 Posts

    Default Re: serialization & data integrity

    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 use the detour
    Qt Code:
    1. qint32 id;
    2. in >> id;
    3. _id = id;
    To copy to clipboard, switch view to plain text mode 

    only example I found that uses the direct attempt is here
    here they use:
    Qt Code:
    1. in >> (qint32) _id;
    To copy to clipboard, switch view to plain text mode 

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

Similar Threads

  1. QSqlQueryModel data update
    By psi in forum Qt Programming
    Replies: 4
    Last Post: 20th July 2012, 04:59
  2. QByteArray with network data
    By merlvingian in forum Qt Programming
    Replies: 1
    Last Post: 1st June 2007, 18:53
  3. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 13:53
  4. How to convert binary data to hexadecimal data
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 8th March 2006, 17:17
  5. Replies: 16
    Last Post: 7th March 2006, 16:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.