From 7f92b5dbc9c24aaf2a3161f40bb6f1aed8cf6121 Mon Sep 17 00:00:00 2001 From: Mathew Sutcliffe Date: Wed, 12 Feb 2014 23:02:43 +0000 Subject: [PATCH] refs #140 to demonstrate the new tuple framework, adapted five typical value classes to use it --- src/blackmisc/avaircrafticao.cpp | 28 +++++-------------------- src/blackmisc/avaircrafticao.h | 2 ++ src/blackmisc/coordinategeodetic.cpp | 24 +++++---------------- src/blackmisc/coordinategeodetic.h | 2 ++ src/blackmisc/mathvector3dbase.cpp | 15 +++----------- src/blackmisc/mathvector3dbase.h | 4 ++++ src/blackmisc/nwserver.cpp | 28 +++++-------------------- src/blackmisc/nwserver.h | 2 ++ src/blackmisc/nwuser.cpp | 31 +++++----------------------- src/blackmisc/nwuser.h | 7 +++++++ 10 files changed, 40 insertions(+), 103 deletions(-) diff --git a/src/blackmisc/avaircrafticao.cpp b/src/blackmisc/avaircrafticao.cpp index 8971af5d3..c342acdfb 100644 --- a/src/blackmisc/avaircrafticao.cpp +++ b/src/blackmisc/avaircrafticao.cpp @@ -45,12 +45,7 @@ namespace BlackMisc { const auto &other = static_cast(otherBase); - const auto lhs = std::tie(this->m_designator, this->m_color, this->m_airline, this->m_livery); - const auto rhs = std::tie(other.m_designator, other.m_color, other.m_airline, other.m_livery); - - if (lhs < rhs) { return -1; } - if (lhs > rhs) { return 1; } - return 0; + return compare(TupleConverter::toTuple(*this), TupleConverter::toTuple(other)); } /* @@ -58,11 +53,7 @@ namespace BlackMisc */ void CAircraftIcao::marshallToDbus(QDBusArgument &argument) const { - argument << this->m_designator; - argument << this->m_airline; - argument << this->m_livery; - argument << this->m_type; - argument << this->m_color; + argument << TupleConverter::toTuple(*this); } /* @@ -70,11 +61,7 @@ namespace BlackMisc */ void CAircraftIcao::unmarshallFromDbus(const QDBusArgument &argument) { - argument >> this->m_designator; - argument >> this->m_airline; - argument >> this->m_livery; - argument >> this->m_type; - argument >> this->m_color; + argument >> TupleConverter::toTuple(*this); } /* @@ -100,7 +87,7 @@ namespace BlackMisc bool CAircraftIcao::operator ==(const CAircraftIcao &other) const { if (this == &other) return true; - return compare(*this, other) == 0; + return TupleConverter::toTuple(*this) == TupleConverter::toTuple(other); } /* @@ -116,12 +103,7 @@ namespace BlackMisc */ uint CAircraftIcao::getValueHash() const { - QList hashs; - hashs << qHash(this->m_designator); - hashs << qHash(this->m_airline); - hashs << qHash(this->m_type); - hashs << qHash(this->m_color); - return BlackMisc::calculateHash(hashs, "CAircraftIcao"); + return qHash(TupleConverter::toTuple(*this)); } /* diff --git a/src/blackmisc/avaircrafticao.h b/src/blackmisc/avaircrafticao.h index f03520261..a9211c25b 100644 --- a/src/blackmisc/avaircrafticao.h +++ b/src/blackmisc/avaircrafticao.h @@ -153,6 +153,7 @@ namespace BlackMisc virtual void unmarshallFromDbus(const QDBusArgument &argument) override; private: + BLACK_ENABLE_TUPLE_CONVERSION(CAircraftIcao) QString m_designator; QString m_type; QString m_airline; @@ -162,6 +163,7 @@ namespace BlackMisc } // namespace } // namespace +BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::Aviation::CAircraftIcao, (o.m_designator, o.m_type, o.m_airline, o.m_livery, o.m_color)) Q_DECLARE_METATYPE(BlackMisc::Aviation::CAircraftIcao) #endif // guard diff --git a/src/blackmisc/coordinategeodetic.cpp b/src/blackmisc/coordinategeodetic.cpp index e3f09db00..d60ac47ea 100644 --- a/src/blackmisc/coordinategeodetic.cpp +++ b/src/blackmisc/coordinategeodetic.cpp @@ -50,11 +50,7 @@ namespace BlackMisc { const auto &other = static_cast(otherBase); - int cmp = compare(this->m_latitude, other.m_latitude); - if (cmp) { return cmp; } - cmp = compare(this->m_longitude, other.m_longitude); - if (cmp) { return cmp; } - return compare(this->m_height, other.m_height); + return compare(TupleConverter::toTuple(*this), TupleConverter::toTuple(other)); } /* @@ -62,9 +58,7 @@ namespace BlackMisc */ void CCoordinateGeodetic::marshallToDbus(QDBusArgument &argument) const { - argument << this->m_latitude; - argument << this->m_longitude; - argument << this->m_height; + argument << TupleConverter::toTuple(*this); } /* @@ -72,9 +66,7 @@ namespace BlackMisc */ void CCoordinateGeodetic::unmarshallFromDbus(const QDBusArgument &argument) { - argument >> this->m_latitude; - argument >> this->m_longitude; - argument >> this->m_height; + argument >> TupleConverter::toTuple(*this); } /* @@ -83,9 +75,7 @@ namespace BlackMisc bool CCoordinateGeodetic::operator ==(const CCoordinateGeodetic &other) const { if (this == &other) return true; - return this->m_height == other.m_height && - this->m_latitude == other.m_latitude && - this->m_longitude == other.m_longitude; + return TupleConverter::toTuple(*this) == TupleConverter::toTuple(other); } /* @@ -110,11 +100,7 @@ namespace BlackMisc */ uint CCoordinateGeodetic::getValueHash() const { - QList hashs; - hashs << this->m_latitude.getValueHash(); - hashs << this->m_longitude.getValueHash(); - hashs << this->m_height.getValueHash(); - return BlackMisc::calculateHash(hashs, "CCoordinateGeodetic"); + return qHash(TupleConverter::toTuple(*this)); } /* diff --git a/src/blackmisc/coordinategeodetic.h b/src/blackmisc/coordinategeodetic.h index 0bb194086..1e967032b 100644 --- a/src/blackmisc/coordinategeodetic.h +++ b/src/blackmisc/coordinategeodetic.h @@ -70,6 +70,7 @@ namespace BlackMisc class CCoordinateGeodetic : public CValueObject, public ICoordinateGeodetic { private: + BLACK_ENABLE_TUPLE_CONVERSION(CCoordinateGeodetic) BlackMisc::Geo::CLatitude m_latitude; //!< Latitude BlackMisc::Geo::CLongitude m_longitude; //!< Longitude BlackMisc::PhysicalQuantities::CLength m_height; //!< height @@ -236,6 +237,7 @@ namespace BlackMisc } // namespace } // namespace +BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::Geo::CCoordinateGeodetic, (o.m_latitude, o.m_longitude, o.m_height)) Q_DECLARE_METATYPE(BlackMisc::Geo::CCoordinateGeodetic) #endif // guard diff --git a/src/blackmisc/mathvector3dbase.cpp b/src/blackmisc/mathvector3dbase.cpp index 26def4f35..78913eed4 100644 --- a/src/blackmisc/mathvector3dbase.cpp +++ b/src/blackmisc/mathvector3dbase.cpp @@ -52,12 +52,7 @@ namespace BlackMisc { const auto &other = static_cast(otherBase); - const auto lhs = std::tie(this->m_i, this->m_j, this->m_k); - const auto rhs = std::tie(other.m_i, other.m_j, other.m_k); - - if (lhs < rhs) { return -1; } - if (lhs > rhs) { return 1; } - return 0; + return compare(TupleConverter::toTuple(*this), TupleConverter::toTuple(other)); } /* @@ -162,9 +157,7 @@ namespace BlackMisc */ template void CVector3DBase::marshallToDbus(QDBusArgument &argument) const { - argument << this->m_i; - argument << this->m_j; - argument << this->m_k; + argument << TupleConverter::toTuple(*this); } /*! @@ -173,9 +166,7 @@ namespace BlackMisc */ template void CVector3DBase::unmarshallFromDbus(const QDBusArgument &argument) { - argument >> this->m_i; - argument >> this->m_j; - argument >> this->m_k; + argument >> TupleConverter::toTuple(*this); } /*! diff --git a/src/blackmisc/mathvector3dbase.h b/src/blackmisc/mathvector3dbase.h index beef729e3..4ee04b81a 100644 --- a/src/blackmisc/mathvector3dbase.h +++ b/src/blackmisc/mathvector3dbase.h @@ -23,6 +23,8 @@ namespace BlackMisc template class CVector3DBase : public CValueObject { private: + BLACK_ENABLE_TUPLE_CONVERSION(CVector3DBase) + /*! * \brief Easy access to derived class (CRTP template parameter) * \return @@ -366,4 +368,6 @@ namespace BlackMisc } // namespace } // namespace +BLACK_DECLARE_TUPLE_CONVERSION_TEMPLATE(BlackMisc::Math::CVector3DBase, (o.m_i, o.m_j, o.m_k)) + #endif // guard diff --git a/src/blackmisc/nwserver.cpp b/src/blackmisc/nwserver.cpp index e95554094..03ebdaf59 100644 --- a/src/blackmisc/nwserver.cpp +++ b/src/blackmisc/nwserver.cpp @@ -44,12 +44,7 @@ namespace BlackMisc { const auto &other = static_cast(otherBase); - const auto lhs = std::tie(this->m_name, this->m_description, this->m_address, this->m_port); - const auto rhs = std::tie(other.m_name, other.m_description, other.m_address, other.m_port); - - if (lhs < rhs) { return -1; } - if (lhs > rhs) { return 1; } - return compare(this->m_user, other.m_user); + return compare(TupleConverter::toTuple(*this), TupleConverter::toTuple(other)); } /* @@ -57,11 +52,7 @@ namespace BlackMisc */ void CServer::marshallToDbus(QDBusArgument &argument) const { - argument << this->m_name; - argument << this->m_description; - argument << this->m_address; - argument << this->m_port; - argument << this->m_user; + argument << TupleConverter::toTuple(*this); } /* @@ -69,11 +60,7 @@ namespace BlackMisc */ void CServer::unmarshallFromDbus(const QDBusArgument &argument) { - argument >> this->m_name; - argument >> this->m_description; - argument >> this->m_address; - argument >> this->m_port; - argument >> this->m_user; + argument >> TupleConverter::toTuple(*this); } /* @@ -90,7 +77,7 @@ namespace BlackMisc bool CServer::operator ==(const CServer &other) const { if (this == &other) return true; - return compare(*this, other) == 0; + return TupleConverter::toTuple(*this) == TupleConverter::toTuple(other); } /* @@ -106,12 +93,7 @@ namespace BlackMisc */ uint CServer::getValueHash() const { - QList hashs; - hashs << qHash(this->m_name); - hashs << qHash(this->m_address); - hashs << qHash(this->m_port); - hashs << qHash(this->m_user.getValueHash()); - return BlackMisc::calculateHash(hashs, "CServer"); + return qHash(TupleConverter::toTuple(*this)); } /* diff --git a/src/blackmisc/nwserver.h b/src/blackmisc/nwserver.h index ec9da6cda..2430d759b 100644 --- a/src/blackmisc/nwserver.h +++ b/src/blackmisc/nwserver.h @@ -172,6 +172,7 @@ namespace BlackMisc virtual void unmarshallFromDbus(const QDBusArgument &argument) override; private: + BLACK_ENABLE_TUPLE_CONVERSION(CServer) QString m_name; QString m_description; QString m_address; @@ -181,6 +182,7 @@ namespace BlackMisc } // namespace } // namespace +BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::Network::CServer, (o.m_name, o.m_description, o.m_address, o.m_port, o.m_user)) Q_DECLARE_METATYPE(BlackMisc::Network::CServer) #endif // guard diff --git a/src/blackmisc/nwuser.cpp b/src/blackmisc/nwuser.cpp index 107a8b8b1..d0186a96b 100644 --- a/src/blackmisc/nwuser.cpp +++ b/src/blackmisc/nwuser.cpp @@ -49,12 +49,7 @@ namespace BlackMisc { const auto &other = static_cast(otherBase); - const auto lhs = std::tie(this->m_id, this->m_realname, this->m_email, this->m_password); - const auto rhs = std::tie(other.m_id, other.m_realname, other.m_email, other.m_password); - - if (lhs < rhs) { return -1; } - if (lhs > rhs) { return 1; } - return 0; + return compare(TupleConverter::toTuple(*this), TupleConverter::toTuple(other)); } /* @@ -62,11 +57,7 @@ namespace BlackMisc */ void CUser::marshallToDbus(QDBusArgument &argument) const { - argument << this->m_id; - argument << this->m_realname; - argument << this->m_email; - argument << this->m_password; - argument << this->m_callsign; + argument << TupleConverter::toTuple(*this); } /* @@ -74,11 +65,7 @@ namespace BlackMisc */ void CUser::unmarshallFromDbus(const QDBusArgument &argument) { - argument >> this->m_id; - argument >> this->m_realname; - argument >> this->m_email; - argument >> this->m_password; - argument >> this->m_callsign; + argument >> TupleConverter::toTuple(*this); } /* @@ -87,10 +74,7 @@ namespace BlackMisc bool CUser::operator ==(const CUser &other) const { if (this == &other) return true; - return (this->m_id == other.m_id && - this->m_realname == other.m_realname && - this->m_email == other.m_email && - this->m_callsign == other.m_callsign); + return TupleConverter::toTuple(*this) == TupleConverter::toTuple(other); } /* @@ -134,12 +118,7 @@ namespace BlackMisc */ uint CUser::getValueHash() const { - QList hashs; - hashs << qHash(this->m_id); - hashs << qHash(this->m_realname); - hashs << qHash(this->m_email); - hashs << qHash(this->m_callsign.getValueHash()); - return BlackMisc::calculateHash(hashs, "CUser"); + return qHash(TupleConverter::toTuple(*this)); } /* diff --git a/src/blackmisc/nwuser.h b/src/blackmisc/nwuser.h index 4c25bbad8..b11d99272 100644 --- a/src/blackmisc/nwuser.h +++ b/src/blackmisc/nwuser.h @@ -14,8 +14,10 @@ namespace BlackMisc { + namespace Network { + /*! * Value object encapsulating information of a user. */ @@ -154,14 +156,19 @@ namespace BlackMisc virtual void unmarshallFromDbus(const QDBusArgument &argument) override; private: + BLACK_ENABLE_TUPLE_CONVERSION(CUser) QString m_id; QString m_realname; QString m_email; QString m_password; BlackMisc::Aviation::CCallsign m_callsign; }; + } // namespace + } // namespace +BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::Network::CUser, (o.m_id, o.m_realname, o.m_email, o.m_password, o.m_callsign)) Q_DECLARE_METATYPE(BlackMisc::Network::CUser) + #endif // guard