mirror of
https://github.com/overte-org/overte.git
synced 2025-07-26 07:50:04 +02:00
Generics up and running.
This commit is contained in:
parent
9e151001e0
commit
9fc84d6358
4 changed files with 60 additions and 10 deletions
|
@ -760,8 +760,7 @@ Bitstream& Bitstream::operator<<(const QVariant& value) {
|
||||||
}
|
}
|
||||||
const TypeStreamer* streamer = getTypeStreamers().value(value.userType());
|
const TypeStreamer* streamer = getTypeStreamers().value(value.userType());
|
||||||
if (streamer) {
|
if (streamer) {
|
||||||
_typeStreamerStreamer << streamer->getStreamerToWrite(value);
|
streamer->writeVariant(*this, value);
|
||||||
streamer->write(*this, value);
|
|
||||||
} else {
|
} else {
|
||||||
qWarning() << "Non-streamable type: " << value.typeName() << "\n";
|
qWarning() << "Non-streamable type: " << value.typeName() << "\n";
|
||||||
}
|
}
|
||||||
|
@ -774,7 +773,7 @@ Bitstream& Bitstream::operator>>(QVariant& value) {
|
||||||
if (!streamer) {
|
if (!streamer) {
|
||||||
value = QVariant();
|
value = QVariant();
|
||||||
} else {
|
} else {
|
||||||
value = streamer->read(*this);
|
value = streamer->readVariant(*this);
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
@ -1889,6 +1888,15 @@ QVariant TypeStreamer::read(Bitstream& in) const {
|
||||||
return QVariant();
|
return QVariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TypeStreamer::writeVariant(Bitstream& out, const QVariant& value) const {
|
||||||
|
out << this;
|
||||||
|
write(out, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariant TypeStreamer::readVariant(Bitstream& in) const {
|
||||||
|
return read(in);
|
||||||
|
}
|
||||||
|
|
||||||
void TypeStreamer::writeDelta(Bitstream& out, const QVariant& value, const QVariant& reference) const {
|
void TypeStreamer::writeDelta(Bitstream& out, const QVariant& value, const QVariant& reference) const {
|
||||||
if (value == reference) {
|
if (value == reference) {
|
||||||
out << false;
|
out << false;
|
||||||
|
@ -2151,6 +2159,10 @@ const char* GenericTypeStreamer::getName() const {
|
||||||
return _name.constData();
|
return _name.constData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QVariant GenericTypeStreamer::readVariant(Bitstream& in) const {
|
||||||
|
return QVariant::fromValue(GenericValue(_weakSelf, read(in)));
|
||||||
|
}
|
||||||
|
|
||||||
GenericEnumTypeStreamer::GenericEnumTypeStreamer(const QByteArray& name, const QVector<NameIntPair>& values,
|
GenericEnumTypeStreamer::GenericEnumTypeStreamer(const QByteArray& name, const QVector<NameIntPair>& values,
|
||||||
int bits, const QByteArray& hash) :
|
int bits, const QByteArray& hash) :
|
||||||
GenericTypeStreamer(name),
|
GenericTypeStreamer(name),
|
||||||
|
@ -2190,7 +2202,7 @@ void GenericEnumTypeStreamer::write(Bitstream& out, const QVariant& value) const
|
||||||
QVariant GenericEnumTypeStreamer::read(Bitstream& in) const {
|
QVariant GenericEnumTypeStreamer::read(Bitstream& in) const {
|
||||||
int intValue = 0;
|
int intValue = 0;
|
||||||
in.read(&intValue, _bits);
|
in.read(&intValue, _bits);
|
||||||
return QVariant::fromValue(GenericValue(_weakSelf, intValue));
|
return intValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
TypeStreamer::Category GenericEnumTypeStreamer::getCategory() const {
|
TypeStreamer::Category GenericEnumTypeStreamer::getCategory() const {
|
||||||
|
@ -2270,7 +2282,7 @@ QVariant GenericStreamableTypeStreamer::read(Bitstream& in) const {
|
||||||
foreach (const StreamerNamePair& field, _fields) {
|
foreach (const StreamerNamePair& field, _fields) {
|
||||||
values.append(field.first->read(in));
|
values.append(field.first->read(in));
|
||||||
}
|
}
|
||||||
return QVariant::fromValue(GenericValue(_weakSelf, values));
|
return values;
|
||||||
}
|
}
|
||||||
|
|
||||||
TypeStreamer::Category GenericStreamableTypeStreamer::getCategory() const {
|
TypeStreamer::Category GenericStreamableTypeStreamer::getCategory() const {
|
||||||
|
@ -2337,7 +2349,7 @@ QVariant GenericListTypeStreamer::read(Bitstream& in) const {
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
values.append(_valueStreamer->read(in));
|
values.append(_valueStreamer->read(in));
|
||||||
}
|
}
|
||||||
return QVariant::fromValue(GenericValue(_weakSelf, values));
|
return values;
|
||||||
}
|
}
|
||||||
|
|
||||||
TypeStreamer::Category GenericListTypeStreamer::getCategory() const {
|
TypeStreamer::Category GenericListTypeStreamer::getCategory() const {
|
||||||
|
@ -2443,13 +2455,16 @@ QVariant GenericMapTypeStreamer::read(Bitstream& in) const {
|
||||||
QVariant value = _valueStreamer->read(in);
|
QVariant value = _valueStreamer->read(in);
|
||||||
values.append(QVariantPair(key, value));
|
values.append(QVariantPair(key, value));
|
||||||
}
|
}
|
||||||
return QVariant::fromValue(GenericValue(_weakSelf, QVariant::fromValue(values)));
|
return QVariant::fromValue(values);
|
||||||
}
|
}
|
||||||
|
|
||||||
TypeStreamer::Category GenericMapTypeStreamer::getCategory() const {
|
TypeStreamer::Category GenericMapTypeStreamer::getCategory() const {
|
||||||
return MAP_CATEGORY;
|
return MAP_CATEGORY;
|
||||||
}
|
}
|
||||||
|
|
||||||
const TypeStreamer* GenericValueStreamer::getStreamerToWrite(const QVariant& value) const {
|
void GenericValueStreamer::writeVariant(Bitstream& out, const QVariant& value) const {
|
||||||
return value.value<GenericValue>().getStreamer().data();
|
GenericValue genericValue = value.value<GenericValue>();
|
||||||
|
out << genericValue.getStreamer().data();
|
||||||
|
genericValue.getStreamer()->write(out, genericValue.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -918,6 +918,9 @@ public:
|
||||||
virtual void write(Bitstream& out, const QVariant& value) const;
|
virtual void write(Bitstream& out, const QVariant& value) const;
|
||||||
virtual QVariant read(Bitstream& in) const;
|
virtual QVariant read(Bitstream& in) const;
|
||||||
|
|
||||||
|
virtual void writeVariant(Bitstream& out, const QVariant& value) const;
|
||||||
|
virtual QVariant readVariant(Bitstream& in) const;
|
||||||
|
|
||||||
virtual void writeDelta(Bitstream& out, const QVariant& value, const QVariant& reference) const;
|
virtual void writeDelta(Bitstream& out, const QVariant& value, const QVariant& reference) const;
|
||||||
virtual void readDelta(Bitstream& in, QVariant& value, const QVariant& reference) const;
|
virtual void readDelta(Bitstream& in, QVariant& value, const QVariant& reference) const;
|
||||||
|
|
||||||
|
@ -1031,6 +1034,7 @@ public:
|
||||||
GenericTypeStreamer(const QByteArray& name);
|
GenericTypeStreamer(const QByteArray& name);
|
||||||
|
|
||||||
virtual const char* getName() const;
|
virtual const char* getName() const;
|
||||||
|
virtual QVariant readVariant(Bitstream& in) const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
|
@ -1260,7 +1264,7 @@ private:
|
||||||
class GenericValueStreamer : public SimpleTypeStreamer<GenericValue> {
|
class GenericValueStreamer : public SimpleTypeStreamer<GenericValue> {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
virtual const TypeStreamer* getStreamerToWrite(const QVariant& value) const;
|
virtual void writeVariant(Bitstream& out, const QVariant& value) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Macro for registering simple type streamers.
|
/// Macro for registering simple type streamers.
|
||||||
|
|
|
@ -41,6 +41,8 @@ public:
|
||||||
/// Returns the unique local ID for this object.
|
/// Returns the unique local ID for this object.
|
||||||
int getID() const { return _id; }
|
int getID() const { return _id; }
|
||||||
|
|
||||||
|
void setID(int id) { _weakHash.insert(_id = id, this); }
|
||||||
|
|
||||||
/// Returns the local origin ID for this object.
|
/// Returns the local origin ID for this object.
|
||||||
int getOriginID() const { return _originID; }
|
int getOriginID() const { return _originID; }
|
||||||
|
|
||||||
|
|
|
@ -214,6 +214,35 @@ static bool testSerialization(Bitstream::MetadataType metadataType) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// go back to the beginning and read everything as generics
|
||||||
|
inStream.device()->seek(0);
|
||||||
|
Bitstream genericIn(inStream, metadataType, Bitstream::ALL_GENERICS);
|
||||||
|
genericIn >> testObjectReadA;
|
||||||
|
genericIn >> testObjectReadB;
|
||||||
|
genericIn >> messageRead;
|
||||||
|
genericIn >> endRead;
|
||||||
|
|
||||||
|
// reassign the ids
|
||||||
|
testObjectReadA->setID(testObjectWrittenA->getID());
|
||||||
|
testObjectReadA->setOriginID(testObjectWrittenA->getOriginID());
|
||||||
|
testObjectReadB->setID(testObjectWrittenB->getID());
|
||||||
|
testObjectReadB->setOriginID(testObjectWrittenB->getOriginID());
|
||||||
|
|
||||||
|
// write it back out and compare
|
||||||
|
QByteArray compareArray;
|
||||||
|
QDataStream compareOutStream(&compareArray, QIODevice::WriteOnly);
|
||||||
|
Bitstream compareOut(compareOutStream, metadataType);
|
||||||
|
compareOut << testObjectReadA;
|
||||||
|
compareOut << testObjectReadB;
|
||||||
|
compareOut << messageRead;
|
||||||
|
compareOut << endRead;
|
||||||
|
compareOut.flush();
|
||||||
|
|
||||||
|
if (array != compareArray) {
|
||||||
|
qDebug() << "Mismatch between written/generic written streams.";
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue