MsgPackStream missing dev() added. Some little improvements.

This commit is contained in:
Isaikin Roman
2015-08-28 21:12:09 +03:00
parent 5be342038a
commit 4da435044d
26 changed files with 205 additions and 371 deletions
+5 -5
View File
@@ -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)
-41
View File
@@ -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
+1 -1
View File
@@ -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 <QVector>
QVariant MsgPack::unpack(const QByteArray &data)
+1 -1
View File
@@ -1,8 +1,8 @@
#ifndef MSGPACK_H
#define MSGPACK_H
#include "msgpack_common.h"
#include "msgpack_export.h"
#include "msgpackcommon.h"
#include <QByteArray>
#include <QVariantList>
@@ -1,4 +1,4 @@
#include "msgpack_common.h"
#include "msgpackcommon.h"
QString MsgPack::version()
{
+5 -5
View File
@@ -1,5 +1,5 @@
#ifndef MSGPACK_COMMON_H
#define MSGPACK_COMMON_H
#ifndef COMMON_H
#define COMMON_H
#include <QVariant>
#include <QtGlobal>
@@ -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
@@ -1,5 +1,5 @@
#ifndef MSGPACK_COMMON_H
#define MSGPACK_COMMON_H
#ifndef COMMON_H
#define COMMON_H
#include <QVariant>
#include <QtGlobal>
@@ -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
+13 -32
View File
@@ -1,5 +1,6 @@
#include "msgpackstream.h"
#include "private/pack_p.h"
#include <QBuffer>
#include <QDebug>
@@ -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<qint64>::max()) {
if (u64 > (quint64)std::numeric_limits<qint64>::max()) {
setStatus(ReadCorruptData);
return false;
}
+4 -138
View File
@@ -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 <QIODevice>
#include <limits>
class MSGPACK_EXPORT MsgPackStream
#include <QIODevice>
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<T> &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 <typename T>
MsgPackStream& operator>>(MsgPackStream& s, QList<T> &list)
{
list.clear();
quint8 p[5];
quint32 len;
s.readBytes((char *)p, 1);
@@ -108,7 +102,6 @@ MsgPackStream& operator>>(MsgPackStream& s, QList<T> &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<T> &list)
if (s.atEnd())
break;
}
return s;
}
template <class Key, class T>
MsgPackStream& operator<<(MsgPackStream &s, const QHash<Key, T> &hash)
{
quint32 len = 0;
QHashIterator<Key, T> 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<quint16>::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 <class Key, class T>
MsgPackStream& operator>>(MsgPackStream& s, QHash<Key, T> &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 <class Key, class T>
MsgPackStream& operator<<(MsgPackStream &s, const QMap<Key, T> &map)
{
quint32 len = 0;
QMapIterator<Key, T> 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<quint16>::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 <class Key, class T>
MsgPackStream& operator>>(MsgPackStream& s, QMap<Key, T> &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;
}
+16 -27
View File
@@ -1,14 +1,13 @@
#include "pack_p.h"
#include "../endianhelper.h"
#include <limits>
#include <QByteArray>
#include <QDebug>
#include <QMapIterator>
#include <QString>
#include <QStringList>
#include <limits>
#include <QReadLocker>
#include <QWriteLocker>
@@ -19,10 +18,7 @@ QReadWriteLock MsgPackPrivate::packers_lock;
quint8 *MsgPackPrivate::pack(const QVariant &v, quint8 *p, bool wr, QVector<QByteArray> &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<QByt
p = pack_bin(v.toByteArray(), p, wr);
else if (t == QMetaType::QVariantMap)
p = pack_map(v.toMap(), p, wr, user_data);
else
p = pack_user(v, p, wr, user_data);
else {
if (t == QMetaType::User)
t = (QMetaType::Type)v.userType();
QReadLocker locker(&packers_lock);
bool has_packer = user_packers.contains(t);
locker.unlock();
if (has_packer)
p = pack_user(v, p, wr, user_data);
else
qWarning() << "MsgPack::pack can't pack type:" << t;
}
return p;
}
quint8 *MsgPackPrivate::pack_nil(quint8 *p, bool wr)
{
if (wr)
*p = 0xc0;
return p + 1;
}
quint8 *MsgPackPrivate::pack_int(qint32 i, quint8 *p, bool wr)
{
if (i >= -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<QByteArray> &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();
+1 -2
View File
@@ -1,7 +1,7 @@
#ifndef PACK_P_H
#define PACK_P_H
#include "../msgpack_common.h"
#include "../msgpackcommon.h"
#include <QHash>
#include <QMetaType>
@@ -25,7 +25,6 @@ extern bool compatibilityMode;
quint8 * pack(const QVariant &v, quint8 *p, bool wr, QVector<QByteArray> &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);
+9 -7
View File
@@ -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 <QDebug>
#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;
}
+1 -2
View File
@@ -4,7 +4,6 @@
#include <QByteArray>
#include <QDebug>
#include <QMap>
#include <QReadLocker>
#include <QWriteLocker>
@@ -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;
}
+1 -1
View File
@@ -1,7 +1,7 @@
#ifndef MSGPACK_P_H
#define MSGPACK_P_H
#include "../msgpack_common.h"
#include "../msgpackcommon.h"
#include <QHash>
#include <QVariant>
+38 -30
View File
@@ -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
}
+24
View File
@@ -0,0 +1,24 @@
#ifndef QMSGPACK_STREAM_LOCATION_H
#define QMSGPACK_STREAM_LOCATION_H
#include "../msgpackstream.h"
#include <QGeoCoordinate>
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