diff --git a/.qmake.conf b/.qmake.conf new file mode 100644 index 0000000..e2ab5a6 --- /dev/null +++ b/.qmake.conf @@ -0,0 +1,3 @@ +TOP_SRCDIR=$$PWD +TOP_BUILDDIR=$$shadowed($$PWD) + diff --git a/CMakeLists.txt b/CMakeLists.txt index 5791159..6f09424 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -80,14 +80,14 @@ set(MSGPACK_QT_LIB_VERSION_STRING "${QMSGPACK_MAJOR}.${QMSGPACK_MINOR}.${QMSGPAC set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib") add_subdirectory(src) -if (BUILD_TESTS) +#if (MSGPACK_BUILD_TESTS) enable_testing() add_subdirectory(tests) -endif () +#endif () install(EXPORT qmsgpack-export DESTINATION ${CMAKECONFIG_INSTALL_DIR} FILE MsgPackQtTargets.cmake) file(RELATIVE_PATH relInstallDir ${CMAKE_INSTALL_PREFIX}/$CMAKECONFIG_INSTALL_DIR} ${CMAKE_INSTALL_PREFIX}) add_custom_target(uninstall - "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake") + "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake") \ No newline at end of file diff --git a/qmsgpack.pro b/qmsgpack.pro index bb80731..9593051 100644 --- a/qmsgpack.pro +++ b/qmsgpack.pro @@ -1,4 +1,4 @@ TEMPLATE = subdirs SUBDIRS += \ - src \ No newline at end of file + src diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6c76121..cc4c4ba 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,5 @@ -set(qmsgpack_srcs msgpack.cpp msgpack_common.cpp msgpackstream.cpp private/pack_p.cpp private/unpack_p.cpp private/qt_types_p.cpp) -set(qmsgpack_headers msgpack.h msgpackstream.h msgpack_common.h msgpack_export.h endianhelper.h) +set(qmsgpack_srcs msgpack.cpp msgpackcommon.cpp msgpackstream.cpp private/pack_p.cpp private/unpack_p.cpp private/qt_types_p.cpp) +set(qmsgpack_headers msgpack.h msgpackstream.h msgpackcommon.h msgpack_export.h endianhelper.h) add_library(qmsgpack SHARED ${qmsgpack_srcs} ${qmsgpack_headers}) @@ -14,8 +14,8 @@ if (Qt5Gui_FOUND) endif () configure_file( - "${CMAKE_CURRENT_SOURCE_DIR}/msgpack_common.h.in" - "${CMAKE_CURRENT_SOURCE_DIR}/msgpack_common.h" + "${CMAKE_CURRENT_SOURCE_DIR}/msgpackcommon.h.in" + "${CMAKE_CURRENT_SOURCE_DIR}/msgpackcommon.h" IMMEDIATE @ONLY) if (NOT android) @@ -31,4 +31,4 @@ 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) \ No newline at end of file diff --git a/src/msgpack-qt.pro b/src/msgpack-qt.pro deleted file mode 100644 index d69ffaf..0000000 --- a/src/msgpack-qt.pro +++ /dev/null @@ -1,41 +0,0 @@ -#------------------------------------------------- -# -# Project created by QtCreator 2014-09-07T17:43:05 -# -#------------------------------------------------- - -QT += core - -QT += gui - -TARGET = qmsgpack -CONFIG -= app_bundle - -TEMPLATE = lib -DEFINES += MSGPACK_MAKE_LIB -DESTDIR = $$_PRO_FILE_PWD_/../bin -INSTALLS += target -QMAKE_CXXFLAGS += -fPIC - -CONFIG += debug_and_release -CONFIG(debug, debug|release) { - TARGET = $$join(TARGET,,,d) -} - - -SOURCES += msgpack.cpp \ - msgpack_common.cpp \ - private/pack_p.cpp \ - private/unpack_p.cpp \ - private/qt_types_p.cpp \ - stream.cpp - -HEADERS += \ - msgpack.h \ - private/pack_p.h \ - private/unpack_p.h \ - private/sysdep.h \ - msgpack_common.h \ - msgpack_export.h \ - private/qt_types_p.h \ - stream.h diff --git a/src/msgpack.cpp b/src/msgpack.cpp index 57aefd1..fd04437 100644 --- a/src/msgpack.cpp +++ b/src/msgpack.cpp @@ -1,8 +1,8 @@ #include "msgpack.h" -#include "msgpack_common.h" #include "private/unpack_p.h" #include "private/pack_p.h" #include "private/qt_types_p.h" + #include QVariant MsgPack::unpack(const QByteArray &data) diff --git a/src/msgpack.h b/src/msgpack.h index b52ebb5..5052ab0 100644 --- a/src/msgpack.h +++ b/src/msgpack.h @@ -1,8 +1,8 @@ #ifndef MSGPACK_H #define MSGPACK_H -#include "msgpack_common.h" #include "msgpack_export.h" +#include "msgpackcommon.h" #include #include diff --git a/src/msgpack_common.cpp b/src/msgpackcommon.cpp similarity index 85% rename from src/msgpack_common.cpp rename to src/msgpackcommon.cpp index 8e42e73..c87d99b 100644 --- a/src/msgpack_common.cpp +++ b/src/msgpackcommon.cpp @@ -1,4 +1,4 @@ -#include "msgpack_common.h" +#include "msgpackcommon.h" QString MsgPack::version() { diff --git a/src/msgpack_common.h b/src/msgpackcommon.h similarity index 92% rename from src/msgpack_common.h rename to src/msgpackcommon.h index 7903dff..9f399b2 100644 --- a/src/msgpack_common.h +++ b/src/msgpackcommon.h @@ -1,5 +1,5 @@ -#ifndef MSGPACK_COMMON_H -#define MSGPACK_COMMON_H +#ifndef COMMON_H +#define COMMON_H #include #include @@ -36,8 +36,8 @@ const quint8 FIXARRAY = 0x90; const quint8 FIXSTR = 0xa0; const quint8 NIL = 0xc0; const quint8 NEVER_USED = 0xc1; -const quint8 MFALSE = 0xc2; -const quint8 MTRUE = 0xc3; +const quint8 FALSE = 0xc2; +const quint8 TRUE = 0xc3; const quint8 BIN8 = 0xc4; const quint8 BIN16 = 0xc5; const quint8 BIN32 = 0xc6; @@ -69,4 +69,4 @@ const quint8 MAP32 = 0xdf; const quint8 NEGATIVE_FIXINT = 0xe0; } } -#endif // MSGPACK_COMMON_H +#endif // COMMON_H diff --git a/src/msgpack_common.h.in b/src/msgpackcommon.h.in similarity index 92% rename from src/msgpack_common.h.in rename to src/msgpackcommon.h.in index 1f2d3d6..d39965a 100644 --- a/src/msgpack_common.h.in +++ b/src/msgpackcommon.h.in @@ -1,5 +1,5 @@ -#ifndef MSGPACK_COMMON_H -#define MSGPACK_COMMON_H +#ifndef COMMON_H +#define COMMON_H #include #include @@ -36,8 +36,8 @@ const quint8 FIXARRAY = 0x90; const quint8 FIXSTR = 0xa0; const quint8 NIL = 0xc0; const quint8 NEVER_USED = 0xc1; -const quint8 MFALSE = 0xc2; -const quint8 MTRUE = 0xc3; +const quint8 FALSE = 0xc2; +const quint8 TRUE = 0xc3; const quint8 BIN8 = 0xc4; const quint8 BIN16 = 0xc5; const quint8 BIN32 = 0xc6; @@ -69,4 +69,4 @@ const quint8 MAP32 = 0xdf; const quint8 NEGATIVE_FIXINT = 0xe0; } } -#endif // MSGPACK_COMMON_H +#endif // COMMON_H diff --git a/src/msgpackstream.cpp b/src/msgpackstream.cpp index cd71cff..3ad134e 100644 --- a/src/msgpackstream.cpp +++ b/src/msgpackstream.cpp @@ -1,5 +1,6 @@ #include "msgpackstream.h" #include "private/pack_p.h" + #include #include @@ -27,7 +28,7 @@ MsgPackStream::MsgPackStream() : { } MsgPackStream::MsgPackStream(QIODevice *d) : - dev(d), owndev(false), q_status(Ok) + dev(d), owndev(false) { } MsgPackStream::MsgPackStream(QByteArray *a, QIODevice::OpenMode mode) : @@ -61,6 +62,11 @@ void MsgPackStream::setDevice(QIODevice *d) owndev = false; } +QIODevice *MsgPackStream::device() const +{ + return dev; +} + bool MsgPackStream::atEnd() const { return dev ? dev->atEnd() : true; @@ -89,10 +95,10 @@ MsgPackStream &MsgPackStream::operator>>(bool &b) b = false; setStatus(ReadPastEnd); } else { - if (p[0] != MsgPack::FirstByte::MTRUE || - p[0] != MsgPack::FirstByte::MFALSE) + if (p[0] != MsgPack::FirstByte::TRUE || + p[0] != MsgPack::FirstByte::FALSE) setStatus(ReadCorruptData); - b = (p[0] == MsgPack::FirstByte::MTRUE); + b = (p[0] == MsgPack::FirstByte::TRUE); } return *this; } @@ -319,23 +325,11 @@ bool MsgPackStream::readBytes(char *data, uint len) return dev->read(data, len) == len; } -bool MsgPackStream::readNil() -{ - CHECK_STREAM_PRECOND(false); - quint8 b; - if (dev->read((char *)&b, 1) != 1 || - b != MsgPack::FirstByte::NIL) { - setStatus(ReadCorruptData); - return false; - } - return true; -} - MsgPackStream &MsgPackStream::operator<<(bool b) { CHECK_STREAM_WRITE_PRECOND(*this); quint8 m = b == true ? - MsgPack::FirstByte::MTRUE : MsgPack::FirstByte::MFALSE; + MsgPack::FirstByte::TRUE : MsgPack::FirstByte::FALSE; if (dev->write((char *)&m, 1) != 1) setStatus(WriteFailed); return *this; @@ -446,21 +440,8 @@ 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::writeNil() -{ - CHECK_STREAM_WRITE_PRECOND(false); - quint8 b = MsgPack::FirstByte::NIL; - if (dev->write((char *)&b, 1) != 1) { - setStatus(WriteFailed); - return false; - } return true; } @@ -501,7 +482,7 @@ bool MsgPackStream::unpack_longlong(qint64 &i64) } else if (p[0] == MsgPack::FirstByte::UINT64) { quint64 u64; u64 = _msgpack_load64(quint64, p + 1); - if (u64 > std::numeric_limits::max()) { + if (u64 > (quint64)std::numeric_limits::max()) { setStatus(ReadCorruptData); return false; } diff --git a/src/msgpackstream.h b/src/msgpackstream.h index a90381e..e16e9a6 100644 --- a/src/msgpackstream.h +++ b/src/msgpackstream.h @@ -1,15 +1,14 @@ #ifndef STREAM_H #define STREAM_H -#include "msgpack_export.h" -#include "msgpack_common.h" +#include "msgpackcommon.h" #include "endianhelper.h" -#include - #include -class MSGPACK_EXPORT MsgPackStream +#include + +class MsgPackStream { public: MsgPackStream(); @@ -41,7 +40,6 @@ public: MsgPackStream &operator>>(QString &str); MsgPackStream &operator>>(QByteArray &array); bool readBytes(char *data, uint len); - bool readNil(); MsgPackStream &operator<<(bool b); MsgPackStream &operator<<(quint32 u32); @@ -54,7 +52,6 @@ public: MsgPackStream &operator<<(const char *str); MsgPackStream &operator<<(QByteArray array); bool writeBytes(const char *data, uint len); - bool writeNil(); private: QIODevice *dev; @@ -82,12 +79,10 @@ MsgPackStream& operator<<(MsgPackStream& s, const QList &list) _msgpack_store32(p + 1, len); s.writeBytes((const char *)p, 5); } - if (s.status() != MsgPackStream::Ok) return s; for (int i = 0; i < list.size(); ++i) s << list[i]; - return s; } @@ -95,7 +90,6 @@ template MsgPackStream& operator>>(MsgPackStream& s, QList &list) { list.clear(); - quint8 p[5]; quint32 len; s.readBytes((char *)p, 1); @@ -108,7 +102,6 @@ MsgPackStream& operator>>(MsgPackStream& s, QList &list) s.readBytes((char *)p + 1, 4); len = _msgpack_load32(quint32, p + 1); } - for (quint32 i = 0; i < len; ++i) { T t; s >> t; @@ -116,133 +109,6 @@ MsgPackStream& operator>>(MsgPackStream& s, QList &list) if (s.atEnd()) break; } - - return s; -} - -template -MsgPackStream& operator<<(MsgPackStream &s, const QHash &hash) -{ - quint32 len = 0; - QHashIterator it(hash); - while (it.hasNext()) { - it.next(); - len++; - } - - quint8 p[5]; - if (len <= 15) { - p[0] = MsgPack::FirstByte::FIXMAP | len; - s.writeBytes((const char *)p, 1); - } else if (len <= std::numeric_limits::max()) { - p[0] = MsgPack::FirstByte::MAP16; - _msgpack_store16(p + 1, len); - s.writeBytes((const char *)p, 3); - } else { - p[0] = MsgPack::FirstByte::MAP32; - _msgpack_store32(p + 1, len); - s.writeBytes((const char *)p, 5); - } - - it.toFront(); - while (it.hasNext()) { - it.next(); - s << it.key() << it.value(); - } - - return s; -} - -template -MsgPackStream& operator>>(MsgPackStream& s, QHash &hash) -{ - hash.clear(); - - quint8 p[5]; - quint32 len; - s.readBytes((char *)p, 1); - if (p[0] >= 0x80 && p[0] <= 0x8f) { - len = p[0] & 0xf; - } else if (p[0] == MsgPack::FirstByte::MAP16) { - s.readBytes((char *)p + 1, 2); - len = _msgpack_load16(quint16, p + 1); - } else if (p[0] == MsgPack::FirstByte::MAP32) { - s.readBytes((char *)p + 1, 4); - len = _msgpack_load32(quint32, p + 1); - } - - for (quint32 i = 0; i < len; ++i) { - Key key; - T t; - s >> key >> t; - hash.insert(key, t); - if (s.atEnd()) - break; - } - - return s; -} - -template -MsgPackStream& operator<<(MsgPackStream &s, const QMap &map) -{ - quint32 len = 0; - QMapIterator it(map); - while (it.hasNext()) { - it.next(); - len++; - } - - quint8 p[5]; - if (len <= 15) { - p[0] = MsgPack::FirstByte::FIXMAP | len; - s.writeBytes((const char *)p, 1); - } else if (len <= std::numeric_limits::max()) { - p[0] = MsgPack::FirstByte::MAP16; - _msgpack_store16(p + 1, len); - s.writeBytes((const char *)p, 3); - } else { - p[0] = MsgPack::FirstByte::MAP32; - _msgpack_store32(p + 1, len); - s.writeBytes((const char *)p, 5); - } - - it.toFront(); - while (it.hasNext()) { - it.next(); - s << it.key() << it.value(); - } - - return s; -} - -template -MsgPackStream& operator>>(MsgPackStream& s, QMap &map) -{ - map.clear(); - - quint8 p[5]; - quint32 len; - s.readBytes((char *)p, 1); - if (p[0] >= 0x80 && p[0] <= 0x8f) { - len = p[0] & 0xf; - } else if (p[0] == MsgPack::FirstByte::MAP16) { - s.readBytes((char *)p + 1, 2); - len = _msgpack_load16(quint16, p + 1); - } else if (p[0] == MsgPack::FirstByte::MAP32) { - s.readBytes((char *)p + 1, 4); - len = _msgpack_load32(quint32, p + 1); - } - - for (quint32 i = 0; i < len; ++i) { - Key key; - T t; - s >> key >> t; - map.insert(key, t); - if (s.atEnd()) - break; - } - return s; } diff --git a/src/private/pack_p.cpp b/src/private/pack_p.cpp index ca0065d..22a095a 100644 --- a/src/private/pack_p.cpp +++ b/src/private/pack_p.cpp @@ -1,14 +1,13 @@ #include "pack_p.h" #include "../endianhelper.h" +#include + #include #include #include #include #include - -#include - #include #include @@ -19,10 +18,7 @@ QReadWriteLock MsgPackPrivate::packers_lock; quint8 *MsgPackPrivate::pack(const QVariant &v, quint8 *p, bool wr, QVector &user_data) { QMetaType::Type t = (QMetaType::Type)v.type(); - - if (v.isNull()) - p = pack_nil(p, wr); - else if (t == QMetaType::Int) + if (t == QMetaType::Int) p = pack_int(v.toInt(), p, wr); else if (t == QMetaType::UInt) p = pack_uint(v.toUInt(), p, wr); @@ -46,19 +42,21 @@ quint8 *MsgPackPrivate::pack(const QVariant &v, quint8 *p, bool wr, QVector= -32 && i <= 127) { @@ -329,18 +327,9 @@ bool MsgPackPrivate::register_packer(QMetaType::Type q_type, qint8 msgpack_type, quint8 *MsgPackPrivate::pack_user(const QVariant &v, quint8 *p, bool wr, QVector &user_data) { - QMetaType::Type t; - if (v.type() == QVariant::UserType) - t = (QMetaType::Type)v.userType(); - else - t = (QMetaType::Type)v.type(); - + QMetaType::Type t = (QMetaType::Type)v.type() == QMetaType::User ? + (QMetaType::Type)v.userType() : (QMetaType::Type)v.type(); QReadLocker locker(&packers_lock); - bool has_packer = user_packers.contains(t); - if (!has_packer) { - qWarning() << "MsgPack::pack can't pack type:" << t; - return p; - } packer_t pt = user_packers[t]; locker.unlock(); diff --git a/src/private/pack_p.h b/src/private/pack_p.h index 7550377..2b5daba 100644 --- a/src/private/pack_p.h +++ b/src/private/pack_p.h @@ -1,7 +1,7 @@ #ifndef PACK_P_H #define PACK_P_H -#include "../msgpack_common.h" +#include "../msgpackcommon.h" #include #include @@ -25,7 +25,6 @@ extern bool compatibilityMode; quint8 * pack(const QVariant &v, quint8 *p, bool wr, QVector &user_data); -quint8 * pack_nil(quint8 *p, bool wr); quint8 * pack_int(qint32 i, quint8 *p, bool wr); quint8 * pack_uint(quint32 i, quint8 *p, bool wr); quint8 * pack_longlong(qint64 i, quint8 *p, bool wr); diff --git a/src/private/qt_types_p.cpp b/src/private/qt_types_p.cpp index 83949e1..84735ce 100644 --- a/src/private/qt_types_p.cpp +++ b/src/private/qt_types_p.cpp @@ -1,8 +1,9 @@ #include "qt_types_p.h" #include "pack_p.h" #include "unpack_p.h" -#include "msgpackstream.h" -#include "endianhelper.h" +#include "../msgpackstream.h" +#include "../endianhelper.h" + #include #ifdef QT_GUI_LIB @@ -69,7 +70,7 @@ QVariant MsgPackPrivate::unpack_qcolor(const QByteArray &data) // Date and Time void MsgPackPrivate::pack_qtime_raw(const QTime &time, quint8 *p) { - quint8 hm = 0, ms = 0; + quint8 hm, ms; hm = (quint8)time.hour() << 4; hm |= (quint8)time.minute() >> 2; ms = ((quint8)time.minute() << 6) & 0xc0; // 11000000 @@ -169,10 +170,10 @@ QVariant MsgPackPrivate::unpack_qdatetime(const QByteArray &data) // Points and Vectors QByteArray MsgPackPrivate::pack_qpoint(const QVariant &variant) { - QPoint point = variant.toPoint(); QByteArray packed; MsgPackStream stream(&packed, QIODevice::WriteOnly); - stream << point.x() << point.y(); + QPoint pt = variant.toPoint(); + stream << pt.x() << pt.y(); return packed; } @@ -181,15 +182,16 @@ QVariant MsgPackPrivate::unpack_qpoint(const QByteArray &data) 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 size = variant.toSize(); QByteArray packed; MsgPackStream stream(&packed, QIODevice::WriteOnly); - stream << size.width() << size.height(); + QSize sz = variant.toSize(); + stream << sz.width() << sz.height(); return packed; } diff --git a/src/private/unpack_p.cpp b/src/private/unpack_p.cpp index 8d2cc22..092f9b3 100644 --- a/src/private/unpack_p.cpp +++ b/src/private/unpack_p.cpp @@ -4,7 +4,6 @@ #include #include #include - #include #include @@ -67,7 +66,7 @@ quint8 *MsgPackPrivate::unpack_type(QVariant &v, quint8 *p) quint8 * MsgPackPrivate::unpack_nil(QVariant &v, quint8 *p) { Q_UNUSED(p) - v = QVariant(); + Q_UNUSED(v) return p + 1; } diff --git a/src/private/unpack_p.h b/src/private/unpack_p.h index ae85648..cce8fe4 100644 --- a/src/private/unpack_p.h +++ b/src/private/unpack_p.h @@ -1,7 +1,7 @@ #ifndef MSGPACK_P_H #define MSGPACK_P_H -#include "../msgpack_common.h" +#include "../msgpackcommon.h" #include #include diff --git a/src/src.pro b/src/src.pro index 84b7a07..23358fb 100644 --- a/src/src.pro +++ b/src/src.pro @@ -1,44 +1,52 @@ -TEMPLATE = lib -CONFIG += shared -QMAKE_CXXFLAGS += -std=c++11 -TARGET = qmsgpack -DEFINES += MSGPACK_MAKE_LIB -VERSION = 0.1.0 +QT += core +QT -= gui + +TARGET = qmsgpack +CONFIG -= app_bundle + +TEMPLATE = lib +DEFINES += MSGPACK_MAKE_LIB +DESTDIR = $$TOP_SRCDIR/bin +QMAKE_CXXFLAGS += -fPIC + +CONFIG += debug_and_release +CONFIG(debug, debug|release) { + TARGET = $$join(TARGET,,,d) +} + + +SOURCES += msgpack.cpp \ + msgpackcommon.cpp \ + private/pack_p.cpp \ + private/unpack_p.cpp \ + private/qt_types_p.cpp \ + msgpackstream.cpp HEADERS += \ - msgpack_common.h \ - msgpackstream.h \ - msgpack_export.h \ msgpack.h \ - endianhelper.h \ + private/pack_p.h \ private/unpack_p.h \ + endianhelper.h \ + msgpackcommon.h \ + msgpack_export.h \ private/qt_types_p.h \ - private/pack_p.h - -SOURCES += \ - msgpackstream.cpp \ - msgpack_common.cpp \ - msgpack.cpp \ - private/qt_types_p.cpp \ - private/unpack_p.cpp \ - private/pack_p.cpp - -release: DESTDIR = ../build/release -debug: DESTDIR = ../build/debug -OBJECTS_DIR = $$DESTDIR/obj -MOC_DIR = $$DESTDIR/moc + msgpackstream.h HEADERS_INSTALL = \ - msgpack_common.h \ - msgpackstream.h \ - msgpack_export.h \ msgpack.h \ - endianhelper.h + endianhelper.h \ + msgpackcommon.h \ + msgpack_export.h \ + msgpackstream.h \ + +STREAM_HEADERS_INSTALL = \ + stream/location.h unix { header_files.files = $$HEADERS_INSTALL header_files.path = /usr/include/qmsgpack + stream_header_files.files = $$STREAM_HEADERS_INSTALL + stream_header_files.path = /usr/include/qmsgpack/stream target.path = /usr/lib - INSTALLS += header_files target + INSTALLS += header_files stream_header_files target } - diff --git a/src/stream/location.h b/src/stream/location.h new file mode 100644 index 0000000..df35df3 --- /dev/null +++ b/src/stream/location.h @@ -0,0 +1,24 @@ +#ifndef QMSGPACK_STREAM_LOCATION_H +#define QMSGPACK_STREAM_LOCATION_H + +#include "../msgpackstream.h" + +#include + +MsgPackStream& operator>>(MsgPackStream& s, QGeoCoordinate &coordinate) +{ + double x; + s >> x; + coordinate.setLatitude(x); + s >> x; + coordinate.setLongitude(x); + return s; +} + +MsgPackStream &operator<<(MsgPackStream& s, const QGeoCoordinate &coordinate) +{ + s << coordinate.latitude() << coordinate.longitude(); + return s; +} + +#endif // QMSGPACK_STREAM_LOCATION_H \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 9e74114..ae96079 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -8,7 +8,7 @@ if (Qt5Core_FOUND) set(TEST_LIBRARIES ${Qt5Test_LIBRARIES}) endif () -set(TEST_SUBDIRS pack unpack mixed qttypes stream) +set(TEST_SUBDIRS pack unpack mixed stream) foreach(subdir ${TEST_SUBDIRS}) add_subdirectory(${subdir}) diff --git a/tests/big/CMakeLists.txt b/tests/big/CMakeLists.txt new file mode 100644 index 0000000..a7a149f --- /dev/null +++ b/tests/big/CMakeLists.txt @@ -0,0 +1,24 @@ +set(QT_USE_QTTEST TRUE) + +if (NOT Qt5Core_FOUND) + include( ${QT_USE_FILE} ) +endif() + +include(AddFileDependencies) + +include_directories(../../src ${CMAKE_CURRENT_BINARY_DIR}) + +set(UNIT_TESTS big) + +foreach(test ${UNIT_TESTS}) + message(status "Building ${test}") + add_executable(${test} ${test}.cpp) + + target_link_libraries(${test} + ${QT_LIBRARIES} + ${TEST_LIBRARIES} + qmsgpack + ) + + add_test(${test} ${test}) +endforeach() \ No newline at end of file diff --git a/tests/big/big.cpp b/tests/big/big.cpp new file mode 100644 index 0000000..1f8ddac --- /dev/null +++ b/tests/big/big.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include + +class Big : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void test_big_list(); +}; + +void Big::test_big_list() +{ + QVariantList list; + for (int i = 0; i < 10000; ++i) + list << i; + QByteArray packed; + QVariantList unpacked; + QBENCHMARK { + packed = MsgPack::pack(list); + unpacked = MsgPack::unpack(packed).toList(); + } + bool listOk = true; + for (int i = 0; i < unpacked.size(); ++i) { + if (unpacked[i].toInt() != i) { + listOk = false; + } + } + QVERIFY(listOk); +} + + +QTEST_APPLESS_MAIN(Big) + +#include "big.moc" diff --git a/tests/pack/pack_test.cpp b/tests/pack/pack_test.cpp index 2769ffb..2bd6aea 100644 --- a/tests/pack/pack_test.cpp +++ b/tests/pack/pack_test.cpp @@ -9,7 +9,6 @@ class PackTest : public QObject Q_OBJECT private Q_SLOTS: - void test_nil(); void test_bool(); void test_fixint(); void test_integer8(); @@ -22,14 +21,6 @@ private Q_SLOTS: void test_array(); }; -void PackTest::test_nil() -{ - QByteArray arr = MsgPack::pack(QVariant()); - quint8 *p = (quint8 *)arr.data(); - QVERIFY(arr.size() == 1); - QVERIFY(p[0] == 0xc0); -} - void PackTest::test_bool() { QByteArray arr = MsgPack::pack(false); @@ -225,7 +216,7 @@ void PackTest::test_float() void PackTest::test_str() { - QString str = QString("msgpack rocks"); + QString str = QStringLiteral("msgpack rocks"); QByteArray arr = MsgPack::pack(str); QVERIFY(arr.size() == 14); quint8 *p = (quint8 *)arr.data(); diff --git a/tests/qttypes/qttypes_test.cpp b/tests/qttypes/qttypes_test.cpp index f7ca125..287a233 100644 --- a/tests/qttypes/qttypes_test.cpp +++ b/tests/qttypes/qttypes_test.cpp @@ -74,11 +74,11 @@ void QtTypesTest::test_qpoint() QPoint pt2 = MsgPack::unpack(packed).toPoint(); QVERIFY(pt == pt2); - pt = QPoint(1, 2); - packed = MsgPack::pack(pt); - QVERIFY(packed.size() == 4); - pt2 = MsgPack::unpack(packed).toPoint(); - QVERIFY(pt == pt2); + pt = QPoint(1, 2); + packed = MsgPack::pack(pt); + QVERIFY(packed.size() == 4); + pt2 = MsgPack::unpack(packed).toPoint(); + QVERIFY(pt == pt2); pt = QPoint(1234, 5678); packed = MsgPack::pack(pt); @@ -141,4 +141,4 @@ void QtTypesTest::test_qrect() QTEST_APPLESS_MAIN(QtTypesTest) -#include "qttypes_test.moc" +#include "qttypes_test.moc" \ No newline at end of file diff --git a/tests/stream/stream_test.cpp b/tests/stream/stream_test.cpp index 61eac91..ed0041b 100644 --- a/tests/stream/stream_test.cpp +++ b/tests/stream/stream_test.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -17,7 +18,6 @@ private Q_SLOTS: void test_double(); void test_bin(); void test_array(); - void test_map(); }; void StreamTest::test_unpack_integers() @@ -142,7 +142,7 @@ void StreamTest::test_pack_integers() void StreamTest::test_unpack_string() { - QString str = QString("msgpack rocks"); + QString str = QStringLiteral("msgpack rocks"); QByteArray packed = MsgPack::pack(str); QString str2; @@ -367,47 +367,5 @@ void StreamTest::test_array() } } -void StreamTest::test_map() -{ - QMap map, map2; - QByteArray ba; - - map.insert("m0", 0); - { - MsgPackStream stream(&ba, QIODevice::WriteOnly); - stream << map; - MsgPackStream stream2(ba); - stream2 >> map2; - } - QVERIFY(ba.length() == 5); - quint8 *p = (quint8 *)ba.data(); - QVERIFY(p[0] == 0x80 | 1); - QVERIFY(map == map2); - - for (int i = 1; i < 16; ++i) - map.insert(QString("m%1").QString::arg(i), i); - { - MsgPackStream stream(&ba, QIODevice::WriteOnly); - stream << map; - MsgPackStream stream2(ba); - stream2 >> map2; - } - p = (quint8 *)ba.data(); - QVERIFY(p[0] == 0xde); - QVERIFY(map == map2); - - for (int i = 16; i < 65536; ++i) - map.insert(QString("m%1").QString::arg(i), i); - { - MsgPackStream stream(&ba, QIODevice::WriteOnly); - stream << map; - MsgPackStream stream2(ba); - stream2 >> map2; - } - p = (quint8 *)ba.data(); - QVERIFY(p[0] == 0xdf); - QVERIFY(map == map2); -} - QTEST_APPLESS_MAIN(StreamTest) #include "stream_test.moc" diff --git a/tests/unpack/unpack_test.cpp b/tests/unpack/unpack_test.cpp index 02c91ab..10e8c48 100644 --- a/tests/unpack/unpack_test.cpp +++ b/tests/unpack/unpack_test.cpp @@ -9,7 +9,6 @@ class UnpackTest : public QObject Q_OBJECT private Q_SLOTS: - void test_nil(); void test_booleans(); void test_integers(); void test_floats(); @@ -18,13 +17,6 @@ private Q_SLOTS: void test_array(); }; -void UnpackTest::test_nil() -{ - QByteArray pack = MsgPack::pack(QVariantList() << true << QVariant()); - QVariantList u = MsgPack::unpack(pack).toList(); - QVERIFY(u[0] == true); - QVERIFY(u[1].isNull()); -} void UnpackTest::test_booleans() {