Skip to content

Commit d5f722c

Browse files
author
Andy Ford
authored
Merge pull request #11 from ECFMP/api-element-refactor
Api element refactor
2 parents 4202e00 + 768ab55 commit d5f722c

30 files changed

Lines changed: 425 additions & 482 deletions

include/ECFMP/ECFMP.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

33
#include "ECFMP/api/ApiElementCollection.h"
4-
#include "ECFMP/api/EventCollection.h"
4+
#include "ECFMP/api/StringIdentifierApiElementCollection.h"
55
#include "ECFMP/event/Event.h"
66
#include "ECFMP/event/EventParticipant.h"
77
#include "ECFMP/eventbus/EventBus.h"
Lines changed: 73 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,97 @@
11
#pragma once
22
#include "ApiElementIterator.h"
3-
#include "Iterable.h"
43

54
namespace ECFMP::Api {
65

76
/**
87
* A collection of elements from the API.
98
*/
109
template<typename T>
11-
class ApiElementCollection : Plugin::Iterable<T>
10+
class ApiElementCollection
1211
{
1312
public:
1413
virtual ~ApiElementCollection() = default;
1514

15+
/**
16+
* Gets the first item matching some predicate.
17+
*/
18+
[[nodiscard]] auto First(const std::function<bool(const T&)>& predicate) const noexcept
19+
-> std::shared_ptr<const T>
20+
{
21+
const auto lockGuard = std::lock_guard(lock);
22+
auto it = std::find_if(elements.cbegin(), elements.cend(), [&predicate](const auto& item) {
23+
return predicate(*item.second);
24+
});
25+
return it == elements.end() ? nullptr : it->second;
26+
};
27+
1628
/**
1729
* Gets an item from the collection by its API id.
1830
*/
19-
[[nodiscard]] virtual auto Get(int id) const noexcept -> std::shared_ptr<const T> = 0;
31+
[[nodiscard]] auto Get(int id) const noexcept -> std::shared_ptr<const T>
32+
{
33+
const auto lockGuard = std::lock_guard(lock);
34+
auto it = elements.find(id);
35+
return it == elements.end() ? nullptr : it->second;
36+
};
2037

2138
/**
22-
* Iterators.
39+
* Gets the number of elements in the collection.
2340
*/
24-
[[nodiscard]] virtual auto begin() const -> ApiElementIterator<T> override = 0;
25-
[[nodiscard]] virtual auto end() const -> ApiElementIterator<T> override = 0;
26-
[[nodiscard]] virtual auto cbegin() const -> ApiElementIterator<T> override = 0;
27-
[[nodiscard]] virtual auto cend() const -> ApiElementIterator<T> override = 0;
28-
};
41+
[[nodiscard]] auto Count() const noexcept -> size_t
42+
{
43+
const auto lockGuard = std::lock_guard(lock);
44+
return elements.size();
45+
};
46+
47+
/**
48+
* Checks if the collection contains an element with the given id.
49+
*/
50+
[[nodiscard]] auto Contains(int id) const noexcept -> bool
51+
{
52+
const auto lockGuard = std::lock_guard(lock);
53+
return elements.contains(id);
54+
};
2955

30-
template<typename T>
31-
class StringIdentifiedApiElementCollection : public ApiElementCollection<T>
32-
{
33-
public:
3456
/**
35-
* Gets an item from the collection by its identifier, e.g. for London FIR, "EGTT"
57+
* Checks if the collection contains an element that satisfies the given predicate.
58+
*/
59+
[[nodiscard]] auto Contains(const std::function<bool(const T&)>& predicate) const noexcept -> bool
60+
{
61+
const auto lockGuard = std::lock_guard(lock);
62+
return std::any_of(elements.cbegin(), elements.cend(), [&predicate](const auto& item) {
63+
return predicate(*item.second);
64+
});
65+
}
66+
67+
/**
68+
* Iterators.
3669
*/
37-
[[nodiscard]] virtual auto GetByIdentifier(const std::string& identifier) const noexcept
38-
-> std::shared_ptr<const T> = 0;
70+
auto begin() const -> ApiElementIterator<T>
71+
{
72+
return ApiElementIterator<T>(lock, elements.begin());
73+
}
74+
75+
auto end() const -> ApiElementIterator<T>
76+
{
77+
return ApiElementIterator<T>(lock, elements.end());
78+
}
79+
80+
auto cbegin() const -> ApiElementIterator<T>
81+
{
82+
return ApiElementIterator<T>(lock, elements.cbegin());
83+
}
84+
85+
auto cend() const -> ApiElementIterator<T>
86+
{
87+
return ApiElementIterator<T>(lock, elements.cend());
88+
}
89+
90+
protected:
91+
// The elements
92+
std::unordered_map<int, std::shared_ptr<const T>> elements;
93+
94+
// A mutex for locking
95+
mutable std::recursive_mutex lock;
3996
};
4097
}// namespace ECFMP::Api

include/ECFMP/api/ApiElementIterator.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ namespace ECFMP::Api {
88
{
99
public:
1010
ApiElementIterator(
11-
std::recursive_mutex& lockingMutex, typename std::map<int, std::shared_ptr<T>>::const_iterator current
11+
std::recursive_mutex& lockingMutex,
12+
typename std::unordered_map<int, std::shared_ptr<const T>>::const_iterator current
1213
);
1314
ApiElementIterator(const ApiElementIterator& old);
1415
~ApiElementIterator();
@@ -18,8 +19,8 @@ namespace ECFMP::Api {
1819
using pointer = T*; // or also value_type*
1920
using reference = T&;// or also value_type&
2021

21-
auto operator*() const -> T&;
22-
auto operator->() const -> T*;
22+
auto operator*() const -> const T&;
23+
auto operator->() const -> const T*;
2324
auto operator++() -> ApiElementIterator<T>&;
2425
auto operator++(int) -> ApiElementIterator<T>;
2526
auto operator==(const ApiElementIterator<T>& compare) const -> bool;
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
#pragma once
22
#include "ECFMP/api/ApiElementCollection.h"
3+
#include "ECFMP/api/StringIdentifierApiElementCollection.h"
4+
#include "ECFMP/event/Event.h"
35
#include "ECFMP/flightinformationregion/FlightInformationRegion.h"
6+
#include "ECFMP/flowmeasure/FlowMeasure.h"
47

58
namespace ECFMP::Api {
6-
typedef ECFMP::Api::StringIdentifiedApiElementCollection<ECFMP::FlightInformationRegion::FlightInformationRegion>
9+
10+
typedef ECFMP::Api::StringIdentifierApiElementCollection<ECFMP::FlightInformationRegion::FlightInformationRegion>
711
FlightInformationRegionCollection;
12+
13+
typedef ECFMP::Api::ApiElementCollection<ECFMP::Event::Event> EventCollection;
14+
15+
typedef ECFMP::Api::StringIdentifierApiElementCollection<ECFMP::FlowMeasure::FlowMeasure> FlowMeasureCollection;
816
}// namespace ECFMP::Api

include/ECFMP/api/EventCollection.h

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
#include "ECFMP/api/ApiElementCollection.h"
3+
4+
namespace ECFMP::Api {
5+
6+
template<typename T>
7+
class StringIdentifierApiElementCollection : public ApiElementCollection<T>
8+
{
9+
public:
10+
[[nodiscard]] auto ContainsByIdentifier(const std::string& identifier) const -> bool
11+
{
12+
return this->Contains([&identifier](const auto& element) {
13+
return element.Identifier() == identifier;
14+
});
15+
}
16+
17+
[[nodiscard]] auto FirstByIdentifier(const std::string& identifier) const -> std::shared_ptr<const T>
18+
{
19+
return this->First([&identifier](const auto& element) {
20+
return element.Identifier() == identifier;
21+
});
22+
}
23+
};
24+
}// namespace ECFMP::Api

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ SET(PROJECT_NAME flow_plugin_sdk_src)
44

55
set(CMAKE_CXX_STANDARD 20)
66

7-
set(src_api api/ApiDataDownloader.cpp api/ApiDataDownloader.h api/ApiDataScheduler.cpp api/ApiDataScheduler.h api/FlightInformationRegionDataParser.cpp api/FlightInformationRegionDataParser.h api/InternalApiElementCollection.h api/InternalStringIdentifiedApiElementCollection.h api/ConcreteApiElementCollection.cpp api/ConcreteApiElementCollection.h api/ApiElementIterator.cpp api/ConcreteStringIdentifiedApiElementCollection.cpp api/ConcreteStringIdentifiedApiElementCollection.h api/InternalElementCollectionTypes.h api/EventDataParser.cpp api/EventDataParser.h api/FlowMeasureDataParser.cpp api/FlowMeasureDataParser.h api/FlowMeasureMeasureParserInterface.h api/FlowMeasureFilterParser.cpp api/FlowMeasureFilterParser.h api/ApiDataParser.cpp api/ApiDataParser.h api/ApiDataDownloadedEvent.h api/FlightInformationRegionDataParserInterface.h api/EventDataParserInterface.h api/FlowMeasureDataParserInterface.h)
7+
set(src_api api/ApiDataDownloader.cpp api/ApiDataDownloader.h api/ApiDataScheduler.cpp api/ApiDataScheduler.h api/FlightInformationRegionDataParser.cpp api/FlightInformationRegionDataParser.h api/InternalApiElementCollection.h api/InternalApiElementCollection.h api/ApiElementIterator.cpp api/InternalElementCollectionTypes.h api/EventDataParser.cpp api/EventDataParser.h api/FlowMeasureDataParser.cpp api/FlowMeasureDataParser.h api/FlowMeasureMeasureParserInterface.h api/FlowMeasureFilterParser.cpp api/FlowMeasureFilterParser.h api/ApiDataParser.cpp api/ApiDataParser.h api/ApiDataDownloadedEvent.h api/FlightInformationRegionDataParserInterface.h api/EventDataParserInterface.h api/FlowMeasureDataParserInterface.h ../include/ECFMP/api/StringIdentifierApiElementCollection.h api/InternalStringIdentifierApiElementCollection.h)
88

99
set(src_date date/ParseDateStrings.cpp date/ParseDateStrings.h)
1010

src/api/ApiElementIterator.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ namespace ECFMP::Api {
1111
std::unique_lock<std::recursive_mutex> lock;
1212

1313
// The current iterator position
14-
typename std::map<int, std::shared_ptr<T>>::const_iterator current;
14+
typename std::unordered_map<int, std::shared_ptr<const T>>::const_iterator current;
1515
};
1616

1717
template<typename T>
1818
ApiElementIterator<T>::ApiElementIterator(
19-
std::recursive_mutex& lockingMutex, typename std::map<int, std::shared_ptr<T>>::const_iterator current
19+
std::recursive_mutex& lockingMutex,
20+
typename std::unordered_map<int, std::shared_ptr<const T>>::const_iterator current
2021
)
2122
{
2223
impl = std::make_shared<ApiElementIterator<T>::Impl>();
@@ -36,13 +37,13 @@ namespace ECFMP::Api {
3637
ApiElementIterator<T>::~ApiElementIterator() = default;
3738

3839
template<typename T>
39-
auto ApiElementIterator<T>::operator*() const -> T&
40+
auto ApiElementIterator<T>::operator*() const -> const T&
4041
{
4142
return *impl->current->second;
4243
}
4344

4445
template<typename T>
45-
auto ApiElementIterator<T>::operator->() const -> T*
46+
auto ApiElementIterator<T>::operator->() const -> const T*
4647
{
4748
return impl->current->second.get();
4849
}

src/api/ConcreteApiElementCollection.cpp

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/api/ConcreteApiElementCollection.h

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)