OpenTTD Source: src/driver.h Source File

OpenTTD Source 15.3

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