OpenTTD Source  20241121-master-g67a0fccfad
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 <http://www.gnu.org/licenses/>.
6  */
7 
10 #ifndef DRIVER_H
11 #define DRIVER_H
12 
13 #include "core/enum_type.hpp"
14 #include "string_type.h"
15 
16 const char *GetDriverParam(const StringList &parm, const char *name);
17 bool GetDriverParamBool(const StringList &parm, const char *name);
18 int GetDriverParamInt(const StringList &parm, const char *name, int def);
19 
21 class Driver {
22 public:
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 {
39  DT_BEGIN = 0,
40  DT_MUSIC = 0,
44  };
45 
50  virtual std::string_view GetName() const = 0;
51 };
52 
54 
55 
56 
58 private:
59  friend class MusicDriver;
60  friend class SoundDriver;
61  friend class VideoDriver;
62 
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 
85  {
86  static Driver *s_driver[3] = { nullptr, nullptr, nullptr };
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 
105 protected:
106  DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description);
107 
108  virtual ~DriverFactoryBase();
109 
114  virtual bool UsesHardwareAcceleration() const
115  {
116  return false;
117  }
118 
119 public:
123  static void ShutdownDrivers()
124  {
125  for (Driver::Type dt = Driver::DT_BEGIN; dt < Driver::DT_END; dt++) {
126  Driver *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 Driver *CreateInstance() const = 0;
148 };
149 
150 #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:138
std::map< std::string, DriverFactoryBase * > Drivers
Type for a map of drivers.
Definition: driver.h:68
virtual bool UsesHardwareAcceleration() const
Does the driver use hardware acceleration (video-drivers only).
Definition: driver.h:114
static void ShutdownDrivers()
Shuts down all active drivers.
Definition: driver.h:123
virtual Driver * CreateInstance() const =0
Create an instance of this driver-class.
static std::string_view GetDriverTypeName(Driver::Type type)
Get the driver type name.
Definition: driver.h:95
static Drivers & GetDrivers()
Get the map with drivers.
Definition: driver.h:73
Driver::Type type
The type of driver.
Definition: driver.h:63
static Driver ** GetActiveDriver(Driver::Type type)
Get the active driver for the given type.
Definition: driver.h:84
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.
virtual std::optional< std::string_view > Start(const StringList &parm)=0
Start 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
Driver for all music playback.
Base for all sound drivers.
The base of all video drivers.
const char * GetDriverParam(const StringList &parm, const char *name)
Get a string parameter the list of parameters.
Definition: driver.cpp:44
int GetDriverParamInt(const StringList &parm, const char *name, int def)
Get an integer parameter the list of parameters.
Definition: driver.cpp:76
bool GetDriverParamBool(const StringList &parm, const char *name)
Get a boolean parameter the list of parameters.
Definition: driver.cpp:64
Type (helpers) for enums.
#define DECLARE_POSTFIX_INCREMENT(enum_type)
Some enums need to have allowed incrementing (i.e.
Definition: enum_type.hpp:18
Types for strings.
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:60