swift
atcstationlist.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QString>
7 #include <QtGlobal>
8 
12 #include "misc/predicates.h"
13 #include "misc/range.h"
14 
15 using namespace swift::misc::physical_quantities;
16 using namespace swift::misc::network;
17 
18 SWIFT_DEFINE_SEQUENCE_MIXINS(swift::misc::aviation, CAtcStation, CAtcStationList)
19 
20 namespace swift::misc::aviation
21 {
22  CAtcStationList::CAtcStationList() {}
23 
24  CAtcStationList::CAtcStationList(const CSequence<CAtcStation> &other) : CSequence<CAtcStation>(other) {}
25 
27  {
28  return this->findBy(
29  [&](const CAtcStation &atcStation) { return atcStation.isComUnitTunedToFrequency(comUnit); });
30  }
31 
33  {
34  return this->containsBy(
35  [&](const CAtcStation &atcStation) { return atcStation.isComUnitTunedToFrequency(comUnit); });
36  }
37 
39  {
40  if (frequency.isNull()) { return CAtcStationList(); }
41  return this->findBy([&](const CAtcStation &atcStation) { return atcStation.isAtcStationFrequency(frequency); });
42  }
43 
45  bool overrideWithNewer)
46  {
48 
49  // for loop just to get reference
50  bool unequal = false;
51  for (CAtcStation &station : *this)
52  {
53  if (station.getCallsign() != callsign) { continue; }
54 
55  const CInformationMessage m = station.getInformationMessage(type);
56  if (m.getType() == CInformationMessage::Unspecified) { break; }
57 
58  if (m.getMessage() == im.getMessage())
59  {
60  if (!overrideWithNewer) { break; }
61  if (!im.isNewerThan(m)) { break; }
62  }
63  else { unequal = true; }
64  station.setMessage(im);
65  break; // only count unequals
66  }
67  return unequal;
68  }
69 
70  int CAtcStationList::setOnline(const CCallsign &callsign, bool online)
71  {
72  int c = 0;
73  for (CAtcStation &station : *this)
74  {
75  if (station.getCallsign() != callsign) { continue; }
76  if (station.setOnline(online)) { c++; }
77  }
78  return c;
79  }
80 
82  {
83  return this->findBy(&CAtcStation::hasValidFrequency, true);
84  }
85 
87  {
90  }
91 
93 
95  {
96  if (this->isEmpty()) { return {}; }
97  CAtcStationList copy(*this);
98  copy.removeIfOutsideRange();
99  return copy;
100  }
101 
103  {
105  }
106 
108  {
109  CAtcStationList stations = *this;
111  return stations;
112  }
113 
115  {
116  if (this->isEmpty()) { return QHash<QString, CAtcStationList>(); }
117  const CAtcStationList stations = sort ? this->sortedByAtcSuffixSortOrderAndDistance() : *this;
118 
119  QString suffix;
121  for (const CAtcStation &s : stations)
122  {
123  const QString currentSuffix = s.getCallsignSuffix();
124  if (suffix != currentSuffix)
125  {
126  suffix = currentSuffix;
127  split[currentSuffix] = CAtcStationList();
128  }
129  split[currentSuffix].push_back(s);
130  }
131  return split;
132  }
133 } // namespace swift::misc::aviation
bool containsBy(Predicate p) const
Return true if there is an element for which a given predicate returns true.
Definition: range.h:101
auto transform(F function) const
Return a new container generated by applying some transformation function to all elements of this one...
Definition: range.h:403
void sortBy(K1 key1, Keys... keys)
In-place sort by some particular key(s).
Definition: sequence.h:576
CSequence findBy(Predicate p) const
Return a copy containing only those elements for which a given predicate returns true.
Definition: sequence.h:398
int removeIf(Predicate p)
Remove elements for which a given predicate returns true.
Definition: sequence.h:446
bool isEmpty() const
Synonym for empty.
Definition: sequence.h:285
void sort(Predicate p)
In-place sort by a given comparator predicate.
Definition: sequence.h:560
bool isNewerThan(const ITimestampBased &otherTimestampObj) const
Is this newer than other?
Value object encapsulating information about an ATC station.
Definition: atcstation.h:38
const CCallsign & getCallsign() const
Get callsign.
Definition: atcstation.h:84
bool hasValidFrequency() const
Valid COM frequency.
Definition: atcstation.h:132
bool isComUnitTunedToFrequency(const aviation::CComSystem &comUnit) const
Is Com unit tuned to this stations frequency.
Definition: atcstation.cpp:137
const CInformationMessage & getInformationMessage(CInformationMessage::InformationType type) const
Message per type.
Definition: atcstation.cpp:147
bool setOnline(bool online)
Set online.
Definition: atcstation.cpp:130
bool isAtcStationFrequency(const physical_quantities::CFrequency &frequency) const
Is passed frequency the frequency of this station.
Definition: atcstation.cpp:142
bool setMessage(const CInformationMessage &message)
Set given message.
Definition: atcstation.cpp:158
int getSuffixSortOrder() const
Callsign suffix sort order.
Definition: atcstation.cpp:56
const swift::misc::network::CUser & getController() const
Get controller.
Definition: atcstation.h:105
bool isInRange() const
In range? If range and distance to own aircraft are not available false.
Definition: atcstation.cpp:124
Value object for a list of ATC stations.
int removeIfOutsideRange()
Remove if marked outside of range.
QHash< QString, CAtcStationList > splitPerSuffix(bool sort=true) const
Split per suffix.
CAtcStationList stationsWithValidFrequency() const
Find 0..n stations with valid COM frequency.
bool hasComUnitTunedInChannelSpacing(const CComSystem &comUnit) const
Any stations tuned in frequency of COM unit (with channel spacing)
CAtcStationList sortedByAtcSuffixSortOrderAndDistance() const
Sorted by ATC suffix sort order and distance.
int setOnline(const CCallsign &callsign, bool online)
Set online status.
void sortByAtcSuffixSortOrderAndDistance()
Sort by ATC suffix sort order and distance.
CAtcStationList findIfFrequencyIsWithinSpacing(const physical_quantities::CFrequency &frequency)
Find 0..n stations with frequency (with 5 kHz spacing for .x20/.x25 and .x70/.x75)
CAtcStationList findIfComUnitTunedInChannelSpacing(const CComSystem &comUnit) const
Find 0..n stations tuned in frequency of COM unit (with channel spacing)
network::CUserList getControllers() const
All controllers (with valid data)
bool updateIfMessageChanged(const CInformationMessage &im, const CCallsign &callsign, bool overrideWithNewer)
Update if message changed.
CAtcStationList findInRange() const
Those in range.
Value object encapsulating information of a callsign.
Definition: callsign.h:30
COM system (aka "radio")
Definition: comsystem.h:37
Value object encapsulating information message (ATIS, METAR, TAF)
const QString & getMessage() const
Get message.
const physical_quantities::CLength & getRelativeDistance() const
Get the distance.
Value object encapsulating a list of voice rooms.
Definition: userlist.h:26
auto MemberTransform(T memberFunc)
Returns a function object that returns the value returned by one of it's argument member functions.
Definition: predicates.h:54
auto MemberValid(T memberFunc)
Returns a predicate that returns true if the isValid() method of the value returned from one of its m...
Definition: predicates.h:64
#define SWIFT_DEFINE_SEQUENCE_MIXINS(Namespace, T, List)
Explicit template definition of mixins for a CSequence subclass.
Definition: sequence.h:63