Codefix: Documentation comment in IndustryDirectoryWindow (#13059)
[openttd-github.git] / src / driver.h
blobef39391890cea70d55b239d645ce78d008f1029e
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 */
8 /** @file driver.h Base for all drivers (video, sound, music, etc). */
10 #ifndef DRIVER_H
11 #define DRIVER_H
13 #include "core/enum_type.hpp"
14 #include "string_type.h"
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);
20 /** A driver for communicating with the user. */
21 class Driver {
22 public:
23 /**
24 * Start this driver.
25 * @param parm Parameters passed to the driver.
26 * @return std::nullopt if everything went okay, otherwise an error message.
28 virtual std::optional<std::string_view> Start(const StringList &parm) = 0;
30 /**
31 * Stop this driver.
33 virtual void Stop() = 0;
35 virtual ~Driver() = default;
37 /** The type of driver */
38 enum Type {
39 DT_BEGIN = 0, ///< Helper for iteration
40 DT_MUSIC = 0, ///< A music driver, needs to be before sound to properly shut down extmidi forked music players
41 DT_SOUND, ///< A sound driver
42 DT_VIDEO, ///< A video driver
43 DT_END, ///< Helper for iteration
46 /**
47 * Get the name of this driver.
48 * @return The name of the driver.
50 virtual std::string_view GetName() const = 0;
53 DECLARE_POSTFIX_INCREMENT(Driver::Type)
56 /** Base for all driver factories. */
57 class DriverFactoryBase {
58 private:
59 friend class MusicDriver;
60 friend class SoundDriver;
61 friend class VideoDriver;
63 Driver::Type type; ///< The type of driver.
64 int priority; ///< The priority of this factory.
65 std::string_view name; ///< The name of the drivers of this factory.
66 std::string_view description; ///< The description of this driver.
68 typedef std::map<std::string, DriverFactoryBase *> Drivers; ///< Type for a map of drivers.
70 /**
71 * Get the map with drivers.
73 static Drivers &GetDrivers()
75 static Drivers &s_drivers = *new Drivers();
76 return s_drivers;
79 /**
80 * Get the active driver for the given type.
81 * @param type The type to get the driver for.
82 * @return The active driver.
84 static Driver **GetActiveDriver(Driver::Type type)
86 static Driver *s_driver[3] = { nullptr, nullptr, nullptr };
87 return &s_driver[type];
90 /**
91 * Get the driver type name.
92 * @param type The type of driver to get the name of.
93 * @return The name of the type.
95 static std::string_view GetDriverTypeName(Driver::Type type)
97 static const std::string_view driver_type_name[] = { "music", "sound", "video" };
98 return driver_type_name[type];
101 static bool SelectDriverImpl(const std::string &name, Driver::Type type);
103 static void MarkVideoDriverOperational();
105 protected:
106 DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description);
108 virtual ~DriverFactoryBase();
111 * Does the driver use hardware acceleration (video-drivers only).
112 * @return True if the driver uses hardware acceleration.
114 virtual bool UsesHardwareAcceleration() const
116 return false;
119 public:
121 * Shuts down all active drivers
123 static void ShutdownDrivers()
125 for (Driver::Type dt = Driver::DT_BEGIN; dt < Driver::DT_END; dt++) {
126 Driver *driver = *GetActiveDriver(dt);
127 if (driver != nullptr) driver->Stop();
131 static void SelectDriver(const std::string &name, Driver::Type type);
132 static void GetDriversInfo(std::back_insert_iterator<std::string> &output_iterator);
135 * Get a nice description of the driver-class.
136 * @return The description.
138 std::string_view GetDescription() const
140 return this->description;
144 * Create an instance of this driver-class.
145 * @return The instance.
147 virtual Driver *CreateInstance() const = 0;
150 #endif /* DRIVER_H */