driver.h

Go to the documentation of this file.
00001 /* $Id: driver.h 26108 2013-11-25 14:30:22Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #ifndef DRIVER_H
00013 #define DRIVER_H
00014 
00015 #include "core/enum_type.hpp"
00016 #include "core/string_compare_type.hpp"
00017 #include <map>
00018 
00019 const char *GetDriverParam(const char * const *parm, const char *name);
00020 bool GetDriverParamBool(const char * const *parm, const char *name);
00021 int GetDriverParamInt(const char * const *parm, const char *name, int def);
00022 
00024 class Driver {
00025 public:
00031   virtual const char *Start(const char * const *parm) = 0;
00032 
00036   virtual void Stop() = 0;
00037 
00038   virtual ~Driver() { }
00039 
00041   enum Type {
00042     DT_BEGIN = 0, 
00043     DT_MUSIC = 0, 
00044     DT_SOUND,     
00045     DT_VIDEO,     
00046     DT_END,       
00047   };
00048 
00053   virtual const char *GetName() const = 0;
00054 };
00055 
00056 DECLARE_POSTFIX_INCREMENT(Driver::Type)
00057 
00058 
00059 
00060 class DriverFactoryBase {
00061 private:
00062   Driver::Type type;       
00063   int priority;            
00064   const char *name;        
00065   const char *description; 
00066 
00067   typedef std::map<const char *, DriverFactoryBase *, StringCompare> Drivers; 
00068 
00072   static Drivers &GetDrivers()
00073   {
00074     static Drivers &s_drivers = *new Drivers();
00075     return s_drivers;
00076   }
00077 
00083   static Driver **GetActiveDriver(Driver::Type type)
00084   {
00085     static Driver *s_driver[3] = { NULL, NULL, NULL };
00086     return &s_driver[type];
00087   }
00088 
00094   static const char *GetDriverTypeName(Driver::Type type)
00095   {
00096     static const char * const driver_type_name[] = { "music", "sound", "video" };
00097     return driver_type_name[type];
00098   }
00099 
00100 protected:
00101   DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description);
00102 
00103   virtual ~DriverFactoryBase();
00104 
00105 public:
00109   static void ShutdownDrivers()
00110   {
00111     for (Driver::Type dt = Driver::DT_BEGIN; dt < Driver::DT_END; dt++) {
00112       Driver *driver = *GetActiveDriver(dt);
00113       if (driver != NULL) driver->Stop();
00114     }
00115   }
00116 
00117   static Driver *SelectDriver(const char *name, Driver::Type type);
00118   static char *GetDriversInfo(char *p, const char *last);
00119 
00124   const char *GetDescription() const
00125   {
00126     return this->description;
00127   }
00128 
00133   virtual Driver *CreateInstance() const = 0;
00134 };
00135 
00136 #endif /* DRIVER_H */