OpenTTD Source  1.11.0-beta2
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 "string_type.h"
16 #include <map>
17 
18 const char *GetDriverParam(const StringList &parm, const char *name);
19 bool GetDriverParamBool(const StringList &parm, const char *name);
20 int GetDriverParamInt(const StringList &parm, const char *name, int def);
21 
23 class Driver {
24 public:
30  virtual const char *Start(const StringList &parm) = 0;
31 
35  virtual void Stop() = 0;
36 
37  virtual ~Driver() { }
38 
40  enum Type {
41  DT_BEGIN = 0,
42  DT_MUSIC = 0,
46  };
47 
52  virtual const char *GetName() const = 0;
53 };
54 
56 
57 
58 
60 private:
61  friend class MusicDriver;
62  friend class SoundDriver;
63  friend class VideoDriver;
64 
66  int priority;
67  const char *name;
68  const char *description;
69 
70  typedef std::map<std::string, DriverFactoryBase *> Drivers;
71 
75  static Drivers &GetDrivers()
76  {
77  static Drivers &s_drivers = *new Drivers();
78  return s_drivers;
79  }
80 
87  {
88  static Driver *s_driver[3] = { nullptr, nullptr, nullptr };
89  return &s_driver[type];
90  }
91 
97  static const char *GetDriverTypeName(Driver::Type type)
98  {
99  static const char * const driver_type_name[] = { "music", "sound", "video" };
100  return driver_type_name[type];
101  }
102 
103  static bool SelectDriverImpl(const std::string &name, Driver::Type type);
104 
105 protected:
106  DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description);
107 
108  virtual ~DriverFactoryBase();
109 
110 public:
114  static void ShutdownDrivers()
115  {
116  for (Driver::Type dt = Driver::DT_BEGIN; dt < Driver::DT_END; dt++) {
117  Driver *driver = *GetActiveDriver(dt);
118  if (driver != nullptr) driver->Stop();
119  }
120  }
121 
122  static void SelectDriver(const std::string &name, Driver::Type type);
123  static char *GetDriversInfo(char *p, const char *last);
124 
129  const char *GetDescription() const
130  {
131  return this->description;
132  }
133 
138  virtual Driver *CreateInstance() const = 0;
139 };
140 
141 #endif /* DRIVER_H */
DriverFactoryBase::GetDriverTypeName
static const char * GetDriverTypeName(Driver::Type type)
Get the driver type name.
Definition: driver.h:97
DriverFactoryBase::ShutdownDrivers
static void ShutdownDrivers()
Shuts down all active drivers.
Definition: driver.h:114
DriverFactoryBase::GetActiveDriver
static Driver ** GetActiveDriver(Driver::Type type)
Get the active driver for the given type.
Definition: driver.h:86
Driver::GetName
virtual const char * GetName() const =0
Get the name of this driver.
VideoDriver
The base of all video drivers.
Definition: video_driver.hpp:28
DriverFactoryBase::name
const char * name
The name of the drivers of this factory.
Definition: driver.h:67
DriverFactoryBase::GetDrivers
static Drivers & GetDrivers()
Get the map with drivers.
Definition: driver.h:75
DECLARE_POSTFIX_INCREMENT
#define DECLARE_POSTFIX_INCREMENT(enum_type)
Some enums need to have allowed incrementing (i.e.
Definition: enum_type.hpp:14
DriverFactoryBase::description
const char * description
The description of this driver.
Definition: driver.h:68
DriverFactoryBase::priority
int priority
The priority of this factory.
Definition: driver.h:66
DriverFactoryBase::Drivers
std::map< std::string, DriverFactoryBase * > Drivers
Type for a map of drivers.
Definition: driver.h:70
Driver::Start
virtual const char * Start(const StringList &parm)=0
Start this driver.
DriverFactoryBase::type
Driver::Type type
The type of driver.
Definition: driver.h:65
StringList
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:58
GetDriverParamInt
int GetDriverParamInt(const StringList &parm, const char *name, int def)
Get an integer parameter the list of parameters.
Definition: driver.cpp:71
string_type.h
Driver::Type
Type
The type of driver.
Definition: driver.h:40
Driver::DT_END
@ DT_END
Helper for iteration.
Definition: driver.h:45
Driver::DT_SOUND
@ DT_SOUND
A sound driver.
Definition: driver.h:43
Driver::DT_BEGIN
@ DT_BEGIN
Helper for iteration.
Definition: driver.h:41
string_compare_type.hpp
Driver::DT_VIDEO
@ DT_VIDEO
A video driver.
Definition: driver.h:44
DriverFactoryBase::GetDescription
const char * GetDescription() const
Get a nice description of the driver-class.
Definition: driver.h:129
SoundDriver
Base for all sound drivers.
Definition: sound_driver.hpp:16
enum_type.hpp
GetDriverParamBool
bool GetDriverParamBool(const StringList &parm, const char *name)
Get a boolean parameter the list of parameters.
Definition: driver.cpp:59
MusicDriver
Driver for all music playback.
Definition: music_driver.hpp:18
Driver
A driver for communicating with the user.
Definition: driver.h:23
Driver::DT_MUSIC
@ DT_MUSIC
A music driver, needs to be before sound to properly shut down extmidi forked music players.
Definition: driver.h:42
GetDriverParam
const char * GetDriverParam(const StringList &parm, const char *name)
Get a string parameter the list of parameters.
Definition: driver.cpp:39
Driver::Stop
virtual void Stop()=0
Stop this driver.
DriverFactoryBase
Base for all driver factories.
Definition: driver.h:59