OpenTTD Source 20260421-master-gc2fbc6fdeb
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>
55public:
61 constexpr BaseProvider(std::string_view name, std::string_view description) : name(name), description(description) {}
63 virtual ~BaseProvider() = default;
64
69 inline std::string_view GetName() const { return this->name; }
70
75 inline std::string_view GetDescription() const { return this->description; }
76
84 bool operator()(const T *a, const T *b) const
85 {
86 if (a->GetName() == b->GetName()) return a < b;
87 return a->GetName() < b->GetName();
88 }
89 };
90
91protected:
92 const std::string_view name;
93 const std::string_view description;
94};
95
96template <typename T>
98public:
105 constexpr PriorityBaseProvider(std::string_view name, std::string_view description, int priority) : BaseProvider<T>(name, description), priority(priority) {}
106
111 inline int GetPriority() const { return this->priority; }
112
120 bool operator()(const T *a, const T *b) const
121 {
122 if (a->GetPriority() == b->GetPriority()) return a < b;
123 return a->GetPriority() < b->GetPriority();
124 }
125 };
126
127protected:
128 const int priority;
129};
130
131#endif /* PROVIDER_MANAGER_H */
virtual ~BaseProvider()=default
Ensure the destructor of the sub classes are called as well.
std::string_view GetName() const
Get the name of this provider.
constexpr BaseProvider(std::string_view name, std::string_view description)
Create the provider.
const std::string_view name
The name of the provider.
std::string_view GetDescription() const
Get a description of this provider.
const std::string_view description
A description of the provider.
const int priority
The priority of this provider.
constexpr PriorityBaseProvider(std::string_view name, std::string_view description, int priority)
Create the provider.
int GetPriority() const
Get the priority of this provider.
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.