diff --git a/entities/IEntity.h b/entities/IEntity.h new file mode 100644 index 0000000..faeddfb --- /dev/null +++ b/entities/IEntity.h @@ -0,0 +1,12 @@ +#ifndef IENTITY_H +#define IENTITY_H + +#include "ISerializable.h" + + +class IEntity : public ISerializable { +public: + virtual unsigned long long id() = 0; +}; + +#endif // IENTITY_H diff --git a/entities/ISerializable.h b/entities/ISerializable.h new file mode 100644 index 0000000..b1d5022 --- /dev/null +++ b/entities/ISerializable.h @@ -0,0 +1,23 @@ +#ifndef ISERIALIZABLE_H +#define ISERIALIZABLE_H + +#include + + +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 diff --git a/entities/cargo_entity.cpp b/entities/cargo_entity.cpp index e5a6f02..b24a177 100644 --- a/entities/cargo_entity.cpp +++ b/entities/cargo_entity.cpp @@ -19,3 +19,11 @@ QString cargo_entity::title() { unsigned int cargo_entity::volume() { return this->_volume; } + +void cargo_entity::serialize(QDataStream &output) { + output << this->_id << this->_title << this->_volume; +} + +void cargo_entity::deserialize(QDataStream &input) { + input >> this->_id >> this->_title >> this->_volume; +} diff --git a/entities/cargo_entity.h b/entities/cargo_entity.h index 1aed358..cbf00c8 100644 --- a/entities/cargo_entity.h +++ b/entities/cargo_entity.h @@ -1,12 +1,13 @@ #ifndef CARGO_ENTITY_H #define CARGO_ENTITY_H +#include "IEntity.h" + #include #include -class cargo_entity -{ +class cargo_entity : public IEntity { private: unsigned long long _id; QString _title; @@ -19,6 +20,9 @@ public: unsigned long long id(); QString title(); unsigned int volume(); + + void serialize(QDataStream &output); + void deserialize(QDataStream &input); }; #endif // CARGO_ENTITY_H diff --git a/entities/dpoint_entity.cpp b/entities/dpoint_entity.cpp index 60e17e8..fd43073 100644 --- a/entities/dpoint_entity.cpp +++ b/entities/dpoint_entity.cpp @@ -19,3 +19,11 @@ QString dpoint_entity::title() { const QVector dpoint_entity::storages_ids() { return this->_storages_ids; } + +void dpoint_entity::serialize(QDataStream &output) { + output << this->_id << this->_title << this->_storages_ids; +} + +void dpoint_entity::deserialize(QDataStream &input) { + input >> this->_id >> this->_title >> this->_storages_ids; +} diff --git a/entities/dpoint_entity.h b/entities/dpoint_entity.h index c36154f..e431dca 100644 --- a/entities/dpoint_entity.h +++ b/entities/dpoint_entity.h @@ -1,13 +1,14 @@ #ifndef DPOINT_ENTITY_H #define DPOINT_ENTITY_H +#include "IEntity.h" + #include #include #include -class dpoint_entity -{ +class dpoint_entity : public IEntity { private: unsigned long long _id; QString _title; @@ -20,6 +21,9 @@ public: unsigned long long id(); QString title(); const QVector storages_ids(); + + void serialize(QDataStream &output); + void deserialize(QDataStream &input); }; #endif // DPOINT_ENTITY_H diff --git a/entities/storage_entity.cpp b/entities/storage_entity.cpp index b198c38..83f65a1 100644 --- a/entities/storage_entity.cpp +++ b/entities/storage_entity.cpp @@ -19,3 +19,11 @@ unsigned int storage_entity::capacity() { const QVector storage_entity::cargo() { return this->_cargo; } + +void storage_entity::serialize(QDataStream &output) { + output << this->_id << this->_capacity << this->_cargo; +} + +void storage_entity::deserialize(QDataStream &input) { + input >> this->_id >> this->_capacity >> this->_cargo; +} diff --git a/entities/storage_entity.h b/entities/storage_entity.h index a8442be..40210fe 100644 --- a/entities/storage_entity.h +++ b/entities/storage_entity.h @@ -1,14 +1,14 @@ #ifndef STORAGE_ENTITY_H #define STORAGE_ENTITY_H +#include "IEntity.h" #include "cargo_entity.h" #include #include -class storage_entity -{ +class storage_entity : public IEntity { private: static unsigned long long __global_id; @@ -23,6 +23,9 @@ public: unsigned long long id(); unsigned int capacity(); const QVector cargo(); + + void serialize(QDataStream &output); + void deserialize(QDataStream &input); }; #endif // STORAGE_ENTITY_H diff --git a/entities/user_entity.cpp b/entities/user_entity.cpp index ac98ab6..ef8508c 100644 --- a/entities/user_entity.cpp +++ b/entities/user_entity.cpp @@ -1,6 +1,6 @@ #include "user_entity.h" -user_entity::user_entity(const QString &login, const QString &password, UserRole role) { +user_entity::user_entity(const QString &login, const QString &password, UserRole role) : _login(login), _role(role) { this->_pwd_hash = QCryptographicHash::hash(password.toLocal8Bit(), QCryptographicHash::Sha3_256); } @@ -19,3 +19,11 @@ UserRole user_entity::role() { bool user_entity::verify_password(const QString &password) { return (this->_pwd_hash == QCryptographicHash::hash(password.toLocal8Bit(), QCryptographicHash::Sha3_256)); } + +void user_entity::serialize(QDataStream &output) { + output << this->_id << this->_login << this->_role << this->_pwd_hash; +} + +void user_entity::deserialize(QDataStream &input) { + input >> this->_id >> this->_login >> this->_role >> this->_pwd_hash; +} diff --git a/entities/user_entity.h b/entities/user_entity.h index a085969..cf87e8e 100644 --- a/entities/user_entity.h +++ b/entities/user_entity.h @@ -1,6 +1,8 @@ #ifndef USER_ENTITY_H #define USER_ENTITY_H +#include "IEntity.h" + #include #include @@ -10,8 +12,7 @@ enum class UserRole { }; -class user_entity -{ +class user_entity : public IEntity { private: unsigned long long _id; QString _login; @@ -26,6 +27,9 @@ public: const QString login(); UserRole role(); bool verify_password(const QString &password); + + void serialize(QDataStream &output); + void deserialize(QDataStream &input); }; #endif // USER_ENTITY_H diff --git a/entities/vessel_entity.cpp b/entities/vessel_entity.cpp index 7b0dada..a5b99a0 100644 --- a/entities/vessel_entity.cpp +++ b/entities/vessel_entity.cpp @@ -22,3 +22,11 @@ unsigned int vessel_entity::capacity() { const QVector vessel_entity::cargo() { return this->_cargo; } + +void vessel_entity::serialize(QDataStream &output) { + output << this->_id << this->_harbor << this->_capacity << this->_cargo; +} + +void vessel_entity::deserialize(QDataStream &input) { + input >> this->_id >> this->_harbor >> this->_capacity >> this->_cargo; +} diff --git a/entities/vessel_entity.h b/entities/vessel_entity.h index 2d02c54..a0c6004 100644 --- a/entities/vessel_entity.h +++ b/entities/vessel_entity.h @@ -1,13 +1,12 @@ #ifndef VESSEL_ENTITY_H #define VESSEL_ENTITY_H +#include "IEntity.h" #include "cargo_entity.h" #include "dpoint_entity.h" - -class vessel_entity -{ +class vessel_entity : public IEntity { private: static unsigned long long __global_id; @@ -24,6 +23,9 @@ public: const dpoint_entity harbor(); unsigned int capacity(); const QVector cargo(); + + void serialize(QDataStream &output); + void deserialize(QDataStream &input); }; #endif // VESSEL_ENTITY_H diff --git a/sea_transport.pro b/sea_transport.pro index a1be585..447f7ce 100644 --- a/sea_transport.pro +++ b/sea_transport.pro @@ -26,6 +26,8 @@ HEADERS += \ authwindow.h \ cargoeditdialog.h \ deliverypointeditdialog.h \ + entities/IEntity.h \ + entities/ISerializable.h \ entities/cargo_entity.h \ entities/dpoint_entity.h \ entities/storage_entity.h \