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:
Konstantin Ritt
2018-11-20 16:59:57 +03:00
parent e3819fc5b3
commit 34afb5aba3
3 changed files with 49 additions and 44 deletions

View File

@ -7,6 +7,10 @@
#include <QVector>
#ifdef QT_LOCATION_LIB
#include <QGeoCoordinate>
#endif
QVariant MsgPack::unpack(const QByteArray &data)
{
quint8 *p = (quint8 *)data.data();
@ -47,7 +51,51 @@ qint8 MsgPack::msgpackType(int qType)
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)

View File

@ -11,8 +11,6 @@
#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
@ -22,45 +20,6 @@
#include <QTime>
#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
QByteArray MsgPackPrivate::pack_qcolor(const QVariant &variant)
{

View File

@ -6,8 +6,6 @@
namespace MsgPackPrivate
{
bool register_qtype(QMetaType::Type q_type, quint8 msgpack_type);
#ifdef QT_GUI_LIB
QByteArray pack_qcolor(const QVariant &variant);
QVariant unpack_qcolor(const QByteArray &data);