From 34afb5aba32764bab5bd23b303a108bb28dcea9a Mon Sep 17 00:00:00 2001 From: Konstantin Ritt Date: Tue, 20 Nov 2018 16:59:57 +0300 Subject: [PATCH 1/2] 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 --- src/msgpack.cpp | 50 +++++++++++++++++++++++++++++++++++++- src/private/qt_types_p.cpp | 41 ------------------------------- src/private/qt_types_p.h | 2 -- 3 files changed, 49 insertions(+), 44 deletions(-) diff --git a/src/msgpack.cpp b/src/msgpack.cpp index 3bc8e8e..f98a931 100644 --- a/src/msgpack.cpp +++ b/src/msgpack.cpp @@ -7,6 +7,10 @@ #include +#ifdef QT_LOCATION_LIB +#include +#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()) { + 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) diff --git a/src/private/qt_types_p.cpp b/src/private/qt_types_p.cpp index b5e5299..0982c2d 100644 --- a/src/private/qt_types_p.cpp +++ b/src/private/qt_types_p.cpp @@ -11,8 +11,6 @@ #ifdef QT_GUI_LIB #include -#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 #include -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()) { - MsgPackPrivate::register_packer((QMetaType::Type)qMetaTypeId(), - 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) { diff --git a/src/private/qt_types_p.h b/src/private/qt_types_p.h index f09a794..ad842bc 100644 --- a/src/private/qt_types_p.h +++ b/src/private/qt_types_p.h @@ -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); From 0b8282d592be05e82b3b9e5f9ff30d105364eee0 Mon Sep 17 00:00:00 2001 From: Konstantin Ritt Date: Tue, 20 Nov 2018 20:57:25 +0300 Subject: [PATCH 2/2] Make it possible to disable Qt type pack handlers at build time usable with either qmake DEFINES+=MSGPACK_NO_PACKTYPES or DEFINES += MSGPACK_STATIC MSGPACK_NO_PACKTYPES include(path/to/qmsgpack/qmsgpack.pri) --- qmsgpack.pri | 40 +++++++++++++++++++++++----------------- src/msgpack.cpp | 9 ++++++++- src/src.pro | 12 +++++++----- tests/tests.pro | 10 +++++++--- 4 files changed, 45 insertions(+), 26 deletions(-) diff --git a/qmsgpack.pri b/qmsgpack.pri index 026f721..f50e83c 100644 --- a/qmsgpack.pri +++ b/qmsgpack.pri @@ -7,10 +7,7 @@ SOURCES += \ $$PWD/src/msgpackcommon.cpp \ $$PWD/src/private/pack_p.cpp \ $$PWD/src/private/unpack_p.cpp \ - $$PWD/src/private/qt_types_p.cpp \ - $$PWD/src/msgpackstream.cpp \ - $$PWD/src/stream/time.cpp \ - $$PWD/src/stream/geometry.cpp + $$PWD/src/msgpackstream.cpp HEADERS += \ $$PWD/src/msgpack.h \ @@ -19,18 +16,27 @@ HEADERS += \ $$PWD/src/endianhelper.h \ $$PWD/src/msgpackcommon.h \ $$PWD/src/msgpack_export.h \ - $$PWD/src/private/qt_types_p.h \ - $$PWD/src/msgpackstream.h \ - $$PWD/src/stream/time.h \ - $$PWD/src/stream/geometry.h + $$PWD/src/msgpackstream.h -qtHaveModule(gui) { - QT += gui -} - -qtHaveModule(location) { - QT += location - - SOURCES += $$PWD/src/stream/location.cpp - HEADERS += $$PWD/src/stream/location.h +!contains(DEFINES, MSGPACK_NO_PACKTYPES) { + SOURCES += \ + $$PWD/src/private/qt_types_p.cpp \ + $$PWD/src/stream/time.cpp \ + $$PWD/src/stream/geometry.cpp + + HEADERS += \ + $$PWD/src/private/qt_types_p.h \ + $$PWD/src/stream/time.h \ + $$PWD/src/stream/geometry.h + + qtHaveModule(gui) { + QT += gui + } + + qtHaveModule(location) { + QT += location + + SOURCES += $$PWD/src/stream/location.cpp + HEADERS += $$PWD/src/stream/location.h + } } diff --git a/src/msgpack.cpp b/src/msgpack.cpp index f98a931..781d4a3 100644 --- a/src/msgpack.cpp +++ b/src/msgpack.cpp @@ -3,13 +3,16 @@ #include "msgpack.h" #include "private/unpack_p.h" #include "private/pack_p.h" -#include "private/qt_types_p.h" #include +#ifndef MSGPACK_NO_PACKTYPES +#include "private/qt_types_p.h" + #ifdef QT_LOCATION_LIB #include #endif +#endif // MSGPACK_NO_PACKTYPES QVariant MsgPack::unpack(const QByteArray &data) { @@ -51,6 +54,7 @@ qint8 MsgPack::msgpackType(int qType) bool MsgPack::registerType(QMetaType::Type qType, quint8 msgpackType) { +#ifndef MSGPACK_NO_PACKTYPES switch (qType) { #ifdef QT_GUI_LIB case QMetaType::QColor: @@ -92,6 +96,9 @@ bool MsgPack::registerType(QMetaType::Type qType, quint8 msgpackType) #endif // QT_LOCATION_LIB break; } +#else + Q_UNUSED(msgpackType) +#endif // MSGPACK_NO_PACKTYPES qWarning("qmsgpack was built without metatype %d support.", int(qType)); qWarning("Use MsgPack::registerPacker() and MsgPack::registerUnpacker() to register metatype %d manually.", int(qType)); diff --git a/src/src.pro b/src/src.pro index e81a138..8ae368d 100644 --- a/src/src.pro +++ b/src/src.pro @@ -18,12 +18,14 @@ HEADERS_INSTALL = \ msgpack_export.h \ msgpackstream.h \ -STREAM_HEADERS_INSTALL = \ - stream/geometry.h \ - stream/time.h +!contains(DEFINES, MSGPACK_NO_PACKTYPES) { + STREAM_HEADERS_INSTALL = \ + stream/geometry.h \ + stream/time.h -qtHaveModule(location) { - STREAM_HEADERS_INSTALL += stream/location.h + qtHaveModule(location) { + STREAM_HEADERS_INSTALL += stream/location.h + } } unix { diff --git a/tests/tests.pro b/tests/tests.pro index 397bda5..ca76dd3 100644 --- a/tests/tests.pro +++ b/tests/tests.pro @@ -3,6 +3,10 @@ TEMPLATE = subdirs SUBDIRS += \ pack \ unpack \ - mixed \ - stream \ - qttypes + mixed + +!contains(DEFINES, MSGPACK_NO_PACKTYPES) { + SUBDIRS += \ + stream \ + qttypes +}