Fixed serialization

This commit is contained in:
Andrew nuark G 2020-12-15 20:43:19 +07:00
parent 48ad2f1817
commit 7d60d579b6
5 changed files with 26 additions and 16 deletions

View file

@ -8,16 +8,6 @@ class ISerializable {
public: public:
virtual void serialize(QDataStream &output) = 0; virtual void serialize(QDataStream &output) = 0;
virtual void deserialize(QDataStream &input) = 0; virtual void deserialize(QDataStream &input) = 0;
friend QDataStream &operator<<(QDataStream &output, ISerializable &s) {
s.serialize(output);
return output;
}
friend QDataStream &operator>>(QDataStream &input, ISerializable &s) {
s.deserialize(input);
return input;
}
}; };
#endif // ISERIALIZABLE_H #endif // ISERIALIZABLE_H

View file

@ -1,6 +1,6 @@
#include "cargo_entity.h" #include "cargo_entity.h"
cargo_entity::cargo_entity(const QString &title, int volume) : _title(title), _volume(volume) { cargo_entity::cargo_entity(const QString &title, unsigned int volume) : _title(title), _volume(volume) {
this->_id = volume; this->_id = volume;
auto hash = QCryptographicHash::hash(title.toLocal8Bit(), QCryptographicHash::Md5); auto hash = QCryptographicHash::hash(title.toLocal8Bit(), QCryptographicHash::Md5);
for (auto bit : hash) { for (auto bit : hash) {

View file

@ -15,7 +15,7 @@ private:
public: public:
cargo_entity() = default; cargo_entity() = default;
cargo_entity(const QString &title, int volume); cargo_entity(const QString &title, unsigned int volume);
unsigned long long id(); unsigned long long id();
QString title(); QString title();

View file

@ -21,9 +21,17 @@ const QVector<cargo_entity> storage_entity::cargo() {
} }
void storage_entity::serialize(QDataStream &output) { void storage_entity::serialize(QDataStream &output) {
output << this->_id << this->_capacity << this->_cargo; output << this->_id << this->_capacity << this->_cargo.size();
for (auto item : this->_cargo) {
item.serialize(output);
}
} }
void storage_entity::deserialize(QDataStream &input) { void storage_entity::deserialize(QDataStream &input) {
input >> this->_id >> this->_capacity >> this->_cargo; int icnt;
input >> this->_id >> this->_capacity >> icnt;
this->_cargo.resize(icnt);
for (int i = 0; i < icnt; i++) {
this->_cargo[i].deserialize(input);
}
} }

View file

@ -24,9 +24,21 @@ const QVector<cargo_entity> vessel_entity::cargo() {
} }
void vessel_entity::serialize(QDataStream &output) { void vessel_entity::serialize(QDataStream &output) {
output << this->_id << this->_harbor << this->_capacity << this->_cargo; output << this->_id;
this->_harbor.serialize(output);
output << this->_capacity << this->_cargo.size();
for (auto item : this->_cargo) {
item.serialize(output);
}
} }
void vessel_entity::deserialize(QDataStream &input) { void vessel_entity::deserialize(QDataStream &input) {
input >> this->_id >> this->_harbor >> this->_capacity >> this->_cargo; input >> this->_id;
this->_harbor.deserialize(input);
int icnt;
input >> this->_capacity >> icnt;
this->_cargo.resize(icnt);
for (int i = 0; i < icnt; i++) {
this->_cargo[i].deserialize(input);
}
} }