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"
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,
47  };
48 
53  virtual const char *GetName() const = 0;
54 };
55 
57 
58 
59 
61 private:
62  friend class MusicDriver;
63  friend class SoundDriver;
64  friend class VideoDriver;
65 
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 
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 */
Helper for iteration.
Definition: driver.h:46
static Drivers & GetDrivers()
Get the map with drivers.
Definition: driver.h:76
static Driver ** GetActiveDriver(Driver::Type type)
Get the active driver for the given type.
Definition: driver.h:87
int GetDriverParamInt(const char *const *parm, const char *name, int def)
Get an integer parameter the list of parameters.
Definition: driver.cpp:76
static const char * GetDriverTypeName(Driver::Type type)
Get the driver type name.
Definition: driver.h:98
static void ShutdownDrivers()
Shuts down all active drivers.
Definition: driver.h:115
Base for all sound drivers.
Type (helpers) for enums.
int priority
The priority of this factory.
Definition: driver.h:67
A music driver, needs to be before sound to properly shut down extmidi forked music players...
Definition: driver.h:43
const char * description
The description of this driver.
Definition: driver.h:69
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:66
Base for all driver factories.
Definition: driver.h:60
A sound driver.
Definition: driver.h:44
bool GetDriverParamBool(const char *const *parm, const char *name)
Get a boolean parameter the list of parameters.
Definition: driver.cpp:64
Helper for iteration.
Definition: driver.h:42
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:40
A driver for communicating with the user.
Definition: driver.h:24
Comparator class for "const char *" so it can be used as a key for std::map.
Type
The type of driver.
Definition: driver.h:41
The base of all video drivers.
A video driver.
Definition: driver.h:45
const char * GetDescription() const
Get a nice description of the driver-class.
Definition: driver.h:130
#define DECLARE_POSTFIX_INCREMENT(type)
Some enums need to have allowed incrementing (i.e.
Definition: enum_type.hpp:16
std::map< const char *, DriverFactoryBase *, StringCompare > Drivers
Type for a map of drivers.
Definition: driver.h:71
Driver for all music playback.
const char * name
The name of the drivers of this factory.
Definition: driver.h:68