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