OpenTTD Source 20241224-master-gf74b0cf984
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 <http://www.gnu.org/licenses/>.
6 */
7
10#ifndef PROVIDER_MANAGER_H
11#define PROVIDER_MANAGER_H
12
20template <typename TProviderType>
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(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(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<TProviderType *> &GetProviders()
47 {
48 static std::vector<TProviderType *> providers{};
49 return providers;
50 }
51};
52
53template <typename T>
55public:
56 constexpr BaseProvider(std::string_view name, std::string_view description) : name(name), description(description) {}
57 virtual ~BaseProvider() {}
58
59 inline std::string_view GetName() const { return this->name; }
60 inline std::string_view GetDescription() const { return this->description; }
61
69 bool operator()(const T *a, const T *b) const
70 {
71 if (a->GetName() == b->GetName()) return a < b;
72 return a->GetName() < b->GetName();
73 }
74 };
75
76protected:
77 const std::string_view name;
78 const std::string_view description;
79};
80
81template <typename T>
83public:
84 constexpr PriorityBaseProvider(std::string_view name, std::string_view description, int priority) : BaseProvider<T>(name, description), priority(priority) {}
85 virtual ~PriorityBaseProvider() {}
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 */
The ProviderManager manages a single Provider-type.
static std::vector< TProviderType * > & GetProviders()
Get the currently known sound loaders.
Sorter for BaseProvider.
Sorter for PriorityBaseProvider.