OpenTTD

driver.h
Go to the documentation of this file.
1 /* $Id: driver.h 26538 2014-04-28 21:06:51Z rubidium $ */
2
3 /*
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/>.
8 */
9
12 #ifndef DRIVER_H
13 #define DRIVER_H
14
15 #include "core/enum_type.hpp"
16 #include "core/string_compare_type.hpp"
17 #include <map>
18
19 const char *GetDriverParam(const char * const *parm, const char *name);
20 bool GetDriverParamBool(const char * const *parm, const char *name);
21 int GetDriverParamInt(const char * const *parm, const char *name, int def);
22
24 class Driver {
25 public:
31 virtual const char *Start(const char * const *parm) = 0;
32
36 virtual void Stop() = 0;
37
38 virtual ~Driver() { }
39
41 enum Type {
42 DT_BEGIN = 0,
43 DT_MUSIC = 0,
44 DT_SOUND,
45 DT_VIDEO,
46 DT_END,
47 };
48
53 virtual const char *GetName() const = 0;
54 };
55
56 DECLARE_POSTFIX_INCREMENT(Driver::Type)
57
58
59
60 class DriverFactoryBase {
61 private:
62 friend class MusicDriver;
63 friend class SoundDriver;
64 friend class VideoDriver;
65
66 Driver::Type type;
67 int priority;
68 const char *name;
69 const char *description;
70
71 typedef std::map<const char *, DriverFactoryBase *, StringCompare> Drivers;
72
76 static Drivers &GetDrivers()
77 {
78 static Drivers &s_drivers = *new Drivers();
79 return s_drivers;
80 }
81
87 static Driver **GetActiveDriver(Driver::Type type)
88 {
89 static Driver *s_driver[3] = { NULL, NULL, NULL };
90 return &s_driver[type];
91 }
92
98 static const char *GetDriverTypeName(Driver::Type type)
99 {
100 static const char * const driver_type_name[] = { "music", "sound", "video" };
101 return driver_type_name[type];
102 }
103
104 static bool SelectDriverImpl(const char *name, Driver::Type type);
105
106 protected:
107 DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description);
108
109 virtual ~DriverFactoryBase();
110
111 public:
115 static void ShutdownDrivers()
116 {
117 for (Driver::Type dt = Driver::DT_BEGIN; dt < Driver::DT_END; dt++) {
118 Driver *driver = *GetActiveDriver(dt);
119 if (driver != NULL) driver->Stop();
120 }
121 }
122
123 static void SelectDriver(const char *name, Driver::Type type);
124 static char *GetDriversInfo(char *p, const char *last);
125
130 const char *GetDescription() const
131 {
132 return this->description;
133 }
134
139 virtual Driver *CreateInstance() const = 0;
140 };
141
142 #endif /* DRIVER_H */
Driver::DT_END
Helper for iteration.
Definition: driver.h:46
DriverFactoryBase::GetDrivers
static Drivers & GetDrivers()
Get the map with drivers.
Definition: driver.h:76
DriverFactoryBase::GetActiveDriver
static Driver ** GetActiveDriver(Driver::Type type)
Get the active driver for the given type.
Definition: driver.h:87
GetDriverParamInt
int GetDriverParamInt(const char *const *parm, const char *name, int def)
Get an integer parameter the list of parameters.
Definition: driver.cpp:76
DriverFactoryBase::GetDriverTypeName
static const char * GetDriverTypeName(Driver::Type type)
Get the driver type name.
Definition: driver.h:98
DriverFactoryBase::ShutdownDrivers
static void ShutdownDrivers()
Shuts down all active drivers.
Definition: driver.h:115
SoundDriver
Base for all sound drivers.
Definition: sound_driver.hpp:18
enum_type.hpp
Type (helpers) for enums.
DriverFactoryBase::priority
int priority
The priority of this factory.
Definition: driver.h:67
Driver::DT_MUSIC
A music driver, needs to be before sound to properly shut down extmidi forked music players...
Definition: driver.h:43
DriverFactoryBase::description
const char * description
The description of this driver.
Definition: driver.h:69
Driver::Stop
virtual void Stop()=0
Stop this driver.
Driver::Start
virtual const char * Start(const char *const *parm)=0
Start this driver.
DriverFactoryBase::type
Driver::Type type
The type of driver.
Definition: driver.h:66
DriverFactoryBase
Base for all driver factories.
Definition: driver.h:60
Driver::DT_SOUND
A sound driver.
Definition: driver.h:44
GetDriverParamBool
bool GetDriverParamBool(const char *const *parm, const char *name)
Get a boolean parameter the list of parameters.
Definition: driver.cpp:64
Driver::DT_BEGIN
Helper for iteration.
Definition: driver.h:42
Driver::GetName
virtual const char * GetName() const =0
Get the name of this driver.
GetDriverParam
const char * GetDriverParam(const char *const *parm, const char *name)
Get a string parameter the list of parameters.
Definition: driver.cpp:40
Driver
A driver for communicating with the user.
Definition: driver.h:24
string_compare_type.hpp
Comparator class for "const char *" so it can be used as a key for std::map.
Driver::Type
Type
The type of driver.
Definition: driver.h:41
VideoDriver
The base of all video drivers.
Definition: video_driver.hpp:19
Driver::DT_VIDEO
A video driver.
Definition: driver.h:45
DriverFactoryBase::GetDescription
const char * GetDescription() const
Get a nice description of the driver-class.
Definition: driver.h:130
DECLARE_POSTFIX_INCREMENT
#define DECLARE_POSTFIX_INCREMENT(type)
Some enums need to have allowed incrementing (i.e.
Definition: enum_type.hpp:16
DriverFactoryBase::Drivers
std::map< const char *, DriverFactoryBase *, StringCompare > Drivers
Type for a map of drivers.
Definition: driver.h:71
MusicDriver
Driver for all music playback.
Definition: music_driver.hpp:18
DriverFactoryBase::name
const char * name
The name of the drivers of this factory.
Definition: driver.h:68