/* 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/timestampobjectlist.h" #include "blackmisc/predicates.h" #include "blackmisc/avaircraftsituationlist.h" #include "blackmisc/aviation/aircraftpartslist.h" #include #include namespace BlackMisc { template ITimestampObjectList::ITimestampObjectList() { } template CONTAINER ITimestampObjectList::findBefore(qint64 msSinceEpoch) const { return this->container().findBy([&](const OBJ & obj) { return obj.isOlderThan(msSinceEpoch); }); } 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 QList ITimestampObjectList::splitByTime(qint64 msSinceEpoch) const { CONTAINER newer(this->container()); newer.sortLatestFirst(); CONTAINER older; for (auto it = newer.begin(); it != newer.end(); ++it) { if (it->isOlderThan(msSinceEpoch)) { // better "move", ?? std::make_move_iterator older.insert(CRange(it, newer.end())); newer.erase(it, newer.end()); break; } } // before / after return QList({newer, older}); } template OBJ ITimestampObjectList::latestValue() const { if (this->container().isEmpty()) { return OBJ(); } CONTAINER container(container()); // copy container.sortLatestFirst(); return container.front(); } template OBJ ITimestampObjectList::oldestValue() const { if (this->container().isEmpty()) { return OBJ(); } CONTAINER container(container()); // copy container.sortLatestFirst(); return container.back(); } template CONTAINER ITimestampObjectList::findAfter(const QDateTime &dateTime) const { return this->findAfter(dateTime.toMSecsSinceEpoch()); } template void ITimestampObjectList::removeBefore(const QDateTime &dateTime) { this->removeBefore(dateTime.toMSecsSinceEpoch()); } template void ITimestampObjectList::removeBefore(qint64 msSinceEpoc) { this->container().removeIf([&](const OBJ & obj) { return obj.isOlderThan(msSinceEpoc); }); } template void ITimestampObjectList::removeOlderThanNowMinusOffset(int offsetMs) { const qint64 epoch = QDateTime::currentMSecsSinceEpoch() - offsetMs; 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::insertTimestampObject(const OBJ &object, int maxElements) { Q_ASSERT(maxElements > 1); if (this->container().size() >= (maxElements - 1)) { this->container().truncate(maxElements - 1); } this->container().insert_front(object); } // see here for the reason of thess forward instantiations // http://www.parashift.com/c++-faq/separate-template-class-defn-from-decl.html template class ITimestampObjectList; template class ITimestampObjectList; } // namespace