4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file driver.h Base for all drivers (video, sound, music, etc). */
17 #include "core/enum_type.hpp"
18 #include "core/string_compare_type.hpp"
19 #include "core/pointer.h"
22 const char *GetDriverParam(const char * const *parm
, const char *name
);
23 bool GetDriverParamBool(const char * const *parm
, const char *name
);
24 int GetDriverParamInt(const char * const *parm
, const char *name
, int def
);
26 /** A driver for communicating with the user. */
31 * @param parm Parameters passed to the driver.
32 * @return NULL if everything went okay, otherwise an error message.
34 virtual const char *Start(const char * const *parm
) = 0;
39 virtual void Stop() = 0;
45 /** Base for all driver factories. */
46 struct DriverFactoryBase
{
47 typedef Driver
*inst_func (void); ///< Driver instance creation function type.
49 inst_func
*const create
; ///< The function that creates the driver instance.
50 const char *const name
; ///< The name of the drivers of this factory.
51 const char *const description
; ///< The description of this driver.
52 const int priority
; ///< The priority of this factory.
55 * Construct a new DriverFactory.
56 * @param name The name of the driver.
57 * @param description A long-ish description of the driver.
58 * @param priority The priority within the driver class.
60 CONSTEXPR
DriverFactoryBase (inst_func
*create
, const char *name
,
61 const char *description
, int priority
)
62 : create(create
), name(name
),
63 description(description
), priority(priority
)
69 /** Encapsulation of a driver system (music, sound, video). */
71 typedef std::map
<const char *, DriverFactoryBase
*, StringCompare
> map
;
73 map drivers
; ///< Map of available drivers.
74 const char *const desc
; ///< Name of the driver system.
75 Driver
*active
; ///< Currently active driver.
76 const char *name
; ///< Name of the currently active driver.
78 DriverSystem (const char *desc
);
80 void insert (const char *name
, DriverFactoryBase
*factory
);
82 void erase (const char *name
);
84 void select (const char *name
);
86 void list (stringb
*buf
);
90 /** Driver system struct to share a common static DriverSystem. */
92 class SharedDriverSystem
{
94 /** Get the driver system. */
95 static DriverSystem
&GetSystem (void)
97 static DriverSystem
system (T::GetSystemName());
102 /** Insert a driver factory into the list. */
103 static void insert (const char *name
, DriverFactoryBase
*factory
)
105 GetSystem().insert (name
, factory
);
108 /** Remove a driver factory from the list. */
109 static void erase (const char *name
)
111 GetSystem().erase (name
);
115 /** Shuts down the active driver. */
116 static void ShutdownDriver (void)
118 Driver
*driver
= GetSystem().active
;
119 if (driver
!= NULL
) driver
->Stop();
123 * Find the requested driver and return its class.
124 * @param name the driver to select.
125 * @post Sets the driver so GetCurrentDriver() returns it too.
127 static void SelectDriver (const char *name
)
129 GetSystem().select (name
);
133 * Get the active driver.
134 * @return The active driver.
136 static T
*GetActiveDriver (void)
138 return static_cast<T
*> (GetSystem().active
);
142 * Get the name of the active driver.
143 * @return The name of the active driver.
145 static const char *GetActiveDriverName (void)
147 return GetSystem().name
;
151 * Build a human readable list of available drivers.
152 * @param buf The buffer to write to.
154 static void GetDriversInfo (stringb
*buf
)
156 GetSystem().list (buf
);
161 /** Specialised driver factory helper class. */
162 template <class T
, class D
>
163 class DriverFactory
: DriverFactoryBase
, public SharedDriverSystem
<T
> {
166 * Create an instance of this driver-class.
167 * @return The instance.
169 static Driver
*create (void)
175 * Construct a new DriverFactory.
176 * @param priority The priority within the driver class.
177 * @param name The name of the driver.
178 * @param description A long-ish description of the driver.
180 DriverFactory (int priority
, const char *name
, const char *description
)
181 : DriverFactoryBase (&create
, name
, description
, priority
)
183 SharedDriverSystem
<T
>::insert (name
, this);
186 /** Destruct a DriverFactory. */
189 SharedDriverSystem
<T
>::erase (this->name
);
193 #endif /* DRIVER_H */