refs #649, do not init to current timestamp

* force explicit init (performance/invalid objects)
* allow to find mising timestamps
* style
This commit is contained in:
Klaus Basan
2016-05-24 00:25:39 +02:00
parent 9606ab12db
commit 2b49591f1b
6 changed files with 118 additions and 19 deletions

View File

@@ -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()),

View File

@@ -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<CMessageBase &>(*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();
}

View File

@@ -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 &timestamp)
{
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)

View File

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

View File

@@ -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 <class OBJ, class CONTAINER>
CONTAINER ITimestampObjectList<OBJ, CONTAINER>::findInvalidTimestamps() const
{
return this->container().findBy([&](const OBJ & obj)
{
return !obj.hasValidTimestamp();
});
}
template <class OBJ, class CONTAINER>
bool ITimestampObjectList<OBJ, CONTAINER>::hasInvalidTimestamps() const
{
return this->container().contains(&OBJ::hasValidTimestamp, false);
}
template <class OBJ, class CONTAINER>
QList<CONTAINER> ITimestampObjectList<OBJ, CONTAINER>::splitByTime(qint64 msSinceEpoch, bool sortedLatestFirst) const
{
@@ -111,11 +128,39 @@ namespace BlackMisc
return result;
}
template <class OBJ, class CONTAINER>
QDateTime ITimestampObjectList<OBJ, CONTAINER>::latestTimestamp() const
{
if (this->container().isEmpty()) { return QDateTime(); }
return this->latestObject().getUtcTimestamp();
}
template <class OBJ, class CONTAINER>
qint64 ITimestampObjectList<OBJ, CONTAINER>::latestTimestampMsecsSinceEpoch() const
{
const QDateTime dt(latestTimestamp());
return dt.isValid() ? dt.toMSecsSinceEpoch() : -1;
}
template <class OBJ, class CONTAINER>
QDateTime ITimestampObjectList<OBJ, CONTAINER>::oldestTimestamp() const
{
if (this->container().isEmpty()) { return QDateTime(); }
return this->oldestObject().getUtcTimestamp();
}
template <class OBJ, class CONTAINER>
qint64 ITimestampObjectList<OBJ, CONTAINER>::oldestTimestampMsecsSinceEpoch() const
{
const QDateTime dt(oldestTimestamp());
return dt.isValid() ? dt.toMSecsSinceEpoch() : -1;
}
template <class OBJ, class CONTAINER>
OBJ ITimestampObjectList<OBJ, CONTAINER>::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<OBJ, CONTAINER>::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<BlackMisc::Aviation::CLivery, BlackMisc::Aviation::CLiveryList>;
template class ITimestampObjectList<BlackMisc::Aviation::CAircraftIcaoCode, BlackMisc::Aviation::CAircraftIcaoCodeList>;
template class ITimestampObjectList<BlackMisc::Aviation::CAirlineIcaoCode, BlackMisc::Aviation::CAirlineIcaoCodeList>;
template class ITimestampObjectList<BlackMisc::Db::CDbInfo, BlackMisc::Db::CDbInfoList>;
template class ITimestampObjectList<BlackMisc::Simulation::CAircraftModel, BlackMisc::Simulation::CAircraftModelList>;
template class ITimestampObjectList<BlackMisc::Simulation::CDistributor, BlackMisc::Simulation::CDistributorList>;
template class ITimestampObjectList<BlackMisc::Network::CTextMessage, BlackMisc::Network::CTextMessageList>;

View File

@@ -13,7 +13,6 @@
#define BLACKMISC_TIMESTAMPOBJECTLIST_H
#include "blackmisc/blackmiscexport.h"
#include <QList>
#include <QtGlobal>
@@ -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<CONTAINER> 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<BlackMisc::Aviation::CLivery, BlackMisc::Aviation::CLiveryList>;
extern template class BLACKMISC_EXPORT_TEMPLATE ITimestampObjectList<BlackMisc::Aviation::CAircraftIcaoCode, BlackMisc::Aviation::CAircraftIcaoCodeList>;
extern template class BLACKMISC_EXPORT_TEMPLATE ITimestampObjectList<BlackMisc::Aviation::CAirlineIcaoCode, BlackMisc::Aviation::CAirlineIcaoCodeList>;
extern template class BLACKMISC_EXPORT_TEMPLATE ITimestampObjectList<BlackMisc::Db::CDbInfo, BlackMisc::Db::CDbInfoList>;
extern template class BLACKMISC_EXPORT_TEMPLATE ITimestampObjectList<BlackMisc::Simulation::CAircraftModel, BlackMisc::Simulation::CAircraftModelList>;
extern template class BLACKMISC_EXPORT_TEMPLATE ITimestampObjectList<BlackMisc::Simulation::CDistributor, BlackMisc::Simulation::CDistributorList>;
extern template class BLACKMISC_EXPORT_TEMPLATE ITimestampObjectList<BlackMisc::Network::CTextMessage, BlackMisc::Network::CTextMessageList>;