mirror of
https://github.com/romixlab/qmsgpack.git
synced 2025-07-29 18:07:16 +02:00
Refactor MsgPack::registerType() a little bit
move MsgPackPrivate::register_qtype() impl to MsgPack::registerType(); warn and return false when the requested type is not known to qmsgpack
This commit is contained in:
@ -7,6 +7,10 @@
|
|||||||
|
|
||||||
#include <QVector>
|
#include <QVector>
|
||||||
|
|
||||||
|
#ifdef QT_LOCATION_LIB
|
||||||
|
#include <QGeoCoordinate>
|
||||||
|
#endif
|
||||||
|
|
||||||
QVariant MsgPack::unpack(const QByteArray &data)
|
QVariant MsgPack::unpack(const QByteArray &data)
|
||||||
{
|
{
|
||||||
quint8 *p = (quint8 *)data.data();
|
quint8 *p = (quint8 *)data.data();
|
||||||
@ -47,7 +51,51 @@ qint8 MsgPack::msgpackType(int qType)
|
|||||||
|
|
||||||
bool MsgPack::registerType(QMetaType::Type qType, quint8 msgpackType)
|
bool MsgPack::registerType(QMetaType::Type qType, quint8 msgpackType)
|
||||||
{
|
{
|
||||||
return MsgPackPrivate::register_qtype(qType, msgpackType);
|
switch (qType) {
|
||||||
|
#ifdef QT_GUI_LIB
|
||||||
|
case QMetaType::QColor:
|
||||||
|
MsgPackPrivate::register_packer(qType, msgpackType, MsgPackPrivate::pack_qcolor);
|
||||||
|
MsgPackPrivate::register_unpacker(msgpackType, MsgPackPrivate::unpack_qcolor);
|
||||||
|
return true;
|
||||||
|
#endif // QT_GUI_LIB
|
||||||
|
case QMetaType::QTime:
|
||||||
|
MsgPackPrivate::register_packer(qType, msgpackType, MsgPackPrivate::pack_qtime);
|
||||||
|
MsgPackPrivate::register_unpacker(msgpackType, MsgPackPrivate::unpack_qtime);
|
||||||
|
return true;
|
||||||
|
case QMetaType::QDate:
|
||||||
|
MsgPackPrivate::register_packer(qType, msgpackType, MsgPackPrivate::pack_qdate);
|
||||||
|
MsgPackPrivate::register_unpacker(msgpackType, MsgPackPrivate::unpack_qdate);
|
||||||
|
return true;
|
||||||
|
case QMetaType::QDateTime:
|
||||||
|
MsgPackPrivate::register_packer(qType, msgpackType, MsgPackPrivate::pack_qdatetime);
|
||||||
|
MsgPackPrivate::register_unpacker(msgpackType, MsgPackPrivate::unpack_qdatetime);
|
||||||
|
return true;
|
||||||
|
case QMetaType::QPoint:
|
||||||
|
MsgPackPrivate::register_packer(qType, msgpackType, MsgPackPrivate::pack_qpoint);
|
||||||
|
MsgPackPrivate::register_unpacker(msgpackType, MsgPackPrivate::unpack_qpoint);
|
||||||
|
return true;
|
||||||
|
case QMetaType::QSize:
|
||||||
|
MsgPackPrivate::register_packer(qType, msgpackType, MsgPackPrivate::pack_qsize);
|
||||||
|
MsgPackPrivate::register_unpacker(msgpackType, MsgPackPrivate::unpack_qsize);
|
||||||
|
return true;
|
||||||
|
case QMetaType::QRect:
|
||||||
|
MsgPackPrivate::register_packer(qType, msgpackType, MsgPackPrivate::pack_qrect);
|
||||||
|
MsgPackPrivate::register_unpacker(msgpackType, MsgPackPrivate::unpack_qrect);
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
#ifdef QT_LOCATION_LIB
|
||||||
|
if (int(qType) == qMetaTypeId<QGeoCoordinate>()) {
|
||||||
|
MsgPackPrivate::register_packer(qType, msgpackType, MsgPackPrivate::pack_qgeocoordinate);
|
||||||
|
MsgPackPrivate::register_unpacker(msgpackType, MsgPackPrivate::unpack_qgeocoordinate);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#endif // QT_LOCATION_LIB
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
qWarning("qmsgpack was built without metatype %d support.", int(qType));
|
||||||
|
qWarning("Use MsgPack::registerPacker() and MsgPack::registerUnpacker() to register metatype %d manually.", int(qType));
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MsgPack::setCompatibilityModeEnabled(bool enabled)
|
void MsgPack::setCompatibilityModeEnabled(bool enabled)
|
||||||
|
@ -11,8 +11,6 @@
|
|||||||
|
|
||||||
#ifdef QT_GUI_LIB
|
#ifdef QT_GUI_LIB
|
||||||
#include <QColor>
|
#include <QColor>
|
||||||
#else
|
|
||||||
#define NO_QTGUI_WARNING "qmsgpack was built without QtGui, hence some types are not available"
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef QT_LOCATION_LIB
|
#ifdef QT_LOCATION_LIB
|
||||||
@ -22,45 +20,6 @@
|
|||||||
#include <QTime>
|
#include <QTime>
|
||||||
#include <QRect>
|
#include <QRect>
|
||||||
|
|
||||||
bool MsgPackPrivate::register_qtype(QMetaType::Type q_type, quint8 msgpack_type)
|
|
||||||
{
|
|
||||||
if (q_type == QMetaType::QColor) {
|
|
||||||
#ifdef QT_GUI_LIB
|
|
||||||
MsgPackPrivate::register_packer(q_type, msgpack_type, pack_qcolor);
|
|
||||||
MsgPackPrivate::register_unpacker(msgpack_type, unpack_qcolor);
|
|
||||||
#else
|
|
||||||
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);
|
|
||||||
} else if (q_type == QMetaType::QDate) {
|
|
||||||
MsgPackPrivate::register_packer(q_type, msgpack_type, pack_qdate);
|
|
||||||
MsgPackPrivate::register_unpacker(msgpack_type, unpack_qdate);
|
|
||||||
} else if (q_type == QMetaType::QDateTime) {
|
|
||||||
MsgPackPrivate::register_packer(q_type, msgpack_type, pack_qdatetime);
|
|
||||||
MsgPackPrivate::register_unpacker(msgpack_type, unpack_qdatetime);
|
|
||||||
} else if (q_type == QMetaType::QPoint) {
|
|
||||||
MsgPackPrivate::register_packer(q_type, msgpack_type, pack_qpoint);
|
|
||||||
MsgPackPrivate::register_unpacker(msgpack_type, unpack_qpoint);
|
|
||||||
} else if (q_type == QMetaType::QSize) {
|
|
||||||
MsgPackPrivate::register_packer(q_type, msgpack_type, pack_qsize);
|
|
||||||
MsgPackPrivate::register_unpacker(msgpack_type, unpack_qsize);
|
|
||||||
} else if (q_type == QMetaType::QRect) {
|
|
||||||
MsgPackPrivate::register_packer(q_type, msgpack_type, pack_qrect);
|
|
||||||
MsgPackPrivate::register_unpacker(msgpack_type, unpack_qrect);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef QT_GUI_LIB
|
#ifdef QT_GUI_LIB
|
||||||
QByteArray MsgPackPrivate::pack_qcolor(const QVariant &variant)
|
QByteArray MsgPackPrivate::pack_qcolor(const QVariant &variant)
|
||||||
{
|
{
|
||||||
|
@ -6,8 +6,6 @@
|
|||||||
|
|
||||||
namespace MsgPackPrivate
|
namespace MsgPackPrivate
|
||||||
{
|
{
|
||||||
bool register_qtype(QMetaType::Type q_type, quint8 msgpack_type);
|
|
||||||
|
|
||||||
#ifdef QT_GUI_LIB
|
#ifdef QT_GUI_LIB
|
||||||
QByteArray pack_qcolor(const QVariant &variant);
|
QByteArray pack_qcolor(const QVariant &variant);
|
||||||
QVariant unpack_qcolor(const QByteArray &data);
|
QVariant unpack_qcolor(const QByteArray &data);
|
||||||
|
Reference in New Issue
Block a user