OpenTTD Source: src/provider_manager.h Source File

OpenTTD Source 15.0-RC2

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
10#ifndef PROVIDER_MANAGER_H
11#define PROVIDER_MANAGER_H
12
20template <typename TProviderType>
21 class 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>
54 class BaseProvider {
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
68 struct ProviderSorter {
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>
82 class PriorityBaseProvider : public BaseProvider<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
95 struct ProviderSorter {
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 */
BaseProvider
Definition provider_manager.h:54
PriorityBaseProvider
Definition provider_manager.h:82
ProviderManager
The ProviderManager manages a single Provider-type.
Definition provider_manager.h:21
ProviderManager::GetProviders
static std::vector< const TProviderType * > & GetProviders()
Get the currently known providers.
Definition provider_manager.h:46
BaseProvider::ProviderSorter
Sorter for BaseProvider.
Definition provider_manager.h:68
PriorityBaseProvider::ProviderSorter
Sorter for PriorityBaseProvider.
Definition provider_manager.h:95