OpenTTD
driver.h
Go to the documentation of this file.
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  */
7 
10 #ifndef DRIVER_H
11 #define DRIVER_H
12 
13 #include "core/enum_type.hpp"
15 #include <map>
16 
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);
20 
22 class Driver {
23 public:
29  virtual const char *Start(const char * const *parm) = 0;
30 
34  virtual void Stop() = 0;
35 
36  virtual ~Driver() { }
37 
39  enum Type {
40  DT_BEGIN = 0,
41  DT_MUSIC = 0,
45  };
46 
51  virtual const char *GetName() const = 0;
52 };
53 
55 
56 
57 
59 private:
60  friend class MusicDriver;
61  friend class SoundDriver;
62  friend class VideoDriver;
63 
65  int priority;
66  const char *name;
67  const char *description;
68 
69  typedef std::map<const char *, DriverFactoryBase *, StringCompare> Drivers;
70 
74  static Drivers &GetDrivers()
75  {
76  static Drivers &s_drivers = *new Drivers();
77  return s_drivers;
78  }
79 
86  {
87  static Driver *s_driver[3] = { nullptr, nullptr, nullptr };
88  return &s_driver[type];
89  }
90 
96  static const char *GetDriverTypeName(Driver::Type type)
97  {
98  static const char * const driver_type_name[] = { "music", "sound", "video" };
99  return driver_type_name[type];
100  }
101 
102  static bool SelectDriverImpl(const char *name, Driver::Type type);
103 
104 protected:
105  DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description);
106 
107  virtual ~DriverFactoryBase();
108 
109 public:
113  static void ShutdownDrivers()
114  {
115  for (Driver::Type dt = Driver::DT_BEGIN; dt < Driver::DT_END; dt++) {
116  Driver *driver = *GetActiveDriver(dt);
117  if (driver != nullptr) driver->Stop();
118  }
119  }
120 
121  static void SelectDriver(const char *name, Driver::Type type);
122  static char *GetDriversInfo(char *p, const char *last);
123 
128  const char *GetDescription() const
129  {
130  return this->description;
131  }
132 
137  virtual Driver *CreateInstance() const = 0;
138 };
139 
140 #endif /* DRIVER_H */
Helper for iteration.
Definition: driver.h:44
static Drivers & GetDrivers()
Get the map with drivers.
Definition: driver.h:74
static Driver ** GetActiveDriver(Driver::Type type)
Get the active driver for the given type.
Definition: driver.h:85
int GetDriverParamInt(const char *const *parm, const char *name, int def)
Get an integer parameter the list of parameters.
Definition: driver.cpp:73
static const char * GetDriverTypeName(Driver::Type type)
Get the driver type name.
Definition: driver.h:96
static void ShutdownDrivers()
Shuts down all active drivers.
Definition: driver.h:113
Base for all sound drivers.
Type (helpers) for enums.
int priority
The priority of this factory.
Definition: driver.h:65
A music driver, needs to be before sound to properly shut down extmidi forked music players...
Definition: driver.h:41
const char * description
The description of this driver.
Definition: driver.h:67
virtual void Stop()=0
Stop this driver.
virtual const char * Start(const char *const *parm)=0
Start this driver.
Driver::Type type
The type of driver.
Definition: driver.h:64
Base for all driver factories.
Definition: driver.h:58
A sound driver.
Definition: driver.h:42
bool GetDriverParamBool(const char *const *parm, const char *name)
Get a boolean parameter the list of parameters.
Definition: driver.cpp:61
Helper for iteration.
Definition: driver.h:40
virtual const char * GetName() const =0
Get the name of this driver.
const char * GetDriverParam(const char *const *parm, const char *name)
Get a string parameter the list of parameters.
Definition: driver.cpp:37
A driver for communicating with the user.
Definition: driver.h:22
Comparator class for "const char *" so it can be used as a key for std::map.
Type
The type of driver.
Definition: driver.h:39
The base of all video drivers.
A video driver.
Definition: driver.h:43
#define DECLARE_POSTFIX_INCREMENT(enum_type)
Some enums need to have allowed incrementing (i.e.
Definition: enum_type.hpp:14
const char * GetDescription() const
Get a nice description of the driver-class.
Definition: driver.h:128
std::map< const char *, DriverFactoryBase *, StringCompare > Drivers
Type for a map of drivers.
Definition: driver.h:69
Driver for all music playback.
const char * name
The name of the drivers of this factory.
Definition: driver.h:66