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(const CSequence<CAtcStation> &other) : CSequence<CAtcStation>(other) {}
23 
25  {
26  return this->findBy(
27  [&](const CAtcStation &atcStation) { return atcStation.isComUnitTunedToFrequency(comUnit); });
28  }
29 
31  {
32  return this->containsBy(
33  [&](const CAtcStation &atcStation) { return atcStation.isComUnitTunedToFrequency(comUnit); });
34  }
35 
37  {
38  if (frequency.isNull()) { return {}; }
39  return this->findBy([&](const CAtcStation &atcStation) { return atcStation.isAtcStationFrequency(frequency); });
40  }
41 
43  bool overrideWithNewer)
44  {
46 
47  // for loop just to get reference
48  bool unequal = false;
49  for (CAtcStation &station : *this)
50  {
51  if (station.getCallsign() != callsign) { continue; }
52 
53  const CInformationMessage m = station.getInformationMessage(type);
54  if (m.getType() == CInformationMessage::Unspecified) { break; }
55 
56  if (m.getMessage() == im.getMessage())
57  {
58  if (!overrideWithNewer) { break; }
59  if (!im.isNewerThan(m)) { break; }
60  }
61  else { unequal = true; }
62  station.setMessage(im);
63  break; // only count unequals
64  }
65  return unequal;
66  }
67 
68  int CAtcStationList::setOnline(const CCallsign &callsign, bool online)
69  {
70  int c = 0;
71  for (CAtcStation &station : *this)
72  {
73  if (station.getCallsign() != callsign) { continue; }
74  if (station.setOnline(online)) { c++; }
75  }
76  return c;
77  }
78 
80  {
81  return this->findBy(&CAtcStation::hasValidFrequency, true);
82  }
83 
85  {
88  }
89 
91 
93  {
94  if (this->isEmpty()) { return {}; }
95  CAtcStationList copy(*this);
96  copy.removeIfOutsideRange();
97  return copy;
98  }
99 
101  {
103  }
104 
106  {
107  CAtcStationList stations = *this;
109  return stations;
110  }
111 
113  {
114  if (this->isEmpty()) { return {}; }
115  const CAtcStationList stations = sort ? this->sortedByAtcSuffixSortOrderAndDistance() : *this;
116 
117  QString suffix;
119  for (const CAtcStation &s : stations)
120  {
121  const QString currentSuffix = s.getCallsignSuffix();
122  if (suffix != currentSuffix)
123  {
124  suffix = currentSuffix;
125  split[currentSuffix] = CAtcStationList();
126  }
127  split[currentSuffix].push_back(s);
128  }
129  return split;
130  }
131 } // 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:135
const CInformationMessage & getInformationMessage(CInformationMessage::InformationType type) const
Message per type.
Definition: atcstation.cpp:145
bool setOnline(bool online)
Set online.
Definition: atcstation.cpp:128
bool isAtcStationFrequency(const physical_quantities::CFrequency &frequency) const
Is passed frequency the frequency of this station.
Definition: atcstation.cpp:140
bool setMessage(const CInformationMessage &message)
Set given message.
Definition: atcstation.cpp:156
int getSuffixSortOrder() const
Callsign suffix sort order.
Definition: atcstation.cpp:54
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:122
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()=default
Default constructor.
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