OpenTTD Source 20260218-master-g2123fca5ea
driver.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 DRIVER_H
11#define DRIVER_H
12
13#include "core/enum_type.hpp"
14#include "string_type.h"
15
16std::optional<std::string_view> GetDriverParam(const StringList &parm, std::string_view name);
17bool GetDriverParamBool(const StringList &parm, std::string_view name);
18int GetDriverParamInt(const StringList &parm, std::string_view name, int def);
19
21class Driver {
22public:
28 virtual std::optional<std::string_view> Start(const StringList &parm) = 0;
29
33 virtual void Stop() = 0;
34
35 virtual ~Driver() = default;
36
38 enum Type : uint8_t {
44 };
45
50 virtual std::string_view GetName() const = 0;
51};
52
54
55
56
58private:
59 friend class MusicDriver;
60 friend class SoundDriver;
61 friend class VideoDriver;
62
65 std::string_view name;
66 std::string_view description;
67
68 typedef std::map<std::string, DriverFactoryBase *> Drivers;
69
75 {
76 static Drivers &s_drivers = *new Drivers();
77 return s_drivers;
78 }
79
85 static std::unique_ptr<Driver> &GetActiveDriver(Driver::Type type)
86 {
87 static std::array<std::unique_ptr<Driver>, Driver::DT_END> s_driver{};
88 return s_driver[type];
89 }
90
96 static std::string_view GetDriverTypeName(Driver::Type type)
97 {
98 static const std::string_view driver_type_name[] = { "music", "sound", "video" };
99 return driver_type_name[type];
100 }
101
102 static bool SelectDriverImpl(const std::string &name, Driver::Type type);
103
104 static void MarkVideoDriverOperational();
105
106protected:
107 DriverFactoryBase(Driver::Type type, int priority, std::string_view name, std::string_view description);
108
109 virtual ~DriverFactoryBase();
110
115 virtual bool UsesHardwareAcceleration() const
116 {
117 return false;
118 }
119
120public:
124 static void ShutdownDrivers()
125 {
126 for (Driver::Type dt = Driver::DT_BEGIN; dt < Driver::DT_END; dt++) {
127 auto &driver = GetActiveDriver(dt);
128 if (driver != nullptr) driver->Stop();
129 }
130 }
131
132 static void SelectDriver(const std::string &name, Driver::Type type);
133 static void GetDriversInfo(std::back_insert_iterator<std::string> &output_iterator);
134
139 std::string_view GetDescription() const
140 {
141 return this->description;
142 }
143
148 virtual std::unique_ptr<Driver> CreateInstance() const = 0;
149};
150
151#endif /* DRIVER_H */
Base for all driver factories.
Definition driver.h:57
std::string_view name
The name of the drivers of this factory.
Definition driver.h:65
std::string_view GetDescription() const
Get a nice description of the driver-class.
Definition driver.h:139
std::map< std::string, DriverFactoryBase * > Drivers
Type for a map of drivers.
Definition driver.h:68
static Drivers & GetDrivers()
Get the map with drivers.
Definition driver.h:74
virtual bool UsesHardwareAcceleration() const
Does the driver use hardware acceleration (video-drivers only).
Definition driver.h:115
static void ShutdownDrivers()
Shuts down all active drivers.
Definition driver.h:124
static std::unique_ptr< Driver > & GetActiveDriver(Driver::Type type)
Get the active driver for the given type.
Definition driver.h:85
DriverFactoryBase(Driver::Type type, int priority, std::string_view name, std::string_view description)
Construct a new DriverFactory.
Definition driver.cpp:246
static std::string_view GetDriverTypeName(Driver::Type type)
Get the driver type name.
Definition driver.h:96
virtual std::unique_ptr< Driver > CreateInstance() const =0
Create an instance of this driver-class.
Driver::Type type
The type of driver.
Definition driver.h:63
int priority
The priority of this factory.
Definition driver.h:64
std::string_view description
The description of this driver.
Definition driver.h:66
A driver for communicating with the user.
Definition driver.h:21
virtual std::string_view GetName() const =0
Get the name of this driver.
virtual void Stop()=0
Stop this driver.
Type
The type of driver.
Definition driver.h:38
@ DT_END
Helper for iteration.
Definition driver.h:43
@ DT_VIDEO
A video driver.
Definition driver.h:42
@ DT_SOUND
A sound driver.
Definition driver.h:41
@ DT_BEGIN
Helper for iteration.
Definition driver.h:39
@ DT_MUSIC
A music driver, needs to be before sound to properly shut down extmidi forked music players.
Definition driver.h:40
virtual std::optional< std::string_view > Start(const StringList &parm)=0
Start this driver.
int GetDriverParamInt(const StringList &parm, std::string_view name, int def)
Get an integer parameter the list of parameters.
Definition driver.cpp:79
std::optional< std::string_view > GetDriverParam(const StringList &parm, std::string_view name)
Get a string parameter the list of parameters.
Definition driver.cpp:47
bool GetDriverParamBool(const StringList &parm, std::string_view name)
Get a boolean parameter the list of parameters.
Definition driver.cpp:67
Type (helpers) for enums.
#define DECLARE_INCREMENT_DECREMENT_OPERATORS(enum_type)
For some enums it is useful to have pre/post increment/decrement operators.
Definition enum_type.hpp:86
Types for strings.
std::vector< std::string > StringList
Type for a list of strings.
Definition string_type.h:60