From 76ac6245198efe3df918e2ee20e47a44b5f2fef2 Mon Sep 17 00:00:00 2001 From: Andrew nuark G Date: Tue, 15 Dec 2020 21:44:41 +0700 Subject: [PATCH] Entities improvement --- entities/IEntity.h | 4 +++- entities/cargo_entity.cpp | 2 +- entities/cargo_entity.h | 4 ++-- entities/dpoint_entity.cpp | 4 ++-- entities/dpoint_entity.h | 8 ++++---- entities/storage_entity.cpp | 12 ++++++++---- entities/storage_entity.h | 11 ++++++----- entities/user_entity.cpp | 2 +- entities/user_entity.h | 4 ++-- entities/vessel_entity.cpp | 8 ++++++-- entities/vessel_entity.h | 7 ++++--- 11 files changed, 39 insertions(+), 27 deletions(-) diff --git a/entities/IEntity.h b/entities/IEntity.h index faeddfb..a1dbca3 100644 --- a/entities/IEntity.h +++ b/entities/IEntity.h @@ -4,9 +4,11 @@ #include "ISerializable.h" +typedef unsigned long long entity_id; + class IEntity : public ISerializable { public: - virtual unsigned long long id() = 0; + virtual entity_id id() = 0; }; #endif // IENTITY_H diff --git a/entities/cargo_entity.cpp b/entities/cargo_entity.cpp index 27d37f8..4a689f6 100644 --- a/entities/cargo_entity.cpp +++ b/entities/cargo_entity.cpp @@ -8,7 +8,7 @@ cargo_entity::cargo_entity(const QString &title, unsigned int volume) : _title(t } } -unsigned long long cargo_entity::id() { +entity_id cargo_entity::id() { return this->_id; } diff --git a/entities/cargo_entity.h b/entities/cargo_entity.h index ab5e043..07d151b 100644 --- a/entities/cargo_entity.h +++ b/entities/cargo_entity.h @@ -9,7 +9,7 @@ class cargo_entity : public IEntity { private: - unsigned long long _id; + entity_id _id; QString _title; unsigned int _volume; @@ -17,7 +17,7 @@ public: cargo_entity() = default; cargo_entity(const QString &title, unsigned int volume); - unsigned long long id(); + entity_id id(); QString title(); unsigned int volume(); diff --git a/entities/dpoint_entity.cpp b/entities/dpoint_entity.cpp index fd43073..0f8c32c 100644 --- a/entities/dpoint_entity.cpp +++ b/entities/dpoint_entity.cpp @@ -8,7 +8,7 @@ dpoint_entity::dpoint_entity(const QString &title) : _title(title) { } } -unsigned long long dpoint_entity::id() { +entity_id dpoint_entity::id() { return this->_id; } @@ -16,7 +16,7 @@ QString dpoint_entity::title() { return this->_title; } -const QVector dpoint_entity::storages_ids() { +const QVector dpoint_entity::storages_ids() { return this->_storages_ids; } diff --git a/entities/dpoint_entity.h b/entities/dpoint_entity.h index e431dca..6c363a5 100644 --- a/entities/dpoint_entity.h +++ b/entities/dpoint_entity.h @@ -10,17 +10,17 @@ class dpoint_entity : public IEntity { private: - unsigned long long _id; + entity_id _id; QString _title; - QVector _storages_ids; + QVector _storages_ids; public: dpoint_entity() = default; dpoint_entity(const QString &title); - unsigned long long id(); + entity_id id(); QString title(); - const QVector storages_ids(); + const QVector storages_ids(); void serialize(QDataStream &output); void deserialize(QDataStream &input); diff --git a/entities/storage_entity.cpp b/entities/storage_entity.cpp index d0da1d2..903250d 100644 --- a/entities/storage_entity.cpp +++ b/entities/storage_entity.cpp @@ -1,14 +1,14 @@ #include "storage_entity.h" -unsigned long long storage_entity::__global_id = 0; +entity_id storage_entity::__global_id = 0; storage_entity::storage_entity(unsigned int capacity) : _capacity(capacity) { this->_id = ++storage_entity::__global_id; } -unsigned long long storage_entity::id() { +entity_id storage_entity::id() { return this->_id; } @@ -28,7 +28,7 @@ void storage_entity::add_cargo(cargo_entity object, bool &success) { } } -cargo_entity storage_entity::get_cargo(unsigned long long oid, bool &found) { +cargo_entity storage_entity::get_cargo(entity_id oid, bool &found) { cargo_entity ent; found = false; @@ -43,7 +43,7 @@ cargo_entity storage_entity::get_cargo(unsigned long long oid, bool &found) { return ent; } -void storage_entity::withdraw_cargo(unsigned long long oid, bool &success) { +void storage_entity::withdraw_cargo(entity_id oid, bool &success) { success = false; auto vit = this->_cargo.begin(); for (; vit != this->_cargo.end(); vit++) { @@ -71,3 +71,7 @@ void storage_entity::deserialize(QDataStream &input) { this->_cargo[i].deserialize(input); } } + +void storage_entity::preloadGlobalId(entity_id gid) { + storage_entity::__global_id = gid; +} diff --git a/entities/storage_entity.h b/entities/storage_entity.h index c4285e0..253d387 100644 --- a/entities/storage_entity.h +++ b/entities/storage_entity.h @@ -10,9 +10,9 @@ class storage_entity : public IEntity { private: - static unsigned long long __global_id; + static entity_id __global_id; - unsigned long long _id; + entity_id _id; unsigned int _capacity; QVector _cargo; @@ -20,16 +20,17 @@ public: storage_entity() = default; storage_entity(unsigned int capacity); - unsigned long long id(); + entity_id id(); unsigned int capacity(); const QVector cargo(); void add_cargo(cargo_entity object, bool &success); - cargo_entity get_cargo(unsigned long long oid, bool &found); - void withdraw_cargo(unsigned long long oid, bool &success); + cargo_entity get_cargo(entity_id oid, bool &found); + void withdraw_cargo(entity_id oid, bool &success); void serialize(QDataStream &output); void deserialize(QDataStream &input); + void preloadGlobalId(entity_id gid); }; #endif // STORAGE_ENTITY_H diff --git a/entities/user_entity.cpp b/entities/user_entity.cpp index ef8508c..88a1607 100644 --- a/entities/user_entity.cpp +++ b/entities/user_entity.cpp @@ -4,7 +4,7 @@ user_entity::user_entity(const QString &login, const QString &password, UserRole this->_pwd_hash = QCryptographicHash::hash(password.toLocal8Bit(), QCryptographicHash::Sha3_256); } -unsigned long long user_entity::id() { +entity_id user_entity::id() { return this->_id; } diff --git a/entities/user_entity.h b/entities/user_entity.h index cf87e8e..670152f 100644 --- a/entities/user_entity.h +++ b/entities/user_entity.h @@ -14,7 +14,7 @@ enum class UserRole { class user_entity : public IEntity { private: - unsigned long long _id; + entity_id _id; QString _login; UserRole _role; QByteArray _pwd_hash; @@ -23,7 +23,7 @@ public: user_entity() = default; user_entity(const QString &login, const QString &password, UserRole role); - unsigned long long id(); + entity_id id(); const QString login(); UserRole role(); bool verify_password(const QString &password); diff --git a/entities/vessel_entity.cpp b/entities/vessel_entity.cpp index aeafd91..fa14841 100644 --- a/entities/vessel_entity.cpp +++ b/entities/vessel_entity.cpp @@ -1,13 +1,13 @@ #include "vessel_entity.h" -unsigned long long vessel_entity::__global_id = 0; +entity_id vessel_entity::__global_id = 0; vessel_entity::vessel_entity(const dpoint_entity &harbor, unsigned int capacity) : _harbor(harbor), _capacity(capacity) { this->_id = ++vessel_entity::__global_id; } -unsigned long long vessel_entity::id() { +entity_id vessel_entity::id() { return this->_id; } @@ -42,3 +42,7 @@ void vessel_entity::deserialize(QDataStream &input) { this->_cargo[i].deserialize(input); } } + +void vessel_entity::preloadGlobalId(entity_id gid) { + vessel_entity::__global_id = gid; +} diff --git a/entities/vessel_entity.h b/entities/vessel_entity.h index a0c6004..9bede23 100644 --- a/entities/vessel_entity.h +++ b/entities/vessel_entity.h @@ -8,9 +8,9 @@ class vessel_entity : public IEntity { private: - static unsigned long long __global_id; + static entity_id __global_id; - unsigned long long _id; + entity_id _id; dpoint_entity _harbor; unsigned int _capacity; QVector _cargo; @@ -19,13 +19,14 @@ public: vessel_entity() = default; vessel_entity(const dpoint_entity &harbor, unsigned int capacity); - unsigned long long id(); + entity_id id(); const dpoint_entity harbor(); unsigned int capacity(); const QVector cargo(); void serialize(QDataStream &output); void deserialize(QDataStream &input); + static void preloadGlobalId(entity_id gid); }; #endif // VESSEL_ENTITY_H