OpenTTD Source 20260311-master-g511d3794ce
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
36 virtual ~Driver() = default;
37
39 enum class Type : uint8_t {
40 Begin = 0,
41 Music = 0,
45 };
46
51 virtual std::string_view GetName() const = 0;
52};
53
55
56
57
59private:
60 friend class MusicDriver;
61 friend class SoundDriver;
62 friend class VideoDriver;
63
66 std::string_view name;
67 std::string_view description;
68
69 typedef std::map<std::string, DriverFactoryBase *> Drivers;
70
76 {
77 static Drivers &s_drivers = *new Drivers();
78 return s_drivers;
79 }
80
86 static std::unique_ptr<Driver> &GetActiveDriver(Driver::Type type)
87 {
88 static std::array<std::unique_ptr<Driver>, to_underlying(Driver::Type::End)> s_driver{};
89 return s_driver[to_underlying(type)];
90 }
91
97 static std::string_view GetDriverTypeName(Driver::Type type)
98 {
99 static const std::string_view driver_type_name[] = { "music", "sound", "video" };
100 return driver_type_name[to_underlying(type)];
101 }
102
103 static bool SelectDriverImpl(const std::string &name, Driver::Type type);
104
105 static void MarkVideoDriverOperational();
106
107protected:
108 DriverFactoryBase(Driver::Type type, int priority, std::string_view name, std::string_view description);
109
111 virtual ~DriverFactoryBase();
112
117 virtual bool UsesHardwareAcceleration() const
118 {
119 return false;
120 }
121
122public:
126 static void ShutdownDrivers()
127 {
128 for (Driver::Type dt = Driver::Type::Begin; dt != Driver::Type::End; ++dt) {
129 auto &driver = GetActiveDriver(dt);
130 if (driver != nullptr) driver->Stop();
131 }
132 }
133
134 static void SelectDriver(const std::string &name, Driver::Type type);
135 static void GetDriversInfo(std::back_insert_iterator<std::string> &output_iterator);
136
141 std::string_view GetDescription() const
142 {
143 return this->description;
144 }
145
150 virtual std::unique_ptr<Driver> CreateInstance() const = 0;
151};
152
153#endif /* DRIVER_H */
Base for all driver factories.
Definition driver.h:58
std::string_view name
The name of the drivers of this factory.
Definition driver.h:66
std::string_view GetDescription() const
Get a nice description of the driver-class.
Definition driver.h:141
std::map< std::string, DriverFactoryBase * > Drivers
Type for a map of drivers.
Definition driver.h:69
static Drivers & GetDrivers()
Get the map with drivers.
Definition driver.h:75
virtual bool UsesHardwareAcceleration() const
Does the driver use hardware acceleration (video-drivers only).
Definition driver.h:117
static void ShutdownDrivers()
Shuts down all active drivers.
Definition driver.h:126
static std::unique_ptr< Driver > & GetActiveDriver(Driver::Type type)
Get the active driver for the given type.
Definition driver.h:86
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:97
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:64
int priority
The priority of this factory.
Definition driver.h:65
std::string_view description
The description of this driver.
Definition driver.h:67
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 ~Driver()=default
Ensure the destructor of the sub classes are called as well.
virtual void Stop()=0
Stop this driver.
Type
The type of driver.
Definition driver.h:39
@ Begin
Helper for iteration.
Definition driver.h:40
@ Video
A video driver.
Definition driver.h:43
@ Music
A music driver, needs to be before sound to properly shut down extmidi forked music players.
Definition driver.h:41
@ End
Helper for iteration.
Definition driver.h:44
@ Sound
A sound driver.
Definition driver.h:42
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.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
Definition enum_type.hpp:21
#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