refs #369, adjusted aircraft parts/lights and list to new style

* support of property index
* used support classes BlackMisc::ITimestampBased, ITimestampObjectList and ICallsignObjectList
* fixed some issues in related aircraft situation class in same step
This commit is contained in:
Klaus Basan
2015-02-06 21:25:19 +01:00
parent 466a9a24ef
commit ee3417ff8b
14 changed files with 361 additions and 116 deletions

View File

@@ -1,5 +1,5 @@
/* Copyright (C) 2015
* swift project Community / Contributors
* 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,
@@ -7,72 +7,146 @@
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKMISC_TIMESTAMPOBJECTLIST_H
#define BLACKMISC_TIMESTAMPOBJECTLIST_H
#include "blackmisc/collection.h"
#include "blackmisc/sequence.h"
#include <QList>
#include <QMap>
#include "blackmisc/timestampobjectlist.h"
#include "blackmisc/predicates.h"
#include "blackmisc/avaircraftsituationlist.h"
#include "blackmisc/aviation/aircraftpartslist.h"
#include <algorithm>
#include <iterator>
namespace BlackMisc
{
//! List of objects with timestamp.
//! Such objects should implement \sa ITimestampBased
template<class OBJ, class CONTAINER>
class ITimestampObjectList
template <class OBJ, class CONTAINER>
ITimestampObjectList<OBJ, CONTAINER>::ITimestampObjectList()
{ }
template <class OBJ, class CONTAINER>
CONTAINER ITimestampObjectList<OBJ, CONTAINER>::findBefore(qint64 msSinceEpoch) const
{
public:
return this->container().findBy([&](const OBJ & obj)
{
return obj.isOlderThan(msSinceEpoch);
});
}
//! List of objects before dateTime
CONTAINER findBefore(const QDateTime &dateTime) const;
template <class OBJ, class CONTAINER>
CONTAINER ITimestampObjectList<OBJ, CONTAINER>::findBeforeNowMinusOffset(qint64 msOffset) const
{
return this->findBefore(QDateTime::currentMSecsSinceEpoch() - msOffset);
}
//! List of objects before msSinceEpoch
CONTAINER findBefore(qint64 msSinceEpoch) const;
template <class OBJ, class CONTAINER>
CONTAINER ITimestampObjectList<OBJ, CONTAINER>::findBefore(const QDateTime &dateTime) const
{
return this->findBefore(dateTime.toMSecsSinceEpoch());
}
//! List of objects after dateTime
CONTAINER findAfter(const QDateTime &dateTime) const;
template <class OBJ, class CONTAINER>
CONTAINER ITimestampObjectList<OBJ, CONTAINER>::findAfter(qint64 msSinceEpoc) const
{
return this->container().findBy([&](const OBJ & obj)
{
return obj.isNewerThan(msSinceEpoc);
});
}
//! List of objects after msSinceEpoch
CONTAINER findAfter(qint64 msSinceEpoch) const;
template <class OBJ, class CONTAINER>
QList<CONTAINER> ITimestampObjectList<OBJ, CONTAINER>::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<CONTAINER::iterator>(it, newer.end()));
newer.erase(it, newer.end());
break;
}
}
//! Split into 2 containers, [0] >= msSinceEpoch [b] < msSinceEpoch
QList<CONTAINER> splitByTime(qint64 msSinceEpoch) const;
// before / after
return QList<CONTAINER>({newer, older});
}
//! Latest value
OBJ latestValue() const;
template <class OBJ, class CONTAINER>
OBJ ITimestampObjectList<OBJ, CONTAINER>::latestValue() const
{
if (this->container().isEmpty()) { return OBJ(); }
CONTAINER container(container()); // copy
container.sortLatestFirst();
return container.front();
}
//! Latest value
OBJ oldestValue() const;
template <class OBJ, class CONTAINER>
OBJ ITimestampObjectList<OBJ, CONTAINER>::oldestValue() const
{
if (this->container().isEmpty()) { return OBJ(); }
CONTAINER container(container()); // copy
container.sortLatestFirst();
return container.back();
}
//! Remove objects with timestamp before dateTime
void removeBefore(const QDateTime &dateTime);
template <class OBJ, class CONTAINER>
CONTAINER ITimestampObjectList<OBJ, CONTAINER>::findAfter(const QDateTime &dateTime) const
{
return this->findAfter(dateTime.toMSecsSinceEpoch());
}
//! Remove objects with timestamp before dateTime
void removeBefore(qint64 msSinceEpoch);
template <class OBJ, class CONTAINER>
void ITimestampObjectList<OBJ, CONTAINER>::removeBefore(const QDateTime &dateTime)
{
this->removeBefore(dateTime.toMSecsSinceEpoch());
}
//! Remove objects older than seconds
void removeOlderThanNowMinusOffset(int offsetMs);
template <class OBJ, class CONTAINER>
void ITimestampObjectList<OBJ, CONTAINER>::removeBefore(qint64 msSinceEpoc)
{
this->container().removeIf([&](const OBJ & obj)
{
return obj.isOlderThan(msSinceEpoc);
});
}
//! Sort by timestamp
void sortLatestFirst();
template <class OBJ, class CONTAINER>
void ITimestampObjectList<OBJ, CONTAINER>::removeOlderThanNowMinusOffset(int offsetMs)
{
const qint64 epoch = QDateTime::currentMSecsSinceEpoch() - offsetMs;
this->container().removeIf([&](const OBJ & obj)
{
return obj.isOlderThan(epoch);
});
}
//! Sort by timestamp
void sortOldestFirst();
template <class OBJ, class CONTAINER>
void ITimestampObjectList<OBJ, CONTAINER>::sortLatestFirst()
{
this->container().sortOldestFirst();
std::reverse(this->container().begin(), this->container().end());
}
protected:
//! Constructor
ITimestampObjectList();
template <class OBJ, class CONTAINER>
void ITimestampObjectList<OBJ, CONTAINER>::sortOldestFirst()
{
this->container().sort(BlackMisc::Predicates::MemberLess(&OBJ::getMSecsSinceEpoch));
}
//! Container
virtual const CONTAINER &getContainer() const = 0;
template <class OBJ, class CONTAINER>
void ITimestampObjectList<OBJ, CONTAINER>::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);
}
//! Container
virtual CONTAINER &getContainer() = 0;
};
// 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<BlackMisc::Aviation::CAircraftSituation, BlackMisc::Aviation::CAircraftSituationList>;
template class ITimestampObjectList<BlackMisc::Aviation::CAircraftParts, BlackMisc::Aviation::CAircraftPartsList>;
} //namespace
#endif //guard
} // namespace