mirror of
https://github.com/lubosz/overte.git
synced 2025-04-27 05:35:37 +02:00
Generics up and running.
This commit is contained in:
parent
9e151001e0
commit
9fc84d6358
4 changed files with 60 additions and 10 deletions
libraries/metavoxels/src
tests/metavoxels/src
|
@ -760,8 +760,7 @@ Bitstream& Bitstream::operator<<(const QVariant& value) {
|
|||
}
|
||||
const TypeStreamer* streamer = getTypeStreamers().value(value.userType());
|
||||
if (streamer) {
|
||||
_typeStreamerStreamer << streamer->getStreamerToWrite(value);
|
||||
streamer->write(*this, value);
|
||||
streamer->writeVariant(*this, value);
|
||||
} else {
|
||||
qWarning() << "Non-streamable type: " << value.typeName() << "\n";
|
||||
}
|
||||
|
@ -774,7 +773,7 @@ Bitstream& Bitstream::operator>>(QVariant& value) {
|
|||
if (!streamer) {
|
||||
value = QVariant();
|
||||
} else {
|
||||
value = streamer->read(*this);
|
||||
value = streamer->readVariant(*this);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
@ -1889,6 +1888,15 @@ QVariant TypeStreamer::read(Bitstream& in) const {
|
|||
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 {
|
||||
if (value == reference) {
|
||||
out << false;
|
||||
|
@ -2151,6 +2159,10 @@ const char* GenericTypeStreamer::getName() const {
|
|||
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,
|
||||
int bits, const QByteArray& hash) :
|
||||
GenericTypeStreamer(name),
|
||||
|
@ -2190,7 +2202,7 @@ void GenericEnumTypeStreamer::write(Bitstream& out, const QVariant& value) const
|
|||
QVariant GenericEnumTypeStreamer::read(Bitstream& in) const {
|
||||
int intValue = 0;
|
||||
in.read(&intValue, _bits);
|
||||
return QVariant::fromValue(GenericValue(_weakSelf, intValue));
|
||||
return intValue;
|
||||
}
|
||||
|
||||
TypeStreamer::Category GenericEnumTypeStreamer::getCategory() const {
|
||||
|
@ -2270,7 +2282,7 @@ QVariant GenericStreamableTypeStreamer::read(Bitstream& in) const {
|
|||
foreach (const StreamerNamePair& field, _fields) {
|
||||
values.append(field.first->read(in));
|
||||
}
|
||||
return QVariant::fromValue(GenericValue(_weakSelf, values));
|
||||
return values;
|
||||
}
|
||||
|
||||
TypeStreamer::Category GenericStreamableTypeStreamer::getCategory() const {
|
||||
|
@ -2337,7 +2349,7 @@ QVariant GenericListTypeStreamer::read(Bitstream& in) const {
|
|||
for (int i = 0; i < size; i++) {
|
||||
values.append(_valueStreamer->read(in));
|
||||
}
|
||||
return QVariant::fromValue(GenericValue(_weakSelf, values));
|
||||
return values;
|
||||
}
|
||||
|
||||
TypeStreamer::Category GenericListTypeStreamer::getCategory() const {
|
||||
|
@ -2443,13 +2455,16 @@ QVariant GenericMapTypeStreamer::read(Bitstream& in) const {
|
|||
QVariant value = _valueStreamer->read(in);
|
||||
values.append(QVariantPair(key, value));
|
||||
}
|
||||
return QVariant::fromValue(GenericValue(_weakSelf, QVariant::fromValue(values)));
|
||||
return QVariant::fromValue(values);
|
||||
}
|
||||
|
||||
TypeStreamer::Category GenericMapTypeStreamer::getCategory() const {
|
||||
return MAP_CATEGORY;
|
||||
}
|
||||
|
||||
const TypeStreamer* GenericValueStreamer::getStreamerToWrite(const QVariant& value) const {
|
||||
return value.value<GenericValue>().getStreamer().data();
|
||||
void GenericValueStreamer::writeVariant(Bitstream& out, const QVariant& value) const {
|
||||
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 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 readDelta(Bitstream& in, QVariant& value, const QVariant& reference) const;
|
||||
|
||||
|
@ -1031,6 +1034,7 @@ public:
|
|||
GenericTypeStreamer(const QByteArray& name);
|
||||
|
||||
virtual const char* getName() const;
|
||||
virtual QVariant readVariant(Bitstream& in) const;
|
||||
|
||||
protected:
|
||||
|
||||
|
@ -1260,7 +1264,7 @@ private:
|
|||
class GenericValueStreamer : public SimpleTypeStreamer<GenericValue> {
|
||||
public:
|
||||
|
||||
virtual const TypeStreamer* getStreamerToWrite(const QVariant& value) const;
|
||||
virtual void writeVariant(Bitstream& out, const QVariant& value) const;
|
||||
};
|
||||
|
||||
/// Macro for registering simple type streamers.
|
||||
|
|
|
@ -41,6 +41,8 @@ public:
|
|||
/// Returns the unique local ID for this object.
|
||||
int getID() const { return _id; }
|
||||
|
||||
void setID(int id) { _weakHash.insert(_id = id, this); }
|
||||
|
||||
/// Returns the local origin ID for this object.
|
||||
int getOriginID() const { return _originID; }
|
||||
|
||||
|
|
|
@ -214,6 +214,35 @@ static bool testSerialization(Bitstream::MetadataType metadataType) {
|
|||
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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue