From de2f8e01b24deba14a20e21ab7b7fe1ae3f5b6c3 Mon Sep 17 00:00:00 2001 From: "E. Kozlovskaya" Date: Sun, 3 Jan 2021 23:22:49 +0700 Subject: [PATCH] Add serialization and comparison to User class --- iFacility/objects/user.cpp | 28 ++++++++++++++++++++++++---- iFacility/objects/user.h | 8 ++++++-- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/iFacility/objects/user.cpp b/iFacility/objects/user.cpp index 1752f1a..0770360 100644 --- a/iFacility/objects/user.cpp +++ b/iFacility/objects/user.cpp @@ -51,18 +51,18 @@ bool User::addProfession(const Profession &p) { return false; } - if (!mProfessions.contains(p)) { + if (!mProfessions.contains(p.pID())) { if (mProfessions.size() >= 4) { mProfessions.remove(0); } - mProfessions.push_back(p); + mProfessions.push_back(p.pID()); } return true; } bool User::setCurrentProfession(const Profession &p) { - if (!mProfessions.contains(p)) { + if (!mProfessions.contains(p.pID())) { return false; } @@ -74,5 +74,25 @@ bool User::setCurrentProfession(const Profession &p) { } void User::removeProfession(const Profession &p) { - mProfessions.removeAll(p); + mProfessions.removeAll(p.pID()); +} + +bool operator==(const User &l, const User &r) { + return l.mUID == r.mUID && l.mLogin == r.mLogin; +} + +QDataStream& operator<<(QDataStream &stream, const User &usr) { + stream << usr.mUID + << usr.mLogin << usr.mPassword + << usr.mFirstName << usr.mSecondName << usr.mPatronymic + << usr.mProfessions << usr.mCurrentProfession; + return stream; +} + +QDataStream& operator>>(QDataStream &stream, User &usr) { + stream >> usr.mUID + >> usr.mLogin >> usr.mPassword + >> usr.mFirstName >> usr.mSecondName >> usr.mPatronymic + >> usr.mProfessions >> usr.mCurrentProfession; + return stream; } diff --git a/iFacility/objects/user.h b/iFacility/objects/user.h index 136b6b9..34385ab 100644 --- a/iFacility/objects/user.h +++ b/iFacility/objects/user.h @@ -8,7 +8,7 @@ #include "profession.h" typedef QUuid UID; -typedef QVector ProfessionsList; +typedef QVector ProfessionsList; class User { private: @@ -21,9 +21,9 @@ private: ProfessionsList mProfessions; PID mCurrentProfession = 0; +public: User() = default; -public: UID uID() const; QString getLogin() const; bool checkPassword(const QString &password); @@ -39,6 +39,10 @@ public: bool addProfession(const Profession &p); bool setCurrentProfession(const Profession &p); void removeProfession(const Profession &p); + + friend bool operator==(const User &l, const User &r); + friend QDataStream& operator<<(QDataStream &stream, const User &usr); + friend QDataStream& operator>>(QDataStream &stream, User &usr); }; #endif // USER_H