OpenTTD Source 20260311-master-g511d3794ce
provider_manager.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef PROVIDER_MANAGER_H
11#define PROVIDER_MANAGER_H
12
20template <typename TProviderType>
21class ProviderManager {
22public:
23 /* Avoid copying this object; it is a singleton object. */
24 ProviderManager(ProviderManager const &) = delete;
25
26 ProviderManager &operator=(ProviderManager const &) = delete;
27
28 static void Register(const TProviderType &instance)
29 {
30 /* Insert according to comparator. */
31 auto &providers = GetProviders();
32 auto it = std::ranges::lower_bound(providers, &instance, typename TProviderType::ProviderSorter());
33 providers.insert(it, &instance);
34 }
35
36 static void Unregister(const TProviderType &instance)
37 {
38 auto &providers = GetProviders();
39 providers.erase(std::find(std::begin(providers), std::end(providers), &instance));
40 }
41
46 static std::vector<const TProviderType *> &GetProviders()
47 {
48 static std::vector<const TProviderType *> providers{};
49 return providers;
50 }
51};
52
53template <typename T>
54class BaseProvider {
55public:
56 constexpr BaseProvider(std::string_view name, std::string_view description) : name(name), description(description) {}
58 virtual ~BaseProvider() = default;
59
60 inline std::string_view GetName() const { return this->name; }
61 inline std::string_view GetDescription() const { return this->description; }
62
70 bool operator()(const T *a, const T *b) const
71 {
72 if (a->GetName() == b->GetName()) return a < b;
73 return a->GetName() < b->GetName();
74 }
75 };
76
77protected:
78 const std::string_view name;
79 const std::string_view description;
80};
81
82template <typename T>
83class PriorityBaseProvider : public BaseProvider<T> {
84public:
85 constexpr PriorityBaseProvider(std::string_view name, std::string_view description, int priority) : BaseProvider<T>(name, description), priority(priority) {}
86
87 inline int GetPriority() const { return this->priority; }
88
96 bool operator()(const T *a, const T *b) const
97 {
98 if (a->GetPriority() == b->GetPriority()) return a < b;
99 return a->GetPriority() < b->GetPriority();
100 }
101 };
102
103protected:
104 const int priority;
105};
106
107#endif /* PROVIDER_MANAGER_H */
virtual ~BaseProvider()=default
Ensure the destructor of the sub classes are called as well.
static std::vector< const TProviderType * > & GetProviders()
Get the currently known providers.
#define T
Climate temperate.
Definition engines.h:91
Sorter for BaseProvider.
Sorter for PriorityBaseProvider.