mirror of
https://github.com/romixlab/qmsgpack.git
synced 2026-02-05 06:45:08 +01:00
99 lines
1.8 KiB
C++
99 lines
1.8 KiB
C++
|
|
#include "stream.h"
|
||
|
|
#include <QBuffer>
|
||
|
|
|
||
|
|
#undef CHECK_STREAM_PRECOND
|
||
|
|
#ifndef QT_NO_DEBUG
|
||
|
|
#define CHECK_STREAM_PRECOND(retVal) \
|
||
|
|
if (!dev) { \
|
||
|
|
qWarning("msgpack::Stream: No device"); \
|
||
|
|
return retVal; \
|
||
|
|
}
|
||
|
|
#else
|
||
|
|
#define CHECK_STREAM_PRECOND(retVal) \
|
||
|
|
if (!dev) { \
|
||
|
|
return retVal; \
|
||
|
|
}
|
||
|
|
#endif
|
||
|
|
|
||
|
|
#define CHECK_STREAM_WRITE_PRECOND(retVal) \
|
||
|
|
CHECK_STREAM_PRECOND(retVal) \
|
||
|
|
if (q_status != Ok) \
|
||
|
|
return retVal;
|
||
|
|
|
||
|
|
MsgPackStream::MsgPackStream() :
|
||
|
|
dev(0), compatibility(false), owndev(false), q_status(Ok)
|
||
|
|
{ }
|
||
|
|
|
||
|
|
MsgPackStream::MsgPackStream(QIODevice *d) :
|
||
|
|
dev(d), compatibility(false), owndev(false)
|
||
|
|
{ }
|
||
|
|
|
||
|
|
MsgPackStream::MsgPackStream(QByteArray *a, QIODevice::OpenMode mode) :
|
||
|
|
compatibility(false), owndev(true), q_status(Ok)
|
||
|
|
{
|
||
|
|
QBuffer *buf = new QBuffer(a);
|
||
|
|
buf->open(mode);
|
||
|
|
dev = buf;
|
||
|
|
}
|
||
|
|
|
||
|
|
MsgPackStream::MsgPackStream(const QByteArray &a) :
|
||
|
|
compatibility(false), owndev(true), q_status(Ok)
|
||
|
|
{
|
||
|
|
QBuffer *buf = new QBuffer();
|
||
|
|
buf->setData(a);
|
||
|
|
buf->open(QIODevice::ReadOnly);
|
||
|
|
dev = buf;
|
||
|
|
}
|
||
|
|
|
||
|
|
MsgPackStream::~MsgPackStream()
|
||
|
|
{
|
||
|
|
if (owndev)
|
||
|
|
delete dev;
|
||
|
|
}
|
||
|
|
|
||
|
|
void MsgPackStream::setDevice(QIODevice *d)
|
||
|
|
{
|
||
|
|
if (owndev)
|
||
|
|
delete dev;
|
||
|
|
dev = d;
|
||
|
|
owndev = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool MsgPackStream::atEnd() const
|
||
|
|
{
|
||
|
|
return dev ? dev->atEnd() : true;
|
||
|
|
}
|
||
|
|
|
||
|
|
void MsgPackStream::setCompatibility(bool isEnabled)
|
||
|
|
{
|
||
|
|
compatibility = isEnabled;
|
||
|
|
}
|
||
|
|
|
||
|
|
MsgPackStream::Status MsgPackStream::status() const
|
||
|
|
{
|
||
|
|
return q_status;
|
||
|
|
}
|
||
|
|
|
||
|
|
void MsgPackStream::resetStatus()
|
||
|
|
{
|
||
|
|
q_status = Ok;
|
||
|
|
}
|
||
|
|
|
||
|
|
void MsgPackStream::setStatus(Status status)
|
||
|
|
{
|
||
|
|
q_status = status;
|
||
|
|
}
|
||
|
|
|
||
|
|
MsgPackStream &MsgPackStream::operator >>(quint8 &u8)
|
||
|
|
{
|
||
|
|
u8 = 0;
|
||
|
|
CHECK_STREAM_PRECOND(*this)
|
||
|
|
char c;
|
||
|
|
if (!dev->getChar(&c))
|
||
|
|
setStatus(ReadPastEnd);
|
||
|
|
else
|
||
|
|
u8 = quint8(c);
|
||
|
|
return *this;
|
||
|
|
}
|
||
|
|
|