Fixed serialization
This commit is contained in:
parent
48ad2f1817
commit
7d60d579b6
5 changed files with 26 additions and 16 deletions
|
|
@ -8,16 +8,6 @@ class ISerializable {
|
|||
public:
|
||||
virtual void serialize(QDataStream &output) = 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
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#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;
|
||||
auto hash = QCryptographicHash::hash(title.toLocal8Bit(), QCryptographicHash::Md5);
|
||||
for (auto bit : hash) {
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ private:
|
|||
|
||||
public:
|
||||
cargo_entity() = default;
|
||||
cargo_entity(const QString &title, int volume);
|
||||
cargo_entity(const QString &title, unsigned int volume);
|
||||
|
||||
unsigned long long id();
|
||||
QString title();
|
||||
|
|
|
|||
|
|
@ -21,9 +21,17 @@ const QVector<cargo_entity> storage_entity::cargo() {
|
|||
}
|
||||
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,9 +24,21 @@ const QVector<cargo_entity> vessel_entity::cargo() {
|
|||
}
|
||||
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue