|
1 | 1 | #pragma once |
2 | 2 | #include "ApiElementIterator.h" |
3 | | -#include "Iterable.h" |
4 | 3 |
|
5 | 4 | namespace ECFMP::Api { |
6 | 5 |
|
7 | 6 | /** |
8 | 7 | * A collection of elements from the API. |
9 | 8 | */ |
10 | 9 | template<typename T> |
11 | | - class ApiElementCollection : Plugin::Iterable<T> |
| 10 | + class ApiElementCollection |
12 | 11 | { |
13 | 12 | public: |
14 | 13 | virtual ~ApiElementCollection() = default; |
15 | 14 |
|
| 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 | + |
16 | 28 | /** |
17 | 29 | * Gets an item from the collection by its API id. |
18 | 30 | */ |
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 | + }; |
20 | 37 |
|
21 | 38 | /** |
22 | | - * Iterators. |
| 39 | + * Gets the number of elements in the collection. |
23 | 40 | */ |
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 | + }; |
29 | 55 |
|
30 | | - template<typename T> |
31 | | - class StringIdentifiedApiElementCollection : public ApiElementCollection<T> |
32 | | - { |
33 | | - public: |
34 | 56 | /** |
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. |
36 | 69 | */ |
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; |
39 | 96 | }; |
40 | 97 | }// namespace ECFMP::Api |
0 commit comments