driver.h

Go to the documentation of this file.
00001 /* $Id: driver.h 26544 2014-04-29 18:41:19Z frosch $ */
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   friend class MusicDriver;
00063   friend class SoundDriver;
00064   friend class VideoDriver;
00065 
00066   Driver::Type type;       
00067   int priority;            
00068   const char *name;        
00069   const char *description; 
00070 
00071   typedef std::map<const char *, DriverFactoryBase *, StringCompare> Drivers; 
00072 
00076   static Drivers &GetDrivers()
00077   {
00078     static Drivers &s_drivers = *new Drivers();
00079     return s_drivers;
00080   }
00081 
00087   static Driver **GetActiveDriver(Driver::Type type)
00088   {
00089     static Driver *s_driver[3] = { NULL, NULL, NULL };
00090     return &s_driver[type];
00091   }
00092 
00098   static const char *GetDriverTypeName(Driver::Type type)
00099   {
00100     static const char * const driver_type_name[] = { "music", "sound", "video" };
00101     return driver_type_name[type];
00102   }
00103 
00104   static bool SelectDriverImpl(const char *name, Driver::Type type);
00105 
00106 protected:
00107   DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description);
00108 
00109   virtual ~DriverFactoryBase();
00110 
00111 public:
00115   static void ShutdownDrivers()
00116   {
00117     for (Driver::Type dt = Driver::DT_BEGIN; dt < Driver::DT_END; dt++) {
00118       Driver *driver = *GetActiveDriver(dt);
00119       if (driver != NULL) driver->Stop();
00120     }
00121   }
00122 
00123   static void SelectDriver(const char *name, Driver::Type type);
00124   static char *GetDriversInfo(char *p, const char *last);
00125 
00130   const char *GetDescription() const
00131   {
00132     return this->description;
00133   }
00134 
00139   virtual Driver *CreateInstance() const = 0;
00140 };
00141 
00142 #endif /* DRIVER_H */