MsgPackStream: float, double

This commit is contained in:
Isaikin Roman
2015-05-23 20:00:40 +03:00
parent 2acfaf4cc5
commit 1277a0c204
3 changed files with 117 additions and 0 deletions

View File

@ -37,6 +37,8 @@ quint8 *MsgPackPrivate::pack(const QVariant &v, quint8 *p, bool wr, QVector<QByt
p = pack_ulonglong(v.toULongLong(), p, wr);
else if (t == QMetaType::Double)
p = pack_double(v.toDouble(), p, wr);
else if (t == QMetaType::Float)
p = pack_float(v.toFloat(), p, wr);
else if (t == QMetaType::QByteArray)
p = pack_bin(v.toByteArray(), p, wr);
else if (t == QMetaType::QVariantMap)
@ -209,6 +211,24 @@ quint8 *MsgPackPrivate::pack_string(const QString &str, quint8 *p, bool wr)
return pack_string_raw(str_data.data(), str_len, p, wr);
}
quint8 *MsgPackPrivate::pack_float(float f, quint8 *p, bool wr)
{
if (wr) *p = 0xca;
p++;
if (wr) {
quint8 *d = (quint8 *)&i;
#ifdef __LITTLE_ENDIAN__
for (int i = 0; i < 4; ++i)
*(p + 3 - i) = *(d + i);
#else
for (int i = 0; i < 4; ++i)
*(p + i) = *(d + i);
#endif
}
return p + 8;
}
quint8 *MsgPackPrivate::pack_double(double i, quint8 *p, bool wr)
{
if (wr) *p = 0xcb;

View File

@ -38,6 +38,7 @@ quint8 * pack_stringlist(const QStringList &list, quint8 *p, bool wr);
quint8 * pack_string_raw(const char *str, quint32 len, quint8 *p, bool wr);
quint8 * pack_string(const QString &str, quint8 *p, bool wr);
quint8 * pack_float(float f, quint8 *p, bool wr);
quint8 * pack_double(double i, quint8 *p, bool wr);
quint8 * pack_bin(const QByteArray &arr, quint8 *p, bool wr);
quint8 * pack_map(const QVariantMap &map, quint8 *p, bool wr, QVector<QByteArray> &user_data);

View File

@ -204,6 +204,52 @@ MsgPackStream &MsgPackStream::operator>>(qint64 &i64)
return *this;
}
MsgPackStream &MsgPackStream::operator>>(float &f)
{
CHECK_STREAM_PRECOND(*this);
quint8 *fp = (quint8 *)&f;
quint8 p[5];
if (dev->read((char *)&p, 1) != 1) {
setStatus(ReadPastEnd);
return *this;
}
if (p[0] != MsgPack::FirstByte::FLOAT32) {
setStatus(ReadCorruptData);
return *this;
}
#ifdef __LITTLE_ENDIAN__
for (int i = 0; i < 4; ++i)
*(fp + 3 - i) = *(p + i + 1);
#else
for (int i = 0; i < 4; ++i)
*(fp + i) = *(p + i + 1);
#endif
return *this;
}
MsgPackStream &MsgPackStream::operator>>(double &d)
{
CHECK_STREAM_PRECOND(*this);
quint8 *fp = (quint8 *)&f;
quint8 p[9];
if (dev->read((char *)&p, 1) != 1) {
setStatus(ReadPastEnd);
return *this;
}
if (p[0] != MsgPack::FirstByte::FLOAT64) {
setStatus(ReadCorruptData);
return *this;
}
#ifdef __LITTLE_ENDIAN__
for (int i = 0; i < 8; ++i)
*(fp + 7 - i) = *(p + i + 1);
#else
for (int i = 0; i < 8; ++i)
*(fp + i) = *(p + i + 1);
#endif
return *this;
}
MsgPackStream &MsgPackStream::operator>>(QString &str)
{
CHECK_STREAM_PRECOND(*this);
@ -239,6 +285,21 @@ MsgPackStream &MsgPackStream::operator>>(QString &str)
delete[] data;
}
MsgPackStream &MsgPackStream::operator>>(QByteArray &array)
{
}
MsgPackStream &MsgPackStream::operator>>(QVariantList &list)
{
}
MsgPackStream &MsgPackStream::operator>>(QVariantMap &map)
{
}
MsgPackStream &MsgPackStream::operator<<(bool b)
{
CHECK_STREAM_WRITE_PRECOND(*this);
@ -289,6 +350,26 @@ MsgPackStream &MsgPackStream::operator<<(qint64 i64)
return *this;
}
MsgPackStream &MsgPackStream::operator<<(float f)
{
CHECK_STREAM_WRITE_PRECOND(*this);
quint8 p[5];
quint8 sz = MsgPackPrivate::pack_float(f, p, true) - p;
if (dev->write((char *)p, sz) != sz)
setStatus(WriteFailed);
return *this;
}
MsgPackStream &MsgPackStream::operator<<(double d)
{
CHECK_STREAM_WRITE_PRECOND(*this);
quint8 p[9];
quint8 sz = MsgPackPrivate::pack_float(d, p, true) - p;
if (dev->write((char *)p, sz) != sz)
setStatus(WriteFailed);
return *this;
}
MsgPackStream &MsgPackStream::operator<<(QString str)
{
CHECK_STREAM_WRITE_PRECOND(*this);
@ -316,6 +397,21 @@ MsgPackStream &MsgPackStream::operator<<(const char *str)
return *this;
}
MsgPackStream &MsgPackStream::operator<<(QByteArray array)
{
}
MsgPackStream &MsgPackStream::operator<<(QVariantList list)
{
}
MsgPackStream &MsgPackStream::operator<<(QVariantMap map)
{
}
bool MsgPackStream::unpack_upto_quint8(quint8 &u8, quint8 *p)
{
if (*p <= MsgPack::FirstByte::POSITIVE_FIXINT) {