Abstraction and serialization for entities

This commit is contained in:
Andrew nuark G 2020-12-15 20:17:46 +07:00
parent 5cb96fb793
commit 48ad2f1817
13 changed files with 106 additions and 12 deletions

12
entities/IEntity.h Normal file
View file

@ -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

23
entities/ISerializable.h Normal file
View file

@ -0,0 +1,23 @@
#ifndef ISERIALIZABLE_H
#define ISERIALIZABLE_H
#include <QDataStream>
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

View file

@ -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;
}

View file

@ -1,12 +1,13 @@
#ifndef CARGO_ENTITY_H
#define CARGO_ENTITY_H
#include "IEntity.h"
#include <QString>
#include <QCryptographicHash>
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

View file

@ -19,3 +19,11 @@ QString dpoint_entity::title() {
const QVector<unsigned long long> 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;
}

View file

@ -1,13 +1,14 @@
#ifndef DPOINT_ENTITY_H
#define DPOINT_ENTITY_H
#include "IEntity.h"
#include <QString>
#include <QVector>
#include <QCryptographicHash>
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<unsigned long long> storages_ids();
void serialize(QDataStream &output);
void deserialize(QDataStream &input);
};
#endif // DPOINT_ENTITY_H

View file

@ -19,3 +19,11 @@ unsigned int storage_entity::capacity() {
const QVector<cargo_entity> 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;
}

View file

@ -1,14 +1,14 @@
#ifndef STORAGE_ENTITY_H
#define STORAGE_ENTITY_H
#include "IEntity.h"
#include "cargo_entity.h"
#include <QVector>
#include <QCryptographicHash>
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_entity> cargo();
void serialize(QDataStream &output);
void deserialize(QDataStream &input);
};
#endif // STORAGE_ENTITY_H

View file

@ -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;
}

View file

@ -1,6 +1,8 @@
#ifndef USER_ENTITY_H
#define USER_ENTITY_H
#include "IEntity.h"
#include <QString>
#include <QCryptographicHash>
@ -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

View file

@ -22,3 +22,11 @@ unsigned int vessel_entity::capacity() {
const QVector<cargo_entity> 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;
}

View file

@ -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_entity> cargo();
void serialize(QDataStream &output);
void deserialize(QDataStream &input);
};
#endif // VESSEL_ENTITY_H

View file

@ -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 \