Fix: Violation of strict weak ordering in engine name sorter
[openttd-github.git] / src / driver.h
blob68d9fac929477ca8b4def218357ee2acd72c7b12
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 "core/string_compare_type.hpp"
15 #include "string_type.h"
16 #include <map>
18 const char *GetDriverParam(const StringList &parm, const char *name);
19 bool GetDriverParamBool(const StringList &parm, const char *name);
20 int GetDriverParamInt(const StringList &parm, const char *name, int def);
22 /** A driver for communicating with the user. */
23 class Driver {
24 public:
25 /**
26 * Start this driver.
27 * @param parm Parameters passed to the driver.
28 * @return nullptr if everything went okay, otherwise an error message.
30 virtual const char *Start(const StringList &parm) = 0;
32 /**
33 * Stop this driver.
35 virtual void Stop() = 0;
37 virtual ~Driver() { }
39 /** The type of driver */
40 enum Type {
41 DT_BEGIN = 0, ///< Helper for iteration
42 DT_MUSIC = 0, ///< A music driver, needs to be before sound to properly shut down extmidi forked music players
43 DT_SOUND, ///< A sound driver
44 DT_VIDEO, ///< A video driver
45 DT_END, ///< Helper for iteration
48 /**
49 * Get the name of this driver.
50 * @return The name of the driver.
52 virtual const char *GetName() const = 0;
55 DECLARE_POSTFIX_INCREMENT(Driver::Type)
58 /** Base for all driver factories. */
59 class DriverFactoryBase {
60 private:
61 friend class MusicDriver;
62 friend class SoundDriver;
63 friend class VideoDriver;
65 Driver::Type type; ///< The type of driver.
66 int priority; ///< The priority of this factory.
67 const char *name; ///< The name of the drivers of this factory.
68 const char *description; ///< The description of this driver.
70 typedef std::map<std::string, DriverFactoryBase *> Drivers; ///< Type for a map of drivers.
72 /**
73 * Get the map with drivers.
75 static Drivers &GetDrivers()
77 static Drivers &s_drivers = *new Drivers();
78 return s_drivers;
81 /**
82 * Get the active driver for the given type.
83 * @param type The type to get the driver for.
84 * @return The active driver.
86 static Driver **GetActiveDriver(Driver::Type type)
88 static Driver *s_driver[3] = { nullptr, nullptr, nullptr };
89 return &s_driver[type];
92 /**
93 * Get the driver type name.
94 * @param type The type of driver to get the name of.
95 * @return The name of the type.
97 static const char *GetDriverTypeName(Driver::Type type)
99 static const char * const driver_type_name[] = { "music", "sound", "video" };
100 return driver_type_name[type];
103 static bool SelectDriverImpl(const std::string &name, Driver::Type type);
105 protected:
106 DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description);
108 virtual ~DriverFactoryBase();
110 public:
112 * Shuts down all active drivers
114 static void ShutdownDrivers()
116 for (Driver::Type dt = Driver::DT_BEGIN; dt < Driver::DT_END; dt++) {
117 Driver *driver = *GetActiveDriver(dt);
118 if (driver != nullptr) driver->Stop();
122 static void SelectDriver(const std::string &name, Driver::Type type);
123 static char *GetDriversInfo(char *p, const char *last);
126 * Get a nice description of the driver-class.
127 * @return The description.
129 const char *GetDescription() const
131 return this->description;
135 * Create an instance of this driver-class.
136 * @return The instance.
138 virtual Driver *CreateInstance() const = 0;
141 #endif /* DRIVER_H */