mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-23 07:15:35 +08:00
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:
@@ -23,11 +23,22 @@ namespace BlackMisc
|
||||
: m_callsign(callsign), m_pilot(user), m_situation(situation)
|
||||
{
|
||||
// sync callsigns
|
||||
if (!this->m_pilot.hasValidCallsign() && !callsign.isEmpty())
|
||||
if (!callsign.isEmpty())
|
||||
{
|
||||
this->m_pilot.setCallsign(callsign);
|
||||
this->m_situation.setCallsign(callsign);
|
||||
this->setCallsign(callsign);
|
||||
}
|
||||
else if (!user.getCallsign().isEmpty())
|
||||
{
|
||||
this->setCallsign(user.getCallsign());
|
||||
}
|
||||
}
|
||||
|
||||
void CAircraft::setCallsign(const CCallsign &callsign)
|
||||
{
|
||||
this->m_callsign = callsign;
|
||||
this->m_pilot.setCallsign(callsign);
|
||||
this->m_situation.setCallsign(callsign);
|
||||
this->m_parts.setCallsign(callsign);
|
||||
}
|
||||
|
||||
QString CAircraft::convertToQString(bool i18n) const
|
||||
@@ -139,6 +150,12 @@ namespace BlackMisc
|
||||
this->setTransponder(xpdr);
|
||||
}
|
||||
|
||||
void CAircraft::setParts(const CAircraftParts &parts)
|
||||
{
|
||||
m_parts = parts;
|
||||
m_parts.setCallsign(this->getCallsign());
|
||||
}
|
||||
|
||||
CVariant CAircraft::propertyByIndex(const BlackMisc::CPropertyIndex &index) const
|
||||
{
|
||||
if (index.isMyself()) { return this->toCVariant(); }
|
||||
|
||||
@@ -63,7 +63,7 @@ namespace BlackMisc
|
||||
QString getCallsignAsString() const { return m_callsign.asString(); }
|
||||
|
||||
//! Set callsign
|
||||
virtual void setCallsign(const CCallsign &callsign) { this->m_callsign = callsign; this->m_pilot.setCallsign(callsign); }
|
||||
virtual void setCallsign(const CCallsign &callsign);
|
||||
|
||||
//! Get situation.
|
||||
const CAircraftSituation &getSituation() const { return m_situation; }
|
||||
@@ -245,7 +245,7 @@ namespace BlackMisc
|
||||
const BlackMisc::Aviation::CAircraftParts &getParts() const { return m_parts; }
|
||||
|
||||
//! Set aircraft parts
|
||||
void setParts(const BlackMisc::Aviation::CAircraftParts &parts) { m_parts = parts; }
|
||||
void setParts(const BlackMisc::Aviation::CAircraftParts &parts);
|
||||
|
||||
//! \copydoc CValueObject::propertyByIndex
|
||||
virtual CVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const override;
|
||||
@@ -281,7 +281,7 @@ BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::Aviation::CAircraft, (
|
||||
o.m_transponder,
|
||||
o.m_parts,
|
||||
o.m_icao,
|
||||
o.m_distanceToPlane,
|
||||
o.m_distanceToOwnAircraft,
|
||||
o.m_bearingToOwnAircraft))
|
||||
|
||||
Q_DECLARE_METATYPE(BlackMisc::Aviation::CAircraft)
|
||||
|
||||
@@ -20,9 +20,6 @@ namespace BlackMisc
|
||||
{
|
||||
namespace Aviation
|
||||
{
|
||||
/*
|
||||
* Convert to string
|
||||
*/
|
||||
QString CAircraftSituation::convertToQString(bool i18n) const
|
||||
{
|
||||
QString s(this->m_position.toQString(i18n));
|
||||
@@ -35,9 +32,6 @@ namespace BlackMisc
|
||||
return s;
|
||||
}
|
||||
|
||||
/*
|
||||
* Property by index
|
||||
*/
|
||||
CVariant CAircraftSituation::propertyByIndex(const BlackMisc::CPropertyIndex &index) const
|
||||
{
|
||||
if (index.isMyself()) { return this->toCVariant(); }
|
||||
@@ -68,17 +62,10 @@ namespace BlackMisc
|
||||
case IndexCallsign:
|
||||
return this->m_correspondingCallsign.propertyByIndex(index.copyFrontRemoved());
|
||||
default:
|
||||
break;
|
||||
return CValueObject::propertyByIndex(index);
|
||||
}
|
||||
|
||||
Q_ASSERT_X(false, "CAircraftSituation", "index unknown");
|
||||
QString m = QString("no property, index ").append(index.toQString());
|
||||
return CVariant::fromValue(m);
|
||||
}
|
||||
|
||||
/*
|
||||
* Property by index
|
||||
*/
|
||||
void CAircraftSituation::setPropertyByIndex(const CVariant &variant, const BlackMisc::CPropertyIndex &index)
|
||||
{
|
||||
if (index.isMyself())
|
||||
@@ -86,6 +73,11 @@ namespace BlackMisc
|
||||
this->convertFromCVariant(variant);
|
||||
return;
|
||||
}
|
||||
if (ITimestampBased::canHandleIndex(index))
|
||||
{
|
||||
ITimestampBased::setPropertyByIndex(variant, index);
|
||||
return;
|
||||
}
|
||||
|
||||
ColumnIndex i = index.frontCasted<ColumnIndex>();
|
||||
switch (i)
|
||||
@@ -109,7 +101,7 @@ namespace BlackMisc
|
||||
this->m_correspondingCallsign.setPropertyByIndex(variant, index.copyFrontRemoved());
|
||||
break;
|
||||
default:
|
||||
Q_ASSERT_X(false, "CAircraftSituation", "index unknown (setter)");
|
||||
CValueObject::setPropertyByIndex(variant, index);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,7 +154,7 @@ namespace BlackMisc
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::Aviation::CAircraftSituation, (o.m_correspondingCallsign, o.m_position, o.m_altitude, o.m_heading, o.m_pitch, o.m_bank, o.m_groundspeed))
|
||||
BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::Aviation::CAircraftSituation, (o.m_correspondingCallsign, o.m_position, o.m_altitude, o.m_heading, o.m_pitch, o.m_bank, o.m_groundspeed, o.m_timestampMSecsSinceEpoch))
|
||||
Q_DECLARE_METATYPE(BlackMisc::Aviation::CAircraftSituation)
|
||||
|
||||
#endif // guard
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "blackmisc/avatcstationlist.h"
|
||||
#include "blackmisc/avaircraftlist.h"
|
||||
#include "blackmisc/avaircraftsituationlist.h"
|
||||
#include "blackmisc/aviation/aircraftpartslist.h"
|
||||
#include "blackmisc/nwclientlist.h"
|
||||
#include "blackmisc/simulation/simulatedaircraftlist.h"
|
||||
|
||||
@@ -130,6 +131,7 @@ namespace BlackMisc
|
||||
template class ICallsignObjectList<BlackMisc::Aviation::CAtcStation, BlackMisc::Aviation::CAtcStationList>;
|
||||
template class ICallsignObjectList<BlackMisc::Aviation::CAircraft, BlackMisc::Aviation::CAircraftList>;
|
||||
template class ICallsignObjectList<BlackMisc::Aviation::CAircraftSituation, BlackMisc::Aviation::CAircraftSituationList>;
|
||||
template class ICallsignObjectList<BlackMisc::Aviation::CAircraftParts, BlackMisc::Aviation::CAircraftPartsList>;
|
||||
template class ICallsignObjectList<BlackMisc::Simulation::CSimulatedAircraft, BlackMisc::Simulation::CSimulatedAircraftList>;
|
||||
template class ICallsignObjectList<BlackMisc::Network::CClient, BlackMisc::Network::CClientList>;
|
||||
|
||||
|
||||
@@ -8,6 +8,9 @@
|
||||
*/
|
||||
|
||||
#include "aircraftlights.h"
|
||||
#include "blackmisc/variant.h"
|
||||
|
||||
using namespace BlackMisc;
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
@@ -15,8 +18,7 @@ namespace BlackMisc
|
||||
{
|
||||
CAircraftLights::CAircraftLights(bool strobeOn, bool landingOn, bool taxiOn, bool beaconOn, bool navOn, bool logoOn)
|
||||
: m_strobeOn(strobeOn), m_landingOn(landingOn), m_taxiOn(taxiOn), m_beaconOn(beaconOn), m_navOn(navOn), m_logoOn(logoOn)
|
||||
{
|
||||
}
|
||||
{ }
|
||||
|
||||
CAircraftLights CAircraftLights::allLightsOn()
|
||||
{
|
||||
@@ -45,5 +47,65 @@ namespace BlackMisc
|
||||
s += m_logoOn;
|
||||
return s;
|
||||
}
|
||||
|
||||
CVariant CAircraftLights::propertyByIndex(const BlackMisc::CPropertyIndex &index) const
|
||||
{
|
||||
if (index.isMyself()) { return this->toCVariant(); }
|
||||
|
||||
ColumnIndex i = index.frontCasted<ColumnIndex>();
|
||||
switch (i)
|
||||
{
|
||||
case IndexBeacon:
|
||||
return CVariant::from(m_beaconOn);
|
||||
case IndexLanding:
|
||||
return CVariant::from(m_landingOn);
|
||||
case IndexLogo:
|
||||
return CVariant::from(m_logoOn);
|
||||
case IndexNav:
|
||||
return CVariant::from(m_navOn);
|
||||
case IndexStrobe:
|
||||
return CVariant::from(m_strobeOn);
|
||||
case IndexTaxi:
|
||||
return CVariant::from(m_taxiOn);
|
||||
default:
|
||||
return CValueObject::propertyByIndex(index);
|
||||
}
|
||||
}
|
||||
|
||||
void CAircraftLights::setPropertyByIndex(const CVariant &variant, const BlackMisc::CPropertyIndex &index)
|
||||
{
|
||||
if (index.isMyself())
|
||||
{
|
||||
this->convertFromCVariant(variant);
|
||||
return;
|
||||
}
|
||||
|
||||
ColumnIndex i = index.frontCasted<ColumnIndex>();
|
||||
switch (i)
|
||||
{
|
||||
case IndexBeacon:
|
||||
this->m_beaconOn = variant.toBool();
|
||||
break;
|
||||
case IndexLanding:
|
||||
this->m_landingOn = variant.toBool();
|
||||
break;
|
||||
case IndexLogo:
|
||||
this->m_logoOn = variant.toBool();
|
||||
break;
|
||||
case IndexNav:
|
||||
this->m_navOn = variant.toBool();
|
||||
break;
|
||||
case IndexStrobe:
|
||||
this->m_strobeOn = variant.toBool();
|
||||
break;
|
||||
case IndexTaxi:
|
||||
this->m_taxiOn = variant.toBool();
|
||||
break;
|
||||
default:
|
||||
CValueObject::setPropertyByIndex(variant, index);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#define BLACKMISC_AIRCRAFTLIGHTS_H
|
||||
|
||||
#include "blackmisc/valueobject.h"
|
||||
#include "blackmisc/propertyindex.h"
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
@@ -22,6 +23,17 @@ namespace BlackMisc
|
||||
class CAircraftLights : public CValueObjectStdTuple<CAircraftLights>
|
||||
{
|
||||
public:
|
||||
//! Properties by index
|
||||
enum ColumnIndex
|
||||
{
|
||||
IndexStrobe = BlackMisc::CPropertyIndex::GlobalIndexCAircraftLights,
|
||||
IndexLanding,
|
||||
IndexTaxi,
|
||||
IndexBeacon,
|
||||
IndexNav,
|
||||
IndexLogo
|
||||
};
|
||||
|
||||
|
||||
//! Default constructor
|
||||
CAircraftLights() = default;
|
||||
@@ -29,6 +41,12 @@ namespace BlackMisc
|
||||
//! Constructor
|
||||
CAircraftLights(bool strobeOn, bool landingOn, bool taxiOn, bool beaconOn, bool navOn, bool logoOn);
|
||||
|
||||
//! \copydoc CValueObject::propertyByIndex
|
||||
virtual CVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const override;
|
||||
|
||||
//! \copydoc CValueObject::setPropertyByIndex
|
||||
virtual void setPropertyByIndex(const CVariant &variant, const BlackMisc::CPropertyIndex &index) override;
|
||||
|
||||
//! Strobes lights on?
|
||||
bool isStrobeOn() const { return m_strobeOn; }
|
||||
|
||||
@@ -99,4 +117,4 @@ BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::Aviation::CAircraftLights, (
|
||||
|
||||
Q_DECLARE_METATYPE(BlackMisc::Aviation::CAircraftLights)
|
||||
|
||||
#endif // BLACKMISC_AIRCRAFTLIGHTS_H
|
||||
#endif // guard
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace BlackMisc
|
||||
s += " gear down: ";
|
||||
s += m_gearDown;
|
||||
s += " flaps pct: ";
|
||||
s += m_flapsPercent;
|
||||
s += m_flapsPercentage;
|
||||
s += " spoilers out: ";
|
||||
s += m_spoilersOut;
|
||||
s += " engines on: ";
|
||||
@@ -29,5 +29,74 @@ namespace BlackMisc
|
||||
s += m_isOnGround;
|
||||
return s;
|
||||
}
|
||||
|
||||
CVariant CAircraftParts::propertyByIndex(const BlackMisc::CPropertyIndex &index) const
|
||||
{
|
||||
if (index.isMyself()) { return this->toCVariant(); }
|
||||
if (ITimestampBased::canHandleIndex(index))
|
||||
{
|
||||
return ITimestampBased::propertyByIndex(index);
|
||||
}
|
||||
|
||||
ColumnIndex i = index.frontCasted<ColumnIndex>();
|
||||
switch (i)
|
||||
{
|
||||
case IndexCallsign:
|
||||
return m_correspondingCallsign.propertyByIndex(index.copyFrontRemoved());
|
||||
case IndexEngines:
|
||||
return this->m_engines.toCVariant();
|
||||
case IndexFlapsPercentage:
|
||||
return CVariant::fromValue(this->m_flapsPercentage);
|
||||
case IndexGearDown:
|
||||
return CVariant::fromValue(this->m_gearDown);
|
||||
case IndexLights:
|
||||
return this->m_lights.propertyByIndex(index.copyFrontRemoved());
|
||||
case IndexSpoilersOut:
|
||||
return CVariant::fromValue(this->m_spoilersOut);
|
||||
default:
|
||||
return CValueObject::propertyByIndex(index);
|
||||
}
|
||||
}
|
||||
|
||||
void CAircraftParts::setPropertyByIndex(const CVariant &variant, const BlackMisc::CPropertyIndex &index)
|
||||
{
|
||||
if (index.isMyself())
|
||||
{
|
||||
this->convertFromCVariant(variant);
|
||||
return;
|
||||
}
|
||||
if (ITimestampBased::canHandleIndex(index))
|
||||
{
|
||||
ITimestampBased::setPropertyByIndex(variant, index);
|
||||
return;
|
||||
}
|
||||
|
||||
ColumnIndex i = index.frontCasted<ColumnIndex>();
|
||||
switch (i)
|
||||
{
|
||||
case IndexEngines:
|
||||
this->m_engines.setPropertyByIndex(variant, index.copyFrontRemoved());
|
||||
break;
|
||||
case IndexFlapsPercentage:
|
||||
this->m_flapsPercentage = variant.toInt();
|
||||
break;
|
||||
case IndexGearDown:
|
||||
this->m_gearDown = variant.toBool();
|
||||
break;
|
||||
case IndexLights:
|
||||
this->m_lights.setPropertyByIndex(variant, index.copyFrontRemoved());
|
||||
break;
|
||||
case IndexSpoilersOut:
|
||||
this->m_spoilersOut = variant.toBool();
|
||||
break;
|
||||
case IndexCallsign:
|
||||
this->m_correspondingCallsign.setPropertyByIndex(variant, index.copyFrontRemoved());
|
||||
break;
|
||||
default:
|
||||
CValueObject::setPropertyByIndex(variant, index);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
@@ -19,22 +19,6 @@ namespace BlackMisc
|
||||
CSequence<CAircraftParts>(other)
|
||||
{ }
|
||||
|
||||
CAircraftPartsList CAircraftPartsList::findBefore(const QDateTime &dateTime) const
|
||||
{
|
||||
return findBy([&](const CAircraftParts & parts)
|
||||
{
|
||||
return parts.getTimestamp() < dateTime;
|
||||
});
|
||||
}
|
||||
|
||||
void CAircraftPartsList::removeBefore(const QDateTime &dateTime)
|
||||
{
|
||||
removeIf([&](const CAircraftParts &parts)
|
||||
{
|
||||
return parts.getTimestamp() < dateTime;
|
||||
});
|
||||
}
|
||||
|
||||
void CAircraftPartsList::registerMetadata()
|
||||
{
|
||||
qRegisterMetaType<BlackMisc::CSequence<CAircraftParts>>();
|
||||
|
||||
@@ -13,8 +13,8 @@
|
||||
#define BLACKMISC_AIRCRAFTPARTSLIST_H
|
||||
|
||||
#include "aircraftparts.h"
|
||||
#include "blackmisc/collection.h"
|
||||
#include "blackmisc/sequence.h"
|
||||
#include "blackmisc/timestampobjectlist.h"
|
||||
#include "blackmisc/avcallsignobjectlist.h"
|
||||
#include <QDateTime>
|
||||
|
||||
namespace BlackMisc
|
||||
@@ -22,7 +22,10 @@ namespace BlackMisc
|
||||
namespace Aviation
|
||||
{
|
||||
//! Value object encapsulating a list of aircraft parts.
|
||||
class CAircraftPartsList : public CSequence<CAircraftParts>
|
||||
class CAircraftPartsList :
|
||||
public CSequence<CAircraftParts>,
|
||||
public ITimestampObjectList<CAircraftParts, CAircraftPartsList>,
|
||||
public ICallsignObjectList<CAircraftParts, CAircraftPartsList>
|
||||
{
|
||||
public:
|
||||
//! Default constructor.
|
||||
@@ -31,12 +34,6 @@ namespace BlackMisc
|
||||
//! Construct from a base class object.
|
||||
CAircraftPartsList(const CSequence<CAircraftParts> &other);
|
||||
|
||||
//! Get a list of situations before dateTime
|
||||
CAircraftPartsList findBefore (const QDateTime& dateTime) const;
|
||||
|
||||
//! Remove parts with timestamp before dateTime
|
||||
void removeBefore(const QDateTime& dateTime);
|
||||
|
||||
//! \copydoc CValueObject::toQVariant
|
||||
virtual QVariant toQVariant() const override { return QVariant::fromValue(*this); }
|
||||
|
||||
@@ -45,6 +42,14 @@ namespace BlackMisc
|
||||
|
||||
//! Register metadata
|
||||
static void registerMetadata();
|
||||
|
||||
protected:
|
||||
//! Myself
|
||||
virtual const CAircraftPartsList &getContainer() const { return *this; }
|
||||
|
||||
//! Myself
|
||||
virtual CAircraftPartsList &getContainer() { return *this; }
|
||||
|
||||
};
|
||||
|
||||
} //namespace
|
||||
|
||||
@@ -44,6 +44,8 @@ namespace BlackMisc
|
||||
GlobalIndexCAircraftSituation = 1300,
|
||||
GlobalIndexCAtcStation = 1400,
|
||||
GlobalIndexCAirport = 1500,
|
||||
GlobalIndexCAircraftParts = 1600,
|
||||
GlobalIndexCAircraftLights = 1700,
|
||||
GlobalIndexCModulator = 2000,
|
||||
GlobalIndexCTransponder = 2100,
|
||||
GlobalIndexICoordinateGeodetic = 3000,
|
||||
|
||||
@@ -67,7 +67,7 @@ namespace BlackMisc
|
||||
virtual void setIcaoInfo(const BlackMisc::Aviation::CAircraftIcao &icao) override;
|
||||
|
||||
//! \copydoc CAircraft::setPilot
|
||||
virtual void setPilot(const BlackMisc::Network::CUser &user);
|
||||
virtual void setPilot(const BlackMisc::Network::CUser &user) override;
|
||||
|
||||
//! Get client
|
||||
const BlackMisc::Network::CClient &getClient() const { return m_client; }
|
||||
|
||||
@@ -23,12 +23,18 @@ namespace BlackMisc
|
||||
template <class OBJ, class CONTAINER>
|
||||
CONTAINER ITimestampObjectList<OBJ, CONTAINER>::findBefore(qint64 msSinceEpoch) const
|
||||
{
|
||||
return this->getContainer().findBy([&](const OBJ & obj)
|
||||
return this->container().findBy([&](const OBJ & obj)
|
||||
{
|
||||
return obj.isOlderThan(msSinceEpoch);
|
||||
});
|
||||
}
|
||||
|
||||
template <class OBJ, class CONTAINER>
|
||||
CONTAINER ITimestampObjectList<OBJ, CONTAINER>::findBeforeNowMinusOffset(qint64 msOffset) const
|
||||
{
|
||||
return this->findBefore(QDateTime::currentMSecsSinceEpoch() - msOffset);
|
||||
}
|
||||
|
||||
template <class OBJ, class CONTAINER>
|
||||
CONTAINER ITimestampObjectList<OBJ, CONTAINER>::findBefore(const QDateTime &dateTime) const
|
||||
{
|
||||
@@ -38,7 +44,7 @@ namespace BlackMisc
|
||||
template <class OBJ, class CONTAINER>
|
||||
CONTAINER ITimestampObjectList<OBJ, CONTAINER>::findAfter(qint64 msSinceEpoc) const
|
||||
{
|
||||
return this->getContainer().findBy([&](const OBJ & obj)
|
||||
return this->container().findBy([&](const OBJ & obj)
|
||||
{
|
||||
return obj.isNewerThan(msSinceEpoc);
|
||||
});
|
||||
@@ -47,7 +53,7 @@ namespace BlackMisc
|
||||
template <class OBJ, class CONTAINER>
|
||||
QList<CONTAINER> ITimestampObjectList<OBJ, CONTAINER>::splitByTime(qint64 msSinceEpoch) const
|
||||
{
|
||||
CONTAINER newer(this->getContainer());
|
||||
CONTAINER newer(this->container());
|
||||
newer.sortLatestFirst();
|
||||
CONTAINER older;
|
||||
for (auto it = newer.begin(); it != newer.end(); ++it)
|
||||
@@ -55,19 +61,21 @@ namespace BlackMisc
|
||||
if (it->isOlderThan(msSinceEpoch))
|
||||
{
|
||||
// better "move", ?? std::make_move_iterator
|
||||
older.insert(CRange<typename CONTAINER::iterator>(it, newer.end()));
|
||||
older.insert(CRange<CONTAINER::iterator>(it, newer.end()));
|
||||
newer.erase(it, newer.end());
|
||||
break;
|
||||
}
|
||||
}
|
||||
return QList<CONTAINER>({older, newer});
|
||||
|
||||
// before / after
|
||||
return QList<CONTAINER>({newer, older});
|
||||
}
|
||||
|
||||
template <class OBJ, class CONTAINER>
|
||||
OBJ ITimestampObjectList<OBJ, CONTAINER>::latestValue() const
|
||||
{
|
||||
if (this->getContainer().isEmpty()) { return OBJ(); }
|
||||
CONTAINER container(getContainer()); // copy
|
||||
if (this->container().isEmpty()) { return OBJ(); }
|
||||
CONTAINER container(container()); // copy
|
||||
container.sortLatestFirst();
|
||||
return container.front();
|
||||
}
|
||||
@@ -75,8 +83,8 @@ namespace BlackMisc
|
||||
template <class OBJ, class CONTAINER>
|
||||
OBJ ITimestampObjectList<OBJ, CONTAINER>::oldestValue() const
|
||||
{
|
||||
if (this->getContainer().isEmpty()) { return OBJ(); }
|
||||
CONTAINER container(getContainer()); // copy
|
||||
if (this->container().isEmpty()) { return OBJ(); }
|
||||
CONTAINER container(container()); // copy
|
||||
container.sortLatestFirst();
|
||||
return container.back();
|
||||
}
|
||||
@@ -96,7 +104,7 @@ namespace BlackMisc
|
||||
template <class OBJ, class CONTAINER>
|
||||
void ITimestampObjectList<OBJ, CONTAINER>::removeBefore(qint64 msSinceEpoc)
|
||||
{
|
||||
this->getContainer().removeIf([&](const OBJ & obj)
|
||||
this->container().removeIf([&](const OBJ & obj)
|
||||
{
|
||||
return obj.isOlderThan(msSinceEpoc);
|
||||
});
|
||||
@@ -106,7 +114,7 @@ namespace BlackMisc
|
||||
void ITimestampObjectList<OBJ, CONTAINER>::removeOlderThanNowMinusOffset(int offsetMs)
|
||||
{
|
||||
const qint64 epoch = QDateTime::currentMSecsSinceEpoch() - offsetMs;
|
||||
this->getContainer().removeIf([&](const OBJ & obj)
|
||||
this->container().removeIf([&](const OBJ & obj)
|
||||
{
|
||||
return obj.isOlderThan(epoch);
|
||||
});
|
||||
@@ -115,18 +123,30 @@ namespace BlackMisc
|
||||
template <class OBJ, class CONTAINER>
|
||||
void ITimestampObjectList<OBJ, CONTAINER>::sortLatestFirst()
|
||||
{
|
||||
this->getContainer().sortOldestFirst();
|
||||
std::reverse(this->getContainer().begin(), this->getContainer().end());
|
||||
this->container().sortOldestFirst();
|
||||
std::reverse(this->container().begin(), this->container().end());
|
||||
}
|
||||
|
||||
template <class OBJ, class CONTAINER>
|
||||
void ITimestampObjectList<OBJ, CONTAINER>::sortOldestFirst()
|
||||
{
|
||||
this->getContainer().sort(BlackMisc::Predicates::MemberLess(&OBJ::getMSecsSinceEpoch));
|
||||
this->container().sort(BlackMisc::Predicates::MemberLess(&OBJ::getMSecsSinceEpoch));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user