Quantcast
Channel: QtWebEngine
Viewing all articles
Browse latest Browse all 13965

Does QVariant know when it has reached its memory bounds?

$
0
0
Hello everyone. I have a bit of a unique question, I think, as I wasn’t able to find a satisfactory answer elsewhere, or find a way to formulate my problem that yielded good answers through google and QtProject. I’m using QDataStream to stream certain types to a file for persistence. This works fine and well, until I realize that the datamembers of these types are very likely to change in the future. I’d like to continue using QDataStream for this, however, since it’s very fast and flexible in other areas. The scenarios are as follows: Suppose we have a project saved with a specific version number. This project contains a number of items that each have a QString somewhere in the middle of their data structure. When they get exported and imported, this string needs to be written and read in the same order, otherwise all hell breaks loose and you never know what you’ll get when the bytes are converted to the proper type. If those QStrings are removed at a later time, for some reason, or a different datamember is added before them (and written/read at that position), then what would be gotten from those streams is undefined. There doesn’t seem to be a clean fix for this, but I also don’t require one as we decided to organize the types in such a manner that data subject to change is kept as late as possible and can be held as ‘reserved’ should they ever be removed. However, one problem that does need fixing it when datamembers are added to the ends of these structures. So, suppose a newer version of the software added a QString to the back of the aforementioned types and also attempts to read these from files saved by an older version of the software (backward compatibility is key!), the result is undefined. However, I attempted to encapsulate everything, including nested lists of other structures, into QVariants, and read/write those to and from the datastream. The result surprised me that the QVariant seemed to know when it was in danger of stampeding over the memory of another object in the structure. My question is, does QVariant know when the bytes allotted to it in a buffer end? If so, does it default construct members it cannot take from a buffer that, from its point of view, is empty? I did some tests and QString seems to always get initialized to “”, while a double gets initialized to 0.0. So as far as I can tell, it seems to work. However, this code is crucial, so I want to understand what’s happening before I commit to any one solution. I hope this question wasn’t too long-winded and I apologize if it’s already been answered elsewhere, but I couldn’t find a good answer in any documentation or other question. Thank you in advance.

Viewing all articles
Browse latest Browse all 13965

Trending Articles