mirror of
https://github.com/romixlab/qmsgpack.git
synced 2025-07-30 18:37:14 +02:00
Started working on Qt types packers/unpackers
QColor implemented cmake will fail with Qt4 for now
This commit is contained in:
@ -39,6 +39,23 @@ else ()
|
||||
set(PC_Requires "QtCore")
|
||||
endif ()
|
||||
|
||||
option (QTGUI_TYPES "Build with support for QtGui types")
|
||||
if (QTGUI_TYPES)
|
||||
if (QT4_BUILD)
|
||||
find_package(Qt4 QTGUI)
|
||||
else ()
|
||||
find_package(Qt5Gui QUIET)
|
||||
endif ()
|
||||
endif ()
|
||||
|
||||
if (Qt5Gui_FOUND)
|
||||
message("Qt5Gui found")
|
||||
include_directories(${Qt5Gui_INCLUDE_DIRS})
|
||||
add_definitions(${Qt5Gui_DEFINITIONS})
|
||||
else ()
|
||||
message("Qt5Gui not found")
|
||||
endif ()
|
||||
|
||||
if (NOT WIN32)
|
||||
set(QT_DONT_USE_QTGUI TRUE)
|
||||
endif ()
|
||||
|
@ -1,4 +1,4 @@
|
||||
set(qmsgpack_srcs msgpack.cpp msgpack_common.cpp private/pack_p.cpp private/unpack_p.cpp)
|
||||
set(qmsgpack_srcs msgpack.cpp msgpack_common.cpp private/pack_p.cpp private/unpack_p.cpp private/qt_types_p.cpp)
|
||||
set(qmsgpack_headers msgpack.h msgpack_common.h msgpack_export.h)
|
||||
|
||||
add_library(qmsgpack SHARED ${qmsgpack_srcs} ${qmsgpack_headers})
|
||||
@ -9,6 +9,10 @@ else ()
|
||||
target_link_libraries(qmsgpack ${QT_LIBRARIES})
|
||||
endif ()
|
||||
|
||||
if (Qt5Gui_FOUND)
|
||||
target_link_libraries(qmsgpack Qt5::Gui)
|
||||
endif ()
|
||||
|
||||
configure_file(
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/msgpack_common.h.in"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/msgpack_common.h"
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
QT += core
|
||||
|
||||
QT -= gui
|
||||
QT += gui
|
||||
|
||||
TARGET = qmsgpack
|
||||
CONFIG -= app_bundle
|
||||
@ -26,7 +26,8 @@ CONFIG(debug, debug|release) {
|
||||
SOURCES += msgpack.cpp \
|
||||
msgpack_common.cpp \
|
||||
private/pack_p.cpp \
|
||||
private/unpack_p.cpp
|
||||
private/unpack_p.cpp \
|
||||
private/qt_types_p.cpp
|
||||
|
||||
HEADERS += \
|
||||
msgpack.h \
|
||||
@ -34,4 +35,5 @@ HEADERS += \
|
||||
private/unpack_p.h \
|
||||
private/sysdep.h \
|
||||
msgpack_common.h \
|
||||
msgpack_export.h
|
||||
msgpack_export.h \
|
||||
private/qt_types_p.h
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include "msgpack_common.h"
|
||||
#include "private/unpack_p.h"
|
||||
#include "private/pack_p.h"
|
||||
#include "private/qt_types_p.h"
|
||||
|
||||
QVariant MsgPack::unpack(const QByteArray &data)
|
||||
{
|
||||
@ -35,6 +36,11 @@ bool MsgPack::registerUnpacker(qint8 msgpackType, MsgPack::unpack_user_f unpacke
|
||||
return MsgPackPrivate::register_unpacker(msgpackType, unpacker);
|
||||
}
|
||||
|
||||
bool MsgPack::registerType(QMetaType::Type qType, quint8 msgpackType)
|
||||
{
|
||||
return MsgPackPrivate::register_qtype(qType, msgpackType);
|
||||
}
|
||||
|
||||
void MsgPack::setCompatibilityModeEnabled(bool enabled)
|
||||
{
|
||||
MsgPackPrivate::compatibilityMode = enabled;
|
||||
|
@ -1,12 +1,10 @@
|
||||
#ifndef MSGPACK_H
|
||||
#define MSGPACK_H
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QVariantList>
|
||||
#include "msgpack_common.h"
|
||||
#include "msgpack_export.h"
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QMetaType>
|
||||
|
||||
namespace MsgPack
|
||||
{
|
||||
MSGPACK_EXPORT QVariant unpack(const QByteArray &data);
|
||||
@ -15,6 +13,8 @@ namespace MsgPack
|
||||
MSGPACK_EXPORT QByteArray pack(const QVariant &variant);
|
||||
MSGPACK_EXPORT bool registerPacker(QMetaType::Type qType, qint8 msgpackType, pack_user_f packer);
|
||||
|
||||
MSGPACK_EXPORT bool registerType(QMetaType::Type qType, quint8 msgpackType);
|
||||
|
||||
MSGPACK_EXPORT void setCompatibilityModeEnabled(bool enabled);
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
#define COMMON_H
|
||||
|
||||
#include <QVariant>
|
||||
#include <QtGlobal>
|
||||
|
||||
#define MSGPACK_MAJOR 0
|
||||
#define MSGPACK_MINOR 1
|
||||
@ -24,6 +25,48 @@ typedef QVariant (*unpack_user_f)(const QByteArray &data);
|
||||
* @return current version
|
||||
*/
|
||||
QString version();
|
||||
/**
|
||||
* @brief The FirstByte enum
|
||||
* From Message Pack spec
|
||||
*/
|
||||
namespace FirstByte {
|
||||
const quint8 POSITIVE_FIXINT = 0x00;
|
||||
const quint8 FIXMAP = 0x80;
|
||||
const quint8 FIXARRAY = 0x90;
|
||||
const quint8 FIXSTR = 0xa0;
|
||||
const quint8 NIL = 0xc0;
|
||||
const quint8 NEVER_USED = 0xc1;
|
||||
const quint8 FALSE = 0xc2;
|
||||
const quint8 TRUE = 0xc3;
|
||||
const quint8 BIN8 = 0xc4;
|
||||
const quint8 BIN16 = 0xc5;
|
||||
const quint8 BIN32 = 0xc6;
|
||||
const quint8 EXT8 = 0xc7;
|
||||
const quint8 EXT16 = 0xc8;
|
||||
const quint8 EXT32 = 0xc9;
|
||||
const quint8 FLOAT32 = 0xca;
|
||||
const quint8 FLOAT64 = 0xcb;
|
||||
const quint8 UINT8 = 0xcc;
|
||||
const quint8 UINT16 = 0xcd;
|
||||
const quint8 UINT32 = 0xce;
|
||||
const quint8 UINT64 = 0xcf;
|
||||
const quint8 INT8 = 0xd0;
|
||||
const quint8 INT16 = 0xd1;
|
||||
const quint8 INT32 = 0xd2;
|
||||
const quint8 INT64 = 0xd3;
|
||||
const quint8 FIXEXT1 = 0xd4;
|
||||
const quint8 FIXEXT2 = 0xd5;
|
||||
const quint8 FIXEXT4 = 0xd6;
|
||||
const quint8 FIXEXT8 = 0xd7;
|
||||
const quint8 FIXEX16 = 0xd8;
|
||||
const quint8 STR8 = 0xd9;
|
||||
const quint8 STR16 = 0xda;
|
||||
const quint8 STR32 = 0xdb;
|
||||
const quint8 ARRAY8 = 0xdc;
|
||||
const quint8 ARRAY16 = 0xdd;
|
||||
const quint8 MAP16 = 0xde;
|
||||
const quint8 MAP32 = 0xdf;
|
||||
const quint8 NEGATIVE_FIXINT = 0xe0;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // COMMON_H
|
||||
|
@ -2,6 +2,7 @@
|
||||
#define COMMON_H
|
||||
|
||||
#include <QVariant>
|
||||
#include <QtGlobal>
|
||||
|
||||
#define MSGPACK_MAJOR @QMSGPACK_MAJOR@
|
||||
#define MSGPACK_MINOR @QMSGPACK_MINOR@
|
||||
@ -24,6 +25,48 @@ typedef QVariant (*unpack_user_f)(const QByteArray &data);
|
||||
* @return current version
|
||||
*/
|
||||
QString version();
|
||||
/**
|
||||
* @brief The FirstByte enum
|
||||
* From Message Pack spec
|
||||
*/
|
||||
namespace FirstByte {
|
||||
const quint8 POSITIVE_FIXINT = 0x00;
|
||||
const quint8 FIXMAP = 0x80;
|
||||
const quint8 FIXARRAY = 0x90;
|
||||
const quint8 FIXSTR = 0xa0;
|
||||
const quint8 NIL = 0xc0;
|
||||
const quint8 NEVER_USED = 0xc1;
|
||||
const quint8 FALSE = 0xc2;
|
||||
const quint8 TRUE = 0xc3;
|
||||
const quint8 BIN8 = 0xc4;
|
||||
const quint8 BIN16 = 0xc5;
|
||||
const quint8 BIN32 = 0xc6;
|
||||
const quint8 EXT8 = 0xc7;
|
||||
const quint8 EXT16 = 0xc8;
|
||||
const quint8 EXT32 = 0xc9;
|
||||
const quint8 FLOAT32 = 0xca;
|
||||
const quint8 FLOAT64 = 0xcb;
|
||||
const quint8 UINT8 = 0xcc;
|
||||
const quint8 UINT16 = 0xcd;
|
||||
const quint8 UINT32 = 0xce;
|
||||
const quint8 UINT64 = 0xcf;
|
||||
const quint8 INT8 = 0xd0;
|
||||
const quint8 INT16 = 0xd1;
|
||||
const quint8 INT32 = 0xd2;
|
||||
const quint8 INT64 = 0xd3;
|
||||
const quint8 FIXEXT1 = 0xd4;
|
||||
const quint8 FIXEXT2 = 0xd5;
|
||||
const quint8 FIXEXT4 = 0xd6;
|
||||
const quint8 FIXEXT8 = 0xd7;
|
||||
const quint8 FIXEX16 = 0xd8;
|
||||
const quint8 STR8 = 0xd9;
|
||||
const quint8 STR16 = 0xda;
|
||||
const quint8 STR32 = 0xdb;
|
||||
const quint8 ARRAY8 = 0xdc;
|
||||
const quint8 ARRAY16 = 0xdd;
|
||||
const quint8 MAP16 = 0xde;
|
||||
const quint8 MAP32 = 0xdf;
|
||||
const quint8 NEGATIVE_FIXINT = 0xe0;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // COMMON_H
|
||||
|
46
src/private/qt_types_p.cpp
Normal file
46
src/private/qt_types_p.cpp
Normal file
@ -0,0 +1,46 @@
|
||||
#include "qt_types_p.h"
|
||||
#include "pack_p.h"
|
||||
#include "unpack_p.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
#ifdef QT_GUI_LIB
|
||||
#include <QColor>
|
||||
#endif
|
||||
#define NO_QTGUI_WARNING "Library built without QtGui, hence some types are not available"
|
||||
|
||||
bool MsgPackPrivate::register_qtype(QMetaType::Type q_type, quint8 msgpack_type)
|
||||
{
|
||||
if (q_type == QMetaType::QColor) {
|
||||
#ifdef QT_GUI_LIB
|
||||
qRegisterMetaType<QColor>("QColor");
|
||||
MsgPackPrivate::register_packer((QMetaType::Type)qMetaTypeId<QColor>(), msgpack_type, pack_qcolor);
|
||||
MsgPackPrivate::register_unpacker(msgpack_type, unpack_qcolor);
|
||||
return true;
|
||||
#else
|
||||
qWarning() << NO_QTGUI_WARNING;
|
||||
return false;
|
||||
#endif //QT_GUI_LIB
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef QT_GUI_LIB
|
||||
quint32 MsgPackPrivate::pack_qcolor(const QVariant &variant, QByteArray &data, bool write)
|
||||
{
|
||||
if (write) {
|
||||
QColor color = variant.value<QColor>();
|
||||
data.resize(4);
|
||||
data[0] = color.red();
|
||||
data[1] = color.green();
|
||||
data[2] = color.blue();
|
||||
data[3] = color.alpha();
|
||||
}
|
||||
return 4; // 4 bytes: r,g,b,a
|
||||
}
|
||||
|
||||
QVariant MsgPackPrivate::unpack_qcolor(const QByteArray &data)
|
||||
{
|
||||
return QColor((quint8)data[0], (quint8)data[1],
|
||||
(quint8)data[2], (quint8)data[3]);
|
||||
}
|
||||
#endif //MsgPackPrivate
|
18
src/private/qt_types_p.h
Normal file
18
src/private/qt_types_p.h
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef QT_TYPES_P_H
|
||||
#define QT_TYPES_P_H
|
||||
|
||||
#include <QVariant>
|
||||
#include <QByteArray>
|
||||
|
||||
namespace MsgPackPrivate
|
||||
{
|
||||
bool register_qtype(QMetaType::Type q_type, quint8 msgpack_type);
|
||||
|
||||
#ifdef QT_GUI_LIB
|
||||
quint32 pack_qcolor(const QVariant &variant, QByteArray &data, bool write);
|
||||
QVariant unpack_qcolor(const QByteArray &data);
|
||||
#endif //QT_GUI_LIB
|
||||
|
||||
} // MsgPackPrivate
|
||||
|
||||
#endif // QT_TYPES_P_H
|
Reference in New Issue
Block a user