Add: INR currency (#8136)
[openttd-github.git] / src / driver.h
blob3ac4f7f8a40223f3b1a9e5d89b94e58ec5dd0ed9
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 <map>
17 const char *GetDriverParam(const char * const *parm, const char *name);
18 bool GetDriverParamBool(const char * const *parm, const char *name);
19 int GetDriverParamInt(const char * const *parm, const char *name, int def);
21 /** A driver for communicating with the user. */
22 class Driver {
23 public:
24 /**
25 * Start this driver.
26 * @param parm Parameters passed to the driver.
27 * @return nullptr if everything went okay, otherwise an error message.
29 virtual const char *Start(const char * const *parm) = 0;
31 /**
32 * Stop this driver.
34 virtual void Stop() = 0;
36 virtual ~Driver() { }
38 /** The type of driver */
39 enum Type {
40 DT_BEGIN = 0, ///< Helper for iteration
41 DT_MUSIC = 0, ///< A music driver, needs to be before sound to properly shut down extmidi forked music players
42 DT_SOUND, ///< A sound driver
43 DT_VIDEO, ///< A video driver
44 DT_END, ///< Helper for iteration
47 /**
48 * Get the name of this driver.
49 * @return The name of the driver.
51 virtual const char *GetName() const = 0;
54 DECLARE_POSTFIX_INCREMENT(Driver::Type)
57 /** Base for all driver factories. */
58 class DriverFactoryBase {
59 private:
60 friend class MusicDriver;
61 friend class SoundDriver;
62 friend class VideoDriver;
64 Driver::Type type; ///< The type of driver.
65 int priority; ///< The priority of this factory.
66 const char *name; ///< The name of the drivers of this factory.
67 const char *description; ///< The description of this driver.
69 typedef std::map<const char *, DriverFactoryBase *, StringCompare> Drivers; ///< Type for a map of drivers.
71 /**
72 * Get the map with drivers.
74 static Drivers &GetDrivers()
76 static Drivers &s_drivers = *new Drivers();
77 return s_drivers;
80 /**
81 * Get the active driver for the given type.
82 * @param type The type to get the driver for.
83 * @return The active driver.
85 static Driver **GetActiveDriver(Driver::Type type)
87 static Driver *s_driver[3] = { nullptr, nullptr, nullptr };
88 return &s_driver[type];
91 /**
92 * Get the driver type name.
93 * @param type The type of driver to get the name of.
94 * @return The name of the type.
96 static const char *GetDriverTypeName(Driver::Type type)
98 static const char * const driver_type_name[] = { "music", "sound", "video" };
99 return driver_type_name[type];
102 static bool SelectDriverImpl(const char *name, Driver::Type type);
104 protected:
105 DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description);
107 virtual ~DriverFactoryBase();
109 public:
111 * Shuts down all active drivers
113 static void ShutdownDrivers()
115 for (Driver::Type dt = Driver::DT_BEGIN; dt < Driver::DT_END; dt++) {
116 Driver *driver = *GetActiveDriver(dt);
117 if (driver != nullptr) driver->Stop();
121 static void SelectDriver(const char *name, Driver::Type type);
122 static char *GetDriversInfo(char *p, const char *last);
125 * Get a nice description of the driver-class.
126 * @return The description.
128 const char *GetDescription() const
130 return this->description;
134 * Create an instance of this driver-class.
135 * @return The instance.
137 virtual Driver *CreateInstance() const = 0;
140 #endif /* DRIVER_H */