pimpl for PackStream
This commit is contained in:
parent
86e6539af9
commit
aecd3dcdf3
2 changed files with 71 additions and 67 deletions
|
@ -4,54 +4,62 @@
|
|||
#include <QFile>
|
||||
#include <QDataStream>
|
||||
#include <QString>
|
||||
#include <QByteArray>
|
||||
|
||||
PackStream::PackStream() {
|
||||
file = NULL;
|
||||
buffer = new QByteArray();
|
||||
stream = new QDataStream(buffer, QIODevice::WriteOnly);
|
||||
stream->setVersion(QDataStream::Qt_5_2);
|
||||
class PackStream::pimpl {
|
||||
public:
|
||||
QFile *file;
|
||||
QByteArray *buffer;
|
||||
QDataStream *stream;
|
||||
};
|
||||
|
||||
PackStream::PackStream() : pv(new pimpl) {
|
||||
pv->file = NULL;
|
||||
pv->buffer = new QByteArray();
|
||||
pv->stream = new QDataStream(pv->buffer, QIODevice::WriteOnly);
|
||||
pv->stream->setVersion(QDataStream::Qt_5_4);
|
||||
}
|
||||
|
||||
PackStream::PackStream(const PackStream *other) : pv(new pimpl) {
|
||||
pv->file = NULL;
|
||||
pv->buffer = new QByteArray();
|
||||
if (other->pv->file) {
|
||||
Logs::error("System") << "Try to read from a substream bound to a file: "
|
||||
<< other->pv->file->fileName().toStdString() << endl;
|
||||
pv->stream = new QDataStream(pv->buffer, QIODevice::ReadOnly);
|
||||
} else {
|
||||
pv->stream = new QDataStream(other->pv->buffer, QIODevice::ReadOnly);
|
||||
}
|
||||
pv->stream->setVersion(QDataStream::Qt_5_4);
|
||||
}
|
||||
|
||||
PackStream::PackStream(const string &buffer_content) : pv(new pimpl) {
|
||||
pv->file = NULL;
|
||||
pv->buffer = new QByteArray(buffer_content.c_str(), buffer_content.size());
|
||||
pv->stream = new QDataStream(pv->buffer, QIODevice::ReadOnly);
|
||||
pv->stream->setVersion(QDataStream::Qt_5_4);
|
||||
}
|
||||
|
||||
PackStream::~PackStream() {
|
||||
delete buffer;
|
||||
delete stream;
|
||||
if (file) {
|
||||
delete file;
|
||||
delete pv->buffer;
|
||||
delete pv->stream;
|
||||
if (pv->file) {
|
||||
delete pv->file;
|
||||
}
|
||||
}
|
||||
|
||||
PackStream::PackStream(const PackStream *other) {
|
||||
file = NULL;
|
||||
buffer = new QByteArray();
|
||||
if (other->file) {
|
||||
Logs::error("System") << "Try to read from a substream bound to a file: "
|
||||
<< other->file->fileName().toStdString() << endl;
|
||||
stream = new QDataStream(buffer, QIODevice::ReadOnly);
|
||||
} else {
|
||||
stream = new QDataStream(other->buffer, QIODevice::ReadOnly);
|
||||
}
|
||||
stream->setVersion(QDataStream::Qt_5_2);
|
||||
}
|
||||
|
||||
PackStream::PackStream(const string &buffer_content) {
|
||||
file = NULL;
|
||||
buffer = new QByteArray(buffer_content.c_str(), buffer_content.size());
|
||||
stream = new QDataStream(buffer, QIODevice::ReadOnly);
|
||||
stream->setVersion(QDataStream::Qt_5_2);
|
||||
}
|
||||
|
||||
bool PackStream::bindToFile(const string &filepath, bool write) {
|
||||
if (not file) {
|
||||
file = new QFile(QString::fromStdString(filepath));
|
||||
if (not file->open(write ? QIODevice::WriteOnly : QIODevice::ReadOnly)) {
|
||||
if (not pv->file) {
|
||||
pv->file = new QFile(QString::fromStdString(filepath));
|
||||
if (not pv->file->open(write ? QIODevice::WriteOnly : QIODevice::ReadOnly)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QDataStream *new_stream = new QDataStream(file);
|
||||
QDataStream *new_stream = new QDataStream(pv->file);
|
||||
if (new_stream) {
|
||||
delete stream;
|
||||
stream = new_stream;
|
||||
stream->setVersion(QDataStream::Qt_5_2);
|
||||
delete pv->stream;
|
||||
pv->stream = new_stream;
|
||||
pv->stream->setVersion(QDataStream::Qt_5_4);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
|
@ -63,79 +71,79 @@ bool PackStream::bindToFile(const string &filepath, bool write) {
|
|||
|
||||
void PackStream::write(const int *value) {
|
||||
if (value) {
|
||||
*stream << *value;
|
||||
*pv->stream << *value;
|
||||
}
|
||||
}
|
||||
|
||||
void PackStream::write(const double *value) {
|
||||
if (value) {
|
||||
*stream << *value;
|
||||
*pv->stream << *value;
|
||||
}
|
||||
}
|
||||
|
||||
void PackStream::write(const char *value, int max_length) {
|
||||
if (value) {
|
||||
int length = qstrlen(value);
|
||||
*stream << QString::fromUtf8(value, length > max_length ? max_length : length);
|
||||
*pv->stream << QString::fromUtf8(value, length > max_length ? max_length : length);
|
||||
}
|
||||
}
|
||||
|
||||
void PackStream::write(const string &value) {
|
||||
*stream << QString::fromStdString(value);
|
||||
*pv->stream << QString::fromStdString(value);
|
||||
}
|
||||
|
||||
void PackStream::writeFromBuffer(const PackStream &other, bool prepend_size) {
|
||||
if (other.file) {
|
||||
if (other.pv->file) {
|
||||
Logs::error("System") << "Try to write from a substream bound to a file: "
|
||||
<< other.file->fileName().toStdString() << endl;
|
||||
<< other.pv->file->fileName().toStdString() << endl;
|
||||
} else {
|
||||
if (prepend_size) {
|
||||
int buffer_size = (int)other.buffer->size();
|
||||
int buffer_size = (int)other.pv->buffer->size();
|
||||
write(&buffer_size);
|
||||
}
|
||||
stream->writeRawData(other.buffer->data(), other.buffer->size());
|
||||
pv->stream->writeRawData(other.pv->buffer->data(), other.pv->buffer->size());
|
||||
}
|
||||
}
|
||||
|
||||
string PackStream::getBuffer() {
|
||||
if (file) {
|
||||
Logs::error("System") << "Try to get buffer on a stream bound to a file: " << file->fileName().toStdString()
|
||||
if (pv->file) {
|
||||
Logs::error("System") << "Try to get buffer on a stream bound to a file: " << pv->file->fileName().toStdString()
|
||||
<< endl;
|
||||
return "";
|
||||
} else {
|
||||
return buffer->toStdString();
|
||||
return pv->buffer->toStdString();
|
||||
}
|
||||
}
|
||||
|
||||
void PackStream::read(int *value) {
|
||||
if (value and not stream->atEnd()) {
|
||||
if (value and not pv->stream->atEnd()) {
|
||||
int output;
|
||||
*stream >> output;
|
||||
*pv->stream >> output;
|
||||
*value = output;
|
||||
}
|
||||
}
|
||||
|
||||
void PackStream::read(double *value) {
|
||||
if (value and not stream->atEnd()) {
|
||||
if (value and not pv->stream->atEnd()) {
|
||||
double output;
|
||||
*stream >> output;
|
||||
*pv->stream >> output;
|
||||
*value = output;
|
||||
}
|
||||
}
|
||||
|
||||
void PackStream::read(char *value, int max_length) {
|
||||
if (value and not stream->atEnd()) {
|
||||
if (value and not pv->stream->atEnd()) {
|
||||
QString output;
|
||||
*stream >> output;
|
||||
*pv->stream >> output;
|
||||
QByteArray array = output.toUtf8();
|
||||
qstrncpy(value, array.constData(), max_length);
|
||||
}
|
||||
}
|
||||
|
||||
string PackStream::readString() {
|
||||
if (not stream->atEnd()) {
|
||||
if (not pv->stream->atEnd()) {
|
||||
QString output;
|
||||
*stream >> output;
|
||||
*pv->stream >> output;
|
||||
return output.toStdString();
|
||||
} else {
|
||||
return "";
|
||||
|
@ -143,13 +151,13 @@ string PackStream::readString() {
|
|||
}
|
||||
|
||||
void PackStream::skip(const int &value, int count) {
|
||||
stream->skipRawData(sizeof(value) * count);
|
||||
pv->stream->skipRawData(sizeof(value) * count);
|
||||
}
|
||||
|
||||
void PackStream::skip(const double &value, int count) {
|
||||
stream->skipRawData(sizeof(value) * count);
|
||||
pv->stream->skipRawData(sizeof(value) * count);
|
||||
}
|
||||
|
||||
void PackStream::skipBytes(int bytes) {
|
||||
stream->skipRawData(bytes);
|
||||
pv->stream->skipRawData(bytes);
|
||||
}
|
||||
|
|
|
@ -3,9 +3,7 @@
|
|||
|
||||
#include "system_global.h"
|
||||
|
||||
class QFile;
|
||||
class QDataStream;
|
||||
class QByteArray;
|
||||
#include <memory>
|
||||
|
||||
namespace paysages {
|
||||
namespace system {
|
||||
|
@ -16,20 +14,19 @@ namespace system {
|
|||
class SYSTEMSHARED_EXPORT PackStream {
|
||||
public:
|
||||
PackStream();
|
||||
~PackStream();
|
||||
|
||||
/**
|
||||
* Open a reading stream for another stream.
|
||||
*
|
||||
* The other stream must not have been bound to a file (still a memory buffer).
|
||||
*/
|
||||
PackStream(const PackStream *other);
|
||||
|
||||
/**
|
||||
* Open a reading stream on a buffer content.
|
||||
*/
|
||||
PackStream(const string &buffer_content);
|
||||
|
||||
~PackStream();
|
||||
|
||||
bool bindToFile(const string &filepath, bool write = false);
|
||||
|
||||
void write(const int *value);
|
||||
|
@ -61,9 +58,8 @@ class SYSTEMSHARED_EXPORT PackStream {
|
|||
void skipBytes(int bytes);
|
||||
|
||||
private:
|
||||
QFile *file;
|
||||
QByteArray *buffer;
|
||||
QDataStream *stream;
|
||||
class pimpl;
|
||||
unique_ptr<pimpl> pv;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue