Qt types stream operators, documentation updated, custom types support improved, bug fixes and new tests.

This commit is contained in:
Isaikin Roman committed 2015-09-12 16:48:21 +03:00
1 parent 4da435044d
commit 5761f66683
25 files changed
+1244 -102

No files matched your search

+6 -2
View File
@@ -1,5 +1,6 @@
set(qmsgpack_srcs msgpack.cpp msgpackcommon.cpp msgpackstream.cpp private/pack_p.cpp private/unpack_p.cpp private/qt_types_p.cpp)
set(qmsgpack_srcs msgpack.cpp msgpackcommon.cpp msgpackstream.cpp private/pack_p.cpp private/unpack_p.cpp private/qt_types_p.cpp stream/time.cpp stream/geometry.cpp)
set(qmsgpack_headers msgpack.h msgpackstream.h msgpackcommon.h msgpack_export.h endianhelper.h)
set(qmsgpack_stream_headers stream/location.h stream/time.h stream/geometry.h)
add_library(qmsgpack SHARED ${qmsgpack_srcs} ${qmsgpack_headers})
@@ -31,4 +32,7 @@ install(TARGETS qmsgpack EXPORT qmsgpack-export
LIBRARY DESTINATION ${LIB_INSTALL_DIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin
ARCHIVE DESTINATION ${LIB_INSTALL_DIR}
PUBLIC_HEADER DESTINATION ${INCLUDE_INSTALL_DIR}/qmsgpack)
PUBLIC_HEADER DESTINATION ${INCLUDE_INSTALL_DIR}/qmsgpack)
install(FILES ${qmsgpack_stream_headers} DESTINATION ${INCLUDE_INSTALL_DIR}/qmsgpack/stream)
+5
View File
@@ -38,6 +38,11 @@ bool MsgPack::registerUnpacker(qint8 msgpackType, MsgPack::unpack_user_f unpacke
return MsgPackPrivate::register_unpacker(msgpackType, unpacker);
}
qint8 MsgPack::msgpackType(int qType)
{
return MsgPackPrivate::msgpack_type((QMetaType::Type)qType);
}
bool MsgPack::registerType(QMetaType::Type qType, quint8 msgpackType)
{
return MsgPackPrivate::register_qtype(qType, msgpackType);
+1
View File
@@ -14,6 +14,7 @@ namespace MsgPack
MSGPACK_EXPORT QByteArray pack(const QVariant &variant);
MSGPACK_EXPORT bool registerPacker(QMetaType::Type qType, qint8 msgpackType, pack_user_f packer);
MSGPACK_EXPORT qint8 msgpackType(int qType);
MSGPACK_EXPORT bool registerType(QMetaType::Type qType, quint8 msgpackType);
+83 -2
View File
@@ -322,7 +322,47 @@ MsgPackStream &MsgPackStream::operator>>(QByteArray &array)
bool MsgPackStream::readBytes(char *data, uint len)
{
CHECK_STREAM_PRECOND(false);
return dev->read(data, len) == len;
uint readed = dev->read(data, len);
if (readed != len) {
setStatus(ReadPastEnd);
return false;
}
return true;
}
bool MsgPackStream::readExtHeader(quint32 &len)
{
CHECK_STREAM_PRECOND(false);
quint8 d[6];
if (dev->read((char*)d, 2) != 2) {
setStatus(ReadPastEnd);
return false;
}
if (d[0] >= MsgPack::FirstByte::FIXEXT1 &&
d[0] <= MsgPack::FirstByte::FIXEX16) {
len = 1;
len <<= d[0] - MsgPack::FirstByte::FIXEXT1;
return true;
}
quint8 toRead = 1;
toRead <<= d[0] - MsgPack::FirstByte::EXT8;
if (dev->read((char*)&d[2], toRead) != toRead) {
setStatus(ReadPastEnd);
return false;
}
if (d[0] == MsgPack::FirstByte::EXT8) {
len = d[1];
} else if (d[0] == MsgPack::FirstByte::EXT16) {
len = _msgpack_load16(quint32, &d[1]);
} else if (d[0] == MsgPack::FirstByte::EXT32) {
len = _msgpack_load32(quint32, &d[1]);
} else {
setStatus(ReadCorruptData);
return false;
}
return true;
}
MsgPackStream &MsgPackStream::operator<<(bool b)
@@ -440,8 +480,49 @@ MsgPackStream &MsgPackStream::operator<<(QByteArray array)
bool MsgPackStream::writeBytes(const char *data, uint len)
{
CHECK_STREAM_WRITE_PRECOND(false);
if (dev->write(data, len) != len)
if (dev->write(data, len) != len) {
setStatus(WriteFailed);
return false;
}
return true;
}
bool MsgPackStream::writeExtHeader(quint32 len, qint8 msgpackType)
{
CHECK_STREAM_WRITE_PRECOND(false);
quint8 d[6];
d[1] = msgpackType;
quint8 sz = 2;
if (len == 1) {
d[0] = 0xd4;
} else if (len == 2) {
d[0] = 0xd5;
} else if (len == 4) {
d[0] = 0xd6;
} else if (len == 8) {
d[0] = 0xd7;
} else if (len == 16) {
d[0] = 0xd8;
} else if (len <= std::numeric_limits<quint8>::max()) {
d[0] = 0xc7;
d[1] = (quint8)len;
d[2] = msgpackType;
sz = 3;
} else if (len <= std::numeric_limits<quint16>::max()) {
d[0] = 0xc8;
_msgpack_store16(&d[1], len);
d[3] = msgpackType;
sz = 4;
} else {
d[0] = 0xc9;
_msgpack_store32(&d[1], len);
d[5] = msgpackType;
sz = 6;
}
if (dev->write((const char *)d, sz) != sz) {
setStatus(WriteFailed);
return false;
}
return true;
}
+2
View File
@@ -40,6 +40,7 @@ public:
MsgPackStream &operator>>(QString &str);
MsgPackStream &operator>>(QByteArray &array);
bool readBytes(char *data, uint len);
bool readExtHeader(quint32 &len);
MsgPackStream &operator<<(bool b);
MsgPackStream &operator<<(quint32 u32);
@@ -52,6 +53,7 @@ public:
MsgPackStream &operator<<(const char *str);
MsgPackStream &operator<<(QByteArray array);
bool writeBytes(const char *data, uint len);
bool writeExtHeader(quint32 len, qint8 msgpackType);
private:
QIODevice *dev;
+10 -4
View File
@@ -47,6 +47,8 @@ quint8 *MsgPackPrivate::pack(const QVariant &v, quint8 *p, bool wr, QVector<QByt
t = (QMetaType::Type)v.userType();
QReadLocker locker(&packers_lock);
bool has_packer = user_packers.contains(t);
if (has_packer)
has_packer &= user_packers[t].packer != 0;
locker.unlock();
if (has_packer)
p = pack_user(v, p, wr, user_data);
@@ -309,10 +311,6 @@ quint8 *MsgPackPrivate::pack_map(const QVariantMap &map, quint8 *p, bool wr, QVe
bool MsgPackPrivate::register_packer(QMetaType::Type q_type, qint8 msgpack_type, MsgPack::pack_user_f packer)
{
if (packer == 0) {
qWarning() << "MsgPack::packer for qtype" << q_type << "is invalid";
return false;
}
QWriteLocker locker(&packers_lock);
if (user_packers.contains(q_type)) {
qWarning() << "MsgPack::packer for qtype" << q_type << "already exist";
@@ -325,6 +323,14 @@ bool MsgPackPrivate::register_packer(QMetaType::Type q_type, qint8 msgpack_type,
return true;
}
qint8 MsgPackPrivate::msgpack_type(QMetaType::Type q_type)
{
QReadLocker locker(&packers_lock);
if (user_packers.contains(q_type))
return user_packers[q_type].type;
return -1;
}
quint8 *MsgPackPrivate::pack_user(const QVariant &v, quint8 *p, bool wr, QVector<QByteArray> &user_data)
{
QMetaType::Type t = (QMetaType::Type)v.type() == QMetaType::User ?
+2
View File
@@ -1,3 +1,4 @@
#ifndef PACK_P_H
#define PACK_P_H
@@ -19,6 +20,7 @@ typedef struct {
qint8 type;
} packer_t;
bool register_packer(QMetaType::Type q_type, qint8 msgpack_type, MsgPack::pack_user_f packer);
qint8 msgpack_type(QMetaType::Type q_type);
extern QHash<QMetaType::Type, packer_t> user_packers;
extern QReadWriteLock packers_lock;
extern bool compatibilityMode;
+69 -52
View File
@@ -3,13 +3,19 @@
#include "unpack_p.h"
#include "../msgpackstream.h"
#include "../endianhelper.h"
#include "../stream/time.h"
#include <QDebug>
#ifdef QT_GUI_LIB
#include <QColor>
#else
#define NO_QTGUI_WARNING "qmsgpack was built without QtGui, hence some types are not available"
#endif
#ifdef QT_LOCATION_LIB
#include <QGeoCoordinate>
#endif
#define NO_QTGUI_WARNING "Library built without QtGui, hence some types are not available"
#include <QTime>
#include <QRect>
@@ -24,6 +30,13 @@ bool MsgPackPrivate::register_qtype(QMetaType::Type q_type, quint8 msgpack_type)
qWarning() << NO_QTGUI_WARNING;
return false;
#endif //QT_GUI_LIB
#ifdef QT_LOCATION_LIB
} else if ((int)q_type == qMetaTypeId<QGeoCoordinate>()) {
MsgPackPrivate::register_packer((QMetaType::Type)qMetaTypeId<QGeoCoordinate>(),
msgpack_type,
pack_qgeocoordinate);
MsgPackPrivate::register_unpacker(msgpack_type, unpack_qgeocoordinate);
#endif
} else if (q_type == QMetaType::QTime) {
MsgPackPrivate::register_packer(q_type, msgpack_type, pack_qtime);
MsgPackPrivate::register_unpacker(msgpack_type, unpack_qtime);
@@ -49,9 +62,11 @@ bool MsgPackPrivate::register_qtype(QMetaType::Type q_type, quint8 msgpack_type)
#ifdef QT_GUI_LIB
QByteArray MsgPackPrivate::pack_qcolor(const QVariant &variant)
{
QColor color = variant.value<QColor>();
if (!color.isValid())
return QByteArray(1, 0);
QByteArray data;
data.resize(4);
QColor color = variant.value<QColor>();
quint8 *p = (quint8 *)data.data();
p[0] = color.red();
p[1] = color.green();
@@ -62,93 +77,83 @@ QByteArray MsgPackPrivate::pack_qcolor(const QVariant &variant)
QVariant MsgPackPrivate::unpack_qcolor(const QByteArray &data)
{
if (data.length() == 1)
return QColor();
quint8 *p = (quint8 *)data.data();
return QColor(p[0], p[1], p[2], p[3]);
}
#endif // QT_GUI_LIB
// Date and Time
void MsgPackPrivate::pack_qtime_raw(const QTime &time, quint8 *p)
#ifdef QT_LOCATION_LIB
QByteArray MsgPackPrivate::pack_qgeocoordinate(const QVariant &variant)
{
quint8 hm, ms;
hm = (quint8)time.hour() << 4;
hm |= (quint8)time.minute() >> 2;
ms = ((quint8)time.minute() << 6) & 0xc0; // 11000000
ms |= (quint8)time.second();
p[0] = hm; p[1] = ms;
if (time.msec() != 0) {
p[2] = (quint8)( (quint16)time.msec() >> 8 );
p[3] = (quint8)time.msec();
}
QGeoCoordinate c = variant.value<QGeoCoordinate>();
if (!c.isValid())
return QByteArray(1, 0);
QByteArray ba;
MsgPackStream out(&ba, QIODevice::WriteOnly);
out << c.latitude() << c.longitude();
return ba;
}
QTime MsgPackPrivate::unpack_qtime_raw(quint8 *p, bool with_ms)
QVariant MsgPackPrivate::unpack_qgeocoordinate(const QByteArray &data)
{
quint8 h, m, s;
quint16 ms = 0;
h = p[0] >> 4;
m = (p[0] << 2) | (p[1] >> 6);
m &= 0x3f; // 00111111
s = p[1] & 0x3f;
if (with_ms)
ms = (p[2] << 8) | p[3];
return QTime(h, m, s, ms);
if (data.length() == 1)
return QVariant::fromValue(QGeoCoordinate());
MsgPackStream in(data);
QGeoCoordinate c;
double x;
in >> x;
c.setLatitude(x);
in >> x;
c.setLongitude(x);
return QVariant::fromValue(c);
}
#endif // QT_LOCATION_LIB
QByteArray MsgPackPrivate::pack_qtime(const QVariant &variant)
{
QTime time = variant.toTime();
if (!time.isValid())
return QByteArray(1, 0);
quint8 size = time.msec() == 0 ? 2 : 4;
QByteArray data;
data.resize(size);
data.data()[0] = 0;
pack_qtime_raw(time, (quint8 *)data.data());
return data;
}
QVariant MsgPackPrivate::unpack_qtime(const QByteArray &data)
{
if (data.size() == 1)
return QTime();
return unpack_qtime_raw((quint8 *)data.data(), data.size() == 4);
}
void MsgPackPrivate::pack_qdate_raw(const QDate &date, quint8 *p)
{
quint16 year = date.year();
quint8 month = date.month();
quint8 day = date.day();
if (day > 15)
year |= 0x8000;
quint8 md = (month << 4) | (day & 0xf);
_msgpack_store16(p, year);
p[2] = md;
}
QDate MsgPackPrivate::unpack_qdate_raw(quint8 *p)
{
quint16 year = _msgpack_load16(quint16, p);
quint8 month = (p[2] & 0xf0) >> 4;
quint8 day = p[2] & 0xf;
day |= (quint8)((year & 0x8000) >> 11);
year &= 0x7fff;
return QDate(year, month, day);
}
QByteArray MsgPackPrivate::pack_qdate(const QVariant &variant)
{
QDate date = variant.toDate();
if (!date.isValid())
return QByteArray(1, 0);
QByteArray data;
data.resize(3);
pack_qdate_raw(variant.toDate(), (quint8 *)data.data());
pack_qdate_raw(date, (quint8 *)data.data());
return data;
}
QVariant MsgPackPrivate::unpack_qdate(const QByteArray &data)
{
if (data.length() == 1)
return QDate();
return unpack_qdate_raw((quint8 *)data.data());
}
QByteArray MsgPackPrivate::pack_qdatetime(const QVariant &variant)
{
QDateTime dt = variant.toDateTime();
if (!dt.isValid())
return QByteArray(1, 0);
quint8 time_size = dt.time().msec() == 0 ? 2 : 4;
QByteArray data;
data.resize(3 + time_size);
@@ -161,6 +166,8 @@ QByteArray MsgPackPrivate::pack_qdatetime(const QVariant &variant)
QVariant MsgPackPrivate::unpack_qdatetime(const QByteArray &data)
{
if (data.length() == 1)
return QDateTime();
quint8 *p = (quint8 *)data.data();
QDate d = unpack_qdate_raw(p);
QTime t = unpack_qtime_raw(p + 3, data.size() == 7);
@@ -170,33 +177,40 @@ QVariant MsgPackPrivate::unpack_qdatetime(const QByteArray &data)
// Points and Vectors
QByteArray MsgPackPrivate::pack_qpoint(const QVariant &variant)
{
QPoint pt = variant.toPoint();
if (pt.isNull())
return QByteArray(1, 0);
QByteArray packed;
MsgPackStream stream(&packed, QIODevice::WriteOnly);
QPoint pt = variant.toPoint();
stream << pt.x() << pt.y();
return packed;
}
QVariant MsgPackPrivate::unpack_qpoint(const QByteArray &data)
{
if (data.length() == 1)
return QPoint();
MsgPackStream stream(data);
qint32 x, y;
stream >> x >> y;
qDebug() << "unpack qpoint stream" << (stream.status() == MsgPackStream::Ok);
return QPoint(x, y);
}
QByteArray MsgPackPrivate::pack_qsize(const QVariant &variant)
{
QSize sz = variant.toSize();
if (!sz.isValid())
return QByteArray(1, 0);
QByteArray packed;
MsgPackStream stream(&packed, QIODevice::WriteOnly);
QSize sz = variant.toSize();
stream << sz.width() << sz.height();
return packed;
}
QVariant MsgPackPrivate::unpack_qsize(const QByteArray &data)
{
if (data.length() == 1)
return QSize();
MsgPackStream stream(data);
qint32 width, height;
stream >> width >> height;
@@ -206,6 +220,8 @@ QVariant MsgPackPrivate::unpack_qsize(const QByteArray &data)
QByteArray MsgPackPrivate::pack_qrect(const QVariant &variant)
{
QRect rect = variant.toRect();
if (!rect.isValid())
return QByteArray(1, 0);
QPoint pt1 = rect.topLeft();
QPoint pt2 = rect.bottomRight();
QByteArray packed;
@@ -216,6 +232,8 @@ QByteArray MsgPackPrivate::pack_qrect(const QVariant &variant)
QVariant MsgPackPrivate::unpack_qrect(const QByteArray &data)
{
if (data.length() == 1)
return QRect();
MsgPackStream stream(data);
qint32 x, y;
stream >> x >> y;
@@ -225,4 +243,3 @@ QVariant MsgPackPrivate::unpack_qrect(const QByteArray &data)
rect.setBottomRight(QPoint(x, y));
return rect;
}
+6 -5
View File
@@ -13,6 +13,11 @@ QByteArray pack_qcolor(const QVariant &variant);
QVariant unpack_qcolor(const QByteArray &data);
#endif //QT_GUI_LIB
#ifdef QT_LOCATION_LIB
QByteArray pack_qgeocoordinate(const QVariant &variant);
QVariant unpack_qgeocoordinate(const QByteArray &data);
#endif
// Date and Time
/**
* @brief pack_qtime_raw internal: packs QTime to 4 or 2 bytes (with or without ms)
@@ -20,14 +25,12 @@ QVariant unpack_qcolor(const QByteArray &data);
* @param p pointer to preallocated array
* format: (bits) hhhhmmmm mmssssss [------ms msmsmsms]
*/
void pack_qtime_raw(const QTime &time, quint8 *p); // return 2 - without ms, 4 with ms
/**
* @brief unpack_qtime_raw internal: unpack 2 or 4 bytes to QTime
* @param p data to unpack
* @param with_ms true if array is 4 bytes (i.e. unpack with ms)
* @return QTime
*/
QTime unpack_qtime_raw(quint8 *p, bool with_ms);
QByteArray pack_qtime(const QVariant &variant);
QVariant unpack_qtime(const QByteArray &data);
@@ -37,9 +40,7 @@ QVariant unpack_qtime(const QByteArray &data);
* @param p pointer to preallocated array
* format: (bits) d(5th bit)xyyyyyyyyyyyyyy, mmmmdddd
*/
void pack_qdate_raw(const QDate &date, quint8 *p);
/// @brief internal: unpack bytes to QDate
QDate unpack_qdate_raw(quint8 *p);
QByteArray pack_qdate(const QVariant &variant);
QVariant unpack_qdate(const QByteArray &data);
+11 -5
View File
@@ -1,5 +1,4 @@
QT += core
QT -= gui
QT += core gui location
TARGET = qmsgpack
CONFIG -= app_bundle
@@ -20,7 +19,10 @@ SOURCES += msgpack.cpp \
private/pack_p.cpp \
private/unpack_p.cpp \
private/qt_types_p.cpp \
msgpackstream.cpp
msgpackstream.cpp \
stream/time.cpp \
stream/geometry.cpp \
stream/location.cpp
HEADERS += \
msgpack.h \
@@ -30,7 +32,10 @@ HEADERS += \
msgpackcommon.h \
msgpack_export.h \
private/qt_types_p.h \
msgpackstream.h
msgpackstream.h \
stream/location.h \
stream/time.h \
stream/geometry.h
HEADERS_INSTALL = \
msgpack.h \
@@ -40,7 +45,8 @@ HEADERS_INSTALL = \
msgpackstream.h \
STREAM_HEADERS_INSTALL = \
stream/location.h
stream/location.h \
stream/time.h
unix {
header_files.files = $$HEADERS_INSTALL
+117
View File
@@ -0,0 +1,117 @@
#include "geometry.h"
#include "../msgpack.h"
#include "../msgpackstream.h"
MsgPackStream &operator<<(MsgPackStream &s, const QPoint &point)
{
qint8 msgpackType = MsgPack::msgpackType(QMetaType::QPoint);
if (msgpackType == -1) {
s.setStatus(MsgPackStream::WriteFailed);
return s;
}
QByteArray ba;
MsgPackStream out(&ba, QIODevice::WriteOnly);
if (point.isNull()) {
quint8 p[1] = {0};
out.writeBytes((const char *)p, 1);
} else {
out << point.x() << point.y();
}
s.writeExtHeader(ba.length(), msgpackType);
s.writeBytes(ba.data(), ba.length());
return s;
}
MsgPackStream &operator>>(MsgPackStream &s, QPoint &point)
{
quint32 len;
s.readExtHeader(len);
if (len == 1) {
point = QPoint();
return s;
}
QByteArray ba;
ba.resize(len);
s.readBytes(ba.data(), len);
MsgPackStream in(ba);
int x, y;
in >> x >> y;
point = QPoint(x, y);
return s;
}
MsgPackStream &operator<<(MsgPackStream &s, const QSize &sz)
{
qint8 msgpackType = MsgPack::msgpackType(QMetaType::QSize);
if (msgpackType == -1) {
s.setStatus(MsgPackStream::WriteFailed);
return s;
}
QByteArray ba;
MsgPackStream out(&ba, QIODevice::WriteOnly);
if (!sz.isValid()) {
quint8 p[1] = {0};
out.writeBytes((const char *)p, 1);
} else {
out << sz.width() << sz.height();
}
s.writeExtHeader(ba.length(), msgpackType);
s.writeBytes(ba.data(), ba.length());
return s;
}
MsgPackStream &operator>>(MsgPackStream &s, QSize &sz)
{
quint32 len;
s.readExtHeader(len);
if (len == 1) {
sz = QSize();
return s;
}
QByteArray ba;
ba.resize(len);
s.readBytes(ba.data(), len);
MsgPackStream in(ba);
int w, h;
in >> w >> h;
sz = QSize(w, h);
return s;
}
MsgPackStream &operator<<(MsgPackStream &s, const QRect &rect)
{
qint8 msgpackType = MsgPack::msgpackType(QMetaType::QRect);
if (msgpackType == -1) {
s.setStatus(MsgPackStream::WriteFailed);
return s;
}
QByteArray ba;
MsgPackStream out(&ba, QIODevice::WriteOnly);
if (!rect.isValid()) {
quint8 p[1] = {0};
out.writeBytes((const char *)p, 1);
} else {
out << rect.left() << rect.top() << rect.width() << rect.height();
}
s.writeExtHeader(ba.length(), msgpackType);
s.writeBytes(ba.data(), ba.length());
return s;
}
MsgPackStream &operator>>(MsgPackStream &s, QRect &rect)
{
quint32 len;
s.readExtHeader(len);
if (len == 1) {
rect = QRect();
return s;
}
QByteArray ba;
ba.resize(len);
s.readBytes(ba.data(), len);
MsgPackStream in(ba);
int l, t, w, h;
in >> l >> t >> w >> h;
rect = QRect(l, t, w, h);
return s;
}
+19
View File
@@ -0,0 +1,19 @@
#ifndef GEOMETRY_H
#define GEOMETRY_H
#include "../msgpack_export.h"
#include "../msgpackstream.h"
#include <QRect>
MSGPACK_EXPORT MsgPackStream &operator<<(MsgPackStream& s, const QPoint &point);
MSGPACK_EXPORT MsgPackStream& operator>>(MsgPackStream& s, QPoint &point);
MSGPACK_EXPORT MsgPackStream &operator<<(MsgPackStream& s, const QSize &sz);
MSGPACK_EXPORT MsgPackStream& operator>>(MsgPackStream& s, QSize &sz);
MSGPACK_EXPORT MsgPackStream &operator<<(MsgPackStream& s, const QRect &rect);
MSGPACK_EXPORT MsgPackStream& operator>>(MsgPackStream& s, QRect &rect);
#endif // GEOMETRY_H
+34
View File
@@ -0,0 +1,34 @@
#ifdef QT_LOCATION_LIB
#include "location.h"
#include "../msgpack.h"
MsgPackStream& operator>>(MsgPackStream& s, QGeoCoordinate &coordinate)
{
quint32 len;
s.readExtHeader(len);
if (len != 18) {
s.setStatus(MsgPackStream::ReadCorruptData);
return s;
}
double x;
s >> x;
coordinate.setLatitude(x);
s >> x;
coordinate.setLongitude(x);
return s;
}
MsgPackStream &operator<<(MsgPackStream& s, const QGeoCoordinate &coordinate)
{
qint8 msgpackType = MsgPack::msgpackType((QMetaType::Type)qMetaTypeId<QGeoCoordinate>());
if (msgpackType == -1) {
s.setStatus(MsgPackStream::WriteFailed);
return s;
}
s.writeExtHeader(18, msgpackType);
s << coordinate.latitude() << coordinate.longitude();
return s;
}
#endif
+7 -16
View File
@@ -1,24 +1,15 @@
#ifndef QMSGPACK_STREAM_LOCATION_H
#define QMSGPACK_STREAM_LOCATION_H
#ifdef QT_LOCATION_LIB
#include "../msgpack_export.h"
#include "../msgpackstream.h"
#include <QGeoCoordinate>
MsgPackStream& operator>>(MsgPackStream& s, QGeoCoordinate &coordinate)
{
double x;
s >> x;
coordinate.setLatitude(x);
s >> x;
coordinate.setLongitude(x);
return s;
}
MSGPACK_EXPORT MsgPackStream& operator>>(MsgPackStream& s, QGeoCoordinate &coordinate);
MSGPACK_EXPORT MsgPackStream &operator<<(MsgPackStream& s, const QGeoCoordinate &coordinate);
MsgPackStream &operator<<(MsgPackStream& s, const QGeoCoordinate &coordinate)
{
s << coordinate.latitude() << coordinate.longitude();
return s;
}
#endif // QMSGPACK_STREAM_LOCATION_H
#endif // QT_LOCATION_LIB
#endif // QMSGPACK_STREAM_LOCATION_H
+166
View File
@@ -0,0 +1,166 @@
#include "time.h"
#include "../msgpack.h"
void MsgPackPrivate::pack_qtime_raw(const QTime &time, quint8 *p)
{
quint8 hm, ms;
hm = (quint8)time.hour() << 4;
hm |= (quint8)time.minute() >> 2;
ms = ((quint8)time.minute() << 6) & 0xc0; // 11000000
ms |= (quint8)time.second();
p[0] = hm; p[1] = ms;
if (time.msec() != 0) {
p[2] = (quint8)( (quint16)time.msec() >> 8 );
p[3] = (quint8)time.msec();
}
}
QTime MsgPackPrivate::unpack_qtime_raw(quint8 *p, bool with_ms)
{
quint8 h, m, s;
quint16 ms = 0;
h = p[0] >> 4;
m = (p[0] << 2) | (p[1] >> 6);
m &= 0x3f; // 00111111
s = p[1] & 0x3f;
if (with_ms)
ms = (p[2] << 8) | p[3];
return QTime(h, m, s, ms);
}
void MsgPackPrivate::pack_qdate_raw(const QDate &date, quint8 *p)
{
quint16 year = date.year();
quint8 month = date.month();
quint8 day = date.day();
if (day > 15)
year |= 0x8000;
quint8 md = (month << 4) | (day & 0xf);
_msgpack_store16(p, year);
p[2] = md;
}
QDate MsgPackPrivate::unpack_qdate_raw(quint8 *p)
{
quint16 year = _msgpack_load16(quint16, p);
quint8 month = (p[2] & 0xf0) >> 4;
quint8 day = p[2] & 0xf;
day |= (quint8)((year & 0x8000) >> 11);
year &= 0x7fff;
return QDate(year, month, day);
}
MsgPackStream &operator<<(MsgPackStream &s, const QTime &t)
{
qint8 msgpackType = MsgPack::msgpackType(QMetaType::QTime);
if (msgpackType == -1) {
s.setStatus(MsgPackStream::WriteFailed);
return s;
}
quint8 len = 1;
if (t.isValid())
len = t.msec() == 0 ? 2 : 4;
s.writeExtHeader(len, msgpackType);
quint8 p[4] = {0};
if (len != 1)
MsgPackPrivate::pack_qtime_raw(t, p);
s.writeBytes((const char *)p, len);
return s;
}
MsgPackStream& operator>>(MsgPackStream& s, QTime &t)
{
quint32 len;
s.readExtHeader(len);
if (len != 1 && len != 2 && len != 4) {
s.setStatus(MsgPackStream::ReadCorruptData);
t = QTime();
return s;
}
if (len == 1) {
t == QTime();
return s;
}
quint8 p[4];
s.readBytes((char *)p, len);
t = MsgPackPrivate::unpack_qtime_raw(p, len == 4);
return s;
}
MsgPackStream &operator<<(MsgPackStream &s, const QDate &date)
{
qint8 msgpackType = MsgPack::msgpackType(QMetaType::QDate);
if (msgpackType == -1) {
s.setStatus(MsgPackStream::WriteFailed);
return s;
}
quint8 len = date.isValid() ? 3 : 1;
s.writeExtHeader(len, msgpackType);
quint8 p[3] = {0};
if (len != 1)
MsgPackPrivate::pack_qdate_raw(date, p);
s.writeBytes((const char *)p, len);
return s;
}
MsgPackStream& operator>>(MsgPackStream& s, QDate &date)
{
quint32 len;
s.readExtHeader(len);
if (len != 1 && len != 3) {
s.setStatus(MsgPackStream::ReadCorruptData);
date = QDate();
return s;
}
if (len == 1) {
date = QDate();
return s;
}
quint8 p[3];
s.readBytes((char *)p, len);
date = MsgPackPrivate::unpack_qdate_raw(p);
return s;
}
MsgPackStream &operator<<(MsgPackStream& s, const QDateTime &dt)
{
qint8 msgpackType = MsgPack::msgpackType(QMetaType::QDateTime);
if (msgpackType == -1) {
s.setStatus(MsgPackStream::WriteFailed);
return s;
}
quint8 len = 1;
if (dt.isValid()) {
len = 3;
len += dt.time().msec() == 0 ? 2 : 4;
}
s.writeExtHeader(len, msgpackType);
quint8 p[7] = {0};
if (len != 1) {
MsgPackPrivate::pack_qdate_raw(dt.date(), p);
MsgPackPrivate::pack_qtime_raw(dt.time(), p + 3);
}
s.writeBytes((const char *)p, len);
return s;
}
MsgPackStream& operator>>(MsgPackStream& s, QDateTime &dt)
{
quint32 len;
s.readExtHeader(len);
if (len != 1 && len != 5 && len != 7) {
s.setStatus(MsgPackStream::ReadCorruptData);
dt = QDateTime();
return s;
}
if (len == 1) {
dt = QDateTime();
return s;
}
quint8 p[7];
s.readBytes((char *)p, len);
dt.setDate(MsgPackPrivate::unpack_qdate_raw(p));
dt.setTime(MsgPackPrivate::unpack_qtime_raw(p + 3, len == 7));
return s;
}
+26
View File
@@ -0,0 +1,26 @@
#ifndef TIME_H
#define TIME_H
#include "../msgpack_export.h"
#include "../msgpackstream.h"
#include <QDateTime>
MSGPACK_EXPORT MsgPackStream &operator<<(MsgPackStream& s, const QTime &time);
MSGPACK_EXPORT MsgPackStream& operator>>(MsgPackStream& s, QTime &time);
MSGPACK_EXPORT MsgPackStream &operator<<(MsgPackStream& s, const QDate &date);
MSGPACK_EXPORT MsgPackStream& operator>>(MsgPackStream& s, QDate &date);
MSGPACK_EXPORT MsgPackStream &operator<<(MsgPackStream& s, const QDateTime &dt);
MSGPACK_EXPORT MsgPackStream& operator>>(MsgPackStream& s, QDateTime &dt);
namespace MsgPackPrivate {
void pack_qtime_raw(const QTime &time, quint8 *p);
QTime unpack_qtime_raw(quint8 *p, bool with_ms);
void pack_qdate_raw(const QDate &date, quint8 *p);
QDate unpack_qdate_raw(quint8 *p);
}
#endif // TIME_H