From 2b49591f1bb35cadb45d08c3a5053a02914fbb4d Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Tue, 24 May 2016 00:25:39 +0200 Subject: [PATCH] refs #649, do not init to current timestamp * force explicit init (performance/invalid objects) * allow to find mising timestamps * style --- src/blackmisc/identifier.cpp | 3 +- src/blackmisc/statusmessage.cpp | 8 +++- src/blackmisc/timestampbased.cpp | 36 +++++++++++++---- src/blackmisc/timestampbased.h | 5 ++- src/blackmisc/timestampobjectlist.cpp | 58 ++++++++++++++++++++++++--- src/blackmisc/timestampobjectlist.h | 27 ++++++++++++- 6 files changed, 118 insertions(+), 19 deletions(-) diff --git a/src/blackmisc/identifier.cpp b/src/blackmisc/identifier.cpp index a4f48c174..33c783f7f 100644 --- a/src/blackmisc/identifier.cpp +++ b/src/blackmisc/identifier.cpp @@ -16,7 +16,8 @@ namespace BlackMisc { CIdentifier::CIdentifier(const QString &name) - : m_name(name.trimmed()), + : ITimestampBased(QDateTime::currentMSecsSinceEpoch()), + m_name(name.trimmed()), m_machineIdBase64(QDBusConnection::localMachineId().toBase64()), m_machineName(QHostInfo::localHostName()), m_processName(QCoreApplication::applicationName()), diff --git a/src/blackmisc/statusmessage.cpp b/src/blackmisc/statusmessage.cpp index 269312da8..1f8c29743 100644 --- a/src/blackmisc/statusmessage.cpp +++ b/src/blackmisc/statusmessage.cpp @@ -59,7 +59,10 @@ namespace BlackMisc const StatusSeverity CStatusMessage::SeverityWarning; const StatusSeverity CStatusMessage::SeverityError; - CStatusMessage::CStatusMessage() = default; + CStatusMessage::CStatusMessage(): ITimestampBased(QDateTime::currentMSecsSinceEpoch()) + { + // void + } CStatusMessage::CStatusMessage(const CStatusMessage &other) : CValueObject(other), @@ -72,6 +75,7 @@ namespace BlackMisc CStatusMessage &CStatusMessage::operator =(const CStatusMessage &other) { + // locks because of mutable members if (this == &other) { return *this; } static_cast(*this) = other; @@ -85,7 +89,7 @@ namespace BlackMisc return *this; } - CStatusMessage::CStatusMessage(const QString &message) + CStatusMessage::CStatusMessage(const QString &message) : ITimestampBased(QDateTime::currentMSecsSinceEpoch()) { m_message = message.trimmed(); } diff --git a/src/blackmisc/timestampbased.cpp b/src/blackmisc/timestampbased.cpp index 329c5f684..81903d9c3 100644 --- a/src/blackmisc/timestampbased.cpp +++ b/src/blackmisc/timestampbased.cpp @@ -18,7 +18,7 @@ namespace BlackMisc { - ITimestampBased::ITimestampBased() : m_timestampMSecsSinceEpoch(QDateTime::currentMSecsSinceEpoch()) + ITimestampBased::ITimestampBased() { } ITimestampBased::ITimestampBased(qint64 msSincePoch) : m_timestampMSecsSinceEpoch(msSincePoch) @@ -67,7 +67,14 @@ namespace BlackMisc void ITimestampBased::setUtcTimestamp(const QDateTime ×tamp) { - this->m_timestampMSecsSinceEpoch = timestamp.toMSecsSinceEpoch(); + if (timestamp.isValid()) + { + this->m_timestampMSecsSinceEpoch = timestamp.toMSecsSinceEpoch(); + } + else + { + this->m_timestampMSecsSinceEpoch = -1; // invalid + } } bool ITimestampBased::isNewerThan(const ITimestampBased &otherTimestampObj) const @@ -119,27 +126,42 @@ namespace BlackMisc QString ITimestampBased::getFormattedUtcTimestampDhms() const { - return this->getUtcTimestamp().toString("dd hh:mm:ss"); + return this->hasValidTimestamp() ? + this->getUtcTimestamp().toString("dd hh:mm:ss") : + ""; } QString ITimestampBased::getFormattedUtcTimestampHms() const { - return this->getUtcTimestamp().toString("hh:mm:ss"); + return this->hasValidTimestamp() ? + this->getUtcTimestamp().toString("hh:mm:ss") : + ""; } QString ITimestampBased::getFormattedUtcTimestampHm() const { - return this->getUtcTimestamp().toString("hh::mm"); + return this->hasValidTimestamp() ? + this->getUtcTimestamp().toString("hh::mm") : + ""; } QString ITimestampBased::getFormattedUtcTimestampYmdhms() const { - return this->getUtcTimestamp().toString("yyyy-MM-dd HH:mm:ss"); + return this->hasValidTimestamp() ? + this->getUtcTimestamp().toString("yyyy-MM-dd HH:mm:ss") : + ""; } QString ITimestampBased::getFormattedUtcTimestampYmdhmsz() const { - return this->getUtcTimestamp().toString("yyyy-MM-dd HH:mm:ss.zzz"); + return this->hasValidTimestamp() ? + this->getUtcTimestamp().toString("yyyy-MM-dd HH:mm:ss.zzz") : + ""; + } + + bool ITimestampBased::hasValidTimestamp() const + { + return this->m_timestampMSecsSinceEpoch >= 0; } bool ITimestampBased::canHandleIndex(const CPropertyIndex &index) diff --git a/src/blackmisc/timestampbased.h b/src/blackmisc/timestampbased.h index 5d9f41604..51ad95cfe 100644 --- a/src/blackmisc/timestampbased.h +++ b/src/blackmisc/timestampbased.h @@ -95,6 +95,9 @@ namespace BlackMisc //! As yyyy MM dd HH mm ss.zzz QString getFormattedUtcTimestampYmdhmsz() const; + //! Valid timestamp? + bool hasValidTimestamp() const; + //! Can given index be handled static bool canHandleIndex(const BlackMisc::CPropertyIndex &index); @@ -117,7 +120,7 @@ namespace BlackMisc //! Compare for index int comparePropertyByIndex(const CPropertyIndex &index, const ITimestampBased &compareValue) const; - qint64 m_timestampMSecsSinceEpoch; //!< timestamp value + qint64 m_timestampMSecsSinceEpoch = -1; //!< timestamp value }; } // namespace diff --git a/src/blackmisc/timestampobjectlist.cpp b/src/blackmisc/timestampobjectlist.cpp index 0336ef850..473e3037e 100644 --- a/src/blackmisc/timestampobjectlist.cpp +++ b/src/blackmisc/timestampobjectlist.cpp @@ -7,18 +7,20 @@ * contained in the LICENSE file. */ -#include "blackmisc/timestampobjectlist.h" -#include "blackmisc/predicates.h" #include "blackmisc/aviation/aircraftsituationlist.h" #include "blackmisc/aviation/aircraftpartslist.h" #include "blackmisc/aviation/liverylist.h" #include "blackmisc/aviation/aircrafticaocodelist.h" #include "blackmisc/aviation/airlineicaocodelist.h" -#include "blackmisc/simulation/aircraftmodellist.h" -#include "blackmisc/simulation/distributorlist.h" +#include "blackmisc/db/dbinfolist.h" #include "blackmisc/network/textmessage.h" #include "blackmisc/network/textmessagelist.h" +#include "blackmisc/simulation/distributorlist.h" +#include "blackmisc/simulation/aircraftmodellist.h" +#include "blackmisc/simulation/distributorlist.h" #include "blackmisc/statusmessagelist.h" +#include "blackmisc/timestampobjectlist.h" +#include "blackmisc/predicates.h" #include "blackmisc/identifierlist.h" #include "blackmisc/countrylist.h" @@ -85,6 +87,21 @@ namespace BlackMisc }); } + template + CONTAINER ITimestampObjectList::findInvalidTimestamps() const + { + return this->container().findBy([&](const OBJ & obj) + { + return !obj.hasValidTimestamp(); + }); + } + + template + bool ITimestampObjectList::hasInvalidTimestamps() const + { + return this->container().contains(&OBJ::hasValidTimestamp, false); + } + template QList ITimestampObjectList::splitByTime(qint64 msSinceEpoch, bool sortedLatestFirst) const { @@ -111,11 +128,39 @@ namespace BlackMisc return result; } + template + QDateTime ITimestampObjectList::latestTimestamp() const + { + if (this->container().isEmpty()) { return QDateTime(); } + return this->latestObject().getUtcTimestamp(); + } + + template + qint64 ITimestampObjectList::latestTimestampMsecsSinceEpoch() const + { + const QDateTime dt(latestTimestamp()); + return dt.isValid() ? dt.toMSecsSinceEpoch() : -1; + } + + template + QDateTime ITimestampObjectList::oldestTimestamp() const + { + if (this->container().isEmpty()) { return QDateTime(); } + return this->oldestObject().getUtcTimestamp(); + } + + template + qint64 ITimestampObjectList::oldestTimestampMsecsSinceEpoch() const + { + const QDateTime dt(oldestTimestamp()); + return dt.isValid() ? dt.toMSecsSinceEpoch() : -1; + } + template OBJ ITimestampObjectList::latestObject() const { if (this->container().isEmpty()) { return OBJ(); } - auto latest = std::max_element(container().begin(), container().end(), [](const OBJ & a, const OBJ & b) { return a.getMSecsSinceEpoch() < b.getMSecsSinceEpoch(); }); + const auto latest = std::max_element(container().begin(), container().end(), [](const OBJ & a, const OBJ & b) { return a.getMSecsSinceEpoch() < b.getMSecsSinceEpoch(); }); return *latest; } @@ -123,7 +168,7 @@ namespace BlackMisc OBJ ITimestampObjectList::oldestObject() const { if (this->container().isEmpty()) { return OBJ(); } - auto oldest = std::min_element(container().begin(), container().end(), [](const OBJ & a, const OBJ & b) { return a.getMSecsSinceEpoch() < b.getMSecsSinceEpoch(); }); + const auto oldest = std::min_element(container().begin(), container().end(), [](const OBJ & a, const OBJ & b) { return a.getMSecsSinceEpoch() < b.getMSecsSinceEpoch(); }); return *oldest; } @@ -189,6 +234,7 @@ namespace BlackMisc template class ITimestampObjectList; template class ITimestampObjectList; template class ITimestampObjectList; + template class ITimestampObjectList; template class ITimestampObjectList; template class ITimestampObjectList; template class ITimestampObjectList; diff --git a/src/blackmisc/timestampobjectlist.h b/src/blackmisc/timestampobjectlist.h index 26bcab5cd..8ae890e5e 100644 --- a/src/blackmisc/timestampobjectlist.h +++ b/src/blackmisc/timestampobjectlist.h @@ -13,7 +13,6 @@ #define BLACKMISC_TIMESTAMPOBJECTLIST_H #include "blackmisc/blackmiscexport.h" - #include #include @@ -27,7 +26,6 @@ namespace BlackMisc class ITimestampObjectList { public: - //! List of objects before dateTime CONTAINER findBefore(const QDateTime &dateTime) const; @@ -46,10 +44,28 @@ namespace BlackMisc //! List of objects after msSinceEpoch CONTAINER findAfter(qint64 msSinceEpoch) const; + //! Objects without valid timestamp + CONTAINER findInvalidTimestamps() const; + + //! Has invalid timestamp + bool hasInvalidTimestamps() const; + //! Partition into two containers, first [0,msSinceEpoch] and second (msSinceEpoch,LLONG_MAX]. //! Within each of the two parts, the original relative ordering of the elements is preserved. QList splitByTime(qint64 msSinceEpoch, bool sortedLatestFirst = false) const; + //! Latest timestamp + QDateTime latestTimestamp() const; + + //! Latest timestamp + qint64 latestTimestampMsecsSinceEpoch() const; + + //! Oldest timestamp + QDateTime oldestTimestamp() const; + + //! Oldest timestamp + qint64 oldestTimestampMsecsSinceEpoch() const; + //! Latest value OBJ latestObject() const; @@ -106,6 +122,12 @@ namespace BlackMisc class CTextMessageList; } + namespace Db + { + class CDbInfo; + class CDbInfoList; + } + namespace Simulation { class CDistributor; @@ -126,6 +148,7 @@ namespace BlackMisc extern template class BLACKMISC_EXPORT_TEMPLATE ITimestampObjectList; extern template class BLACKMISC_EXPORT_TEMPLATE ITimestampObjectList; extern template class BLACKMISC_EXPORT_TEMPLATE ITimestampObjectList; + extern template class BLACKMISC_EXPORT_TEMPLATE ITimestampObjectList; extern template class BLACKMISC_EXPORT_TEMPLATE ITimestampObjectList; extern template class BLACKMISC_EXPORT_TEMPLATE ITimestampObjectList; extern template class BLACKMISC_EXPORT_TEMPLATE ITimestampObjectList;