/* Copyright (C) 2015 * swift Project Community / Contributors * * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, * including this file, may be copied, modified, propagated, or distributed except according to the terms * contained in the LICENSE file. */ #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/aviation/airport.h" #include "blackmisc/aviation/airportlist.h" #include "blackmisc/db/dbinfolist.h" #include "blackmisc/db/distributionlist.h" #include "blackmisc/network/textmessage.h" #include "blackmisc/network/textmessagelist.h" #include "blackmisc/network/urllog.h" #include "blackmisc/network/urlloglist.h" #include "blackmisc/simulation/distributorlist.h" #include "blackmisc/simulation/aircraftmodellist.h" #include "blackmisc/simulation/distributorlist.h" #include "blackmisc/simulation/matchingstatistics.h" #include "blackmisc/statusmessagelist.h" #include "blackmisc/timestampobjectlist.h" #include "blackmisc/predicates.h" #include "blackmisc/identifierlist.h" #include "blackmisc/countrylist.h" #include #include #include #include namespace BlackMisc { template ITimestampObjectList::ITimestampObjectList() { static_assert(std::is_base_of::value, "OBJ needs to implement ITimestampBased"); } template const CONTAINER &ITimestampObjectList::container() const { return static_cast(*this); } template CONTAINER &ITimestampObjectList::container() { return static_cast(*this); } template CONTAINER ITimestampObjectList::findBefore(qint64 msSinceEpoch) const { return this->container().findBy([&](const OBJ & obj) { return obj.isOlderThan(msSinceEpoch); }); } template CONTAINER ITimestampObjectList::findBeforeAndRemove(qint64 msSinceEpoch) { CONTAINER result(findBefore(msSinceEpoch)); this->removeBefore(msSinceEpoch); return result; } template CONTAINER ITimestampObjectList::findBeforeNowMinusOffset(qint64 msOffset) const { return this->findBefore(QDateTime::currentMSecsSinceEpoch() - msOffset); } template CONTAINER ITimestampObjectList::findBefore(const QDateTime &dateTime) const { return this->findBefore(dateTime.toMSecsSinceEpoch()); } template CONTAINER ITimestampObjectList::findAfter(qint64 msSinceEpoc) const { return this->container().findBy([&](const OBJ & obj) { return obj.isNewerThan(msSinceEpoc); }); } 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 void ITimestampObjectList::setCurrentUtcTime() { for (ITimestampBased &tsObj : this->container()) { tsObj.setCurrentUtcTime(); } } template void ITimestampObjectList::setUtcTime(qint64 msSinceEpoch) { for (ITimestampBased &tsObj : this->container()) { tsObj.setMSecsSinceEpoch(msSinceEpoch); } } template void ITimestampObjectList::setInvalidTimestampsToCurrentUtcTime() { for (ITimestampBased &tsObj : this->container()) { if (tsObj.hasValidTimestamp()) { continue; } tsObj.setCurrentUtcTime(); } } 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(); } const auto latest = std::max_element(container().begin(), container().end(), [](const OBJ & a, const OBJ & b) { return a.getMSecsSinceEpoch() < b.getMSecsSinceEpoch(); }); return *latest; } template OBJ ITimestampObjectList::oldestObject() const { if (this->container().isEmpty()) { return OBJ(); } const auto oldest = std::min_element(container().begin(), container().end(), [](const OBJ & a, const OBJ & b) { return a.getMSecsSinceEpoch() < b.getMSecsSinceEpoch(); }); return *oldest; } template CONTAINER ITimestampObjectList::findAfter(const QDateTime &dateTime) const { return this->findAfter(dateTime.toMSecsSinceEpoch()); } template int ITimestampObjectList::removeBefore(const QDateTime &dateTime) { return this->removeBefore(dateTime.toMSecsSinceEpoch()); } template int ITimestampObjectList::removeBefore(qint64 msSinceEpoc) { return this->container().removeIf([&](const OBJ & obj) { return obj.isOlderThan(msSinceEpoc); }); } template int ITimestampObjectList::removeOlderThanNowMinusOffset(qint64 offsetMs) { const qint64 epoch = QDateTime::currentMSecsSinceEpoch() - offsetMs; return this->container().removeIf([&](const OBJ & obj) { return obj.isOlderThan(epoch); }); } template void ITimestampObjectList::sortLatestFirst() { this->container().sortOldestFirst(); std::reverse(this->container().begin(), this->container().end()); } template void ITimestampObjectList::sortOldestFirst() { this->container().sort(BlackMisc::Predicates::MemberLess(&OBJ::getMSecsSinceEpoch)); } template void ITimestampObjectList::push_frontMaxElements(const OBJ &object, int maxElements) { Q_ASSERT(maxElements > 1); if (this->container().size() >= (maxElements - 1)) { this->container().truncate(maxElements - 1); } this->container().push_front(object); } // see here for the reason of thess forward instantiations // https://isocpp.org/wiki/faq/templates#separate-template-fn-defn-from-decl //! \cond PRIVATE template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList; template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList; template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList; template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList; template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList; template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList; template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList; template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList; template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList; template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList; template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList; template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList; template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList; template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList; template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList; template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList; //! \endcond } // namespace