[Development] Weird offseting in QDataStream
andre at familiesomers.nl
Fri Nov 8 11:32:33 CET 2013
Eskil Abrahamsen Blomfeldt schreef op 8-11-2013 11:20:
> QDataStream supports reading and writing floats and doubles, but it
> might use more bytes than necessary to represent them in the stream.
> The main use case of QDataStream is to serialize Qt data in a portable,
> binary form with the convenience that the code used to write and read
> the file are inverses of each other. One objective is that you get
> predictable results when moving your existing code and data to new
> platforms, and this was not at all the case before we standardized on a
> floating point precision for all floating point values in QDataStream
> for Qt 4.6. (For the same reason, QDataStream also assumes a particular
> endianness of the input data, and this also might have to be overridden
> manually if you want to read data that is not written by QDataStream.)
> It's pretty easy to read four bytes from a QIODevice if you do not worry
> about portability.
> Personally, having MixedPrecision/AutoPrecision seems unnecessary, since
> you would still have to be aware of the issue before you can make the
> decision to set this mode, and once you are aware of the issue, you can
> use setFloatingPointPrecision() to achieve the results you want and your
> code will still be cross-platform.
I find the case Yves make to be quite convincing. I know the main
purpose of QDataStream is to serialize and deserialize data in a
portable, binary form. But that does not exclude the usecase where you
want to read and write to a format defined outside our ecosystem, right?
If you have to deal with such an outside format, and that format uses
both 4-byte floats and 8-byte doubles in its specs, then it really is
quite inconveneant to have to switch back and forth during reading. As
Yves points out, it makes chaining the operations impossible for
instance. Also, it results in less readable code.
I don't see what's wrong with such a mode, as long as the documentation
warns that it won't work reliably with qreal (before 5.2, I read). You
can already duplicate it by setting the version back to something old.
If qreal really is a double on all platforms now by default from 5.2,
perhaps a new version would even be in order that reverts the effect of
setDoublePrecision completely? Just like it has no effect for versions <
Qt_4_6, it could have no effect for versions >= Qt_5_2. What else is it
needed for if qreal translates to double anyway?
More information about the Development