# driver.h

Go to the documentation of this file.

    00001 /* $Id$ */
    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
    00023 class Driver {
    00024 public:
    00025   virtual const char *Start(const char * const *parm) = 0;
    00026
    00027   virtual void Stop() = 0;
    00028
    00029   virtual ~Driver() { }
    00030
    00032   enum Type {
    00033     DT_BEGIN = 0,
    00034     DT_MUSIC = 0,
    00035     DT_SOUND,
    00036     DT_VIDEO,
    00037     DT_END,
    00038   };
    00039
    00040   virtual const char *GetName() const = 0;
    00041 };
    00042
    00043 DECLARE_POSTFIX_INCREMENT(Driver::Type)
    00044
    00045
    00046 class DriverFactoryBase {
    00047 private:
    00048   Driver::Type type;
    00049   const char *name;
    00050   int priority;
    00051
    00052   typedef std::map<const char *, DriverFactoryBase *, StringCompare> Drivers;
    00053
    00054   static Drivers &GetDrivers()
    00055   {
    00056     static Drivers &s_drivers = *new Drivers();
    00057     return s_drivers;
    00058   }
    00059
    00060   static Driver **GetActiveDriver(Driver::Type type)
    00061   {
    00062     static Driver *s_driver[3] = { NULL, NULL, NULL };
    00063     return &s_driver[type];
    00064   }
    00065
    00066   static const char *GetDriverTypeName(Driver::Type type)
    00067   {
    00068     static const char * const driver_type_name[] = { "music", "sound", "video" };
    00069     return driver_type_name[type];
    00070   }
    00071
    00072 protected:
    00073   void RegisterDriver(const char *name, Driver::Type type, int priority);
    00074
    00075 public:
    00076   DriverFactoryBase() :
    00077     name(NULL)
    00078   {}
    00079
    00080   virtual ~DriverFactoryBase();
    00081
    00085   static void ShutdownDrivers()
    00086   {
    00087     for (Driver::Type dt = Driver::DT_BEGIN; dt < Driver::DT_END; dt++) {
    00088       Driver *driver = *GetActiveDriver(dt);
    00089       if (driver != NULL) driver->Stop();
    00090     }
    00091   }
    00092
    00093   static Driver *SelectDriver(const char *name, Driver::Type type);
    00094   static char *GetDriversInfo(char *p, const char *last);
    00095
    00099   virtual const char *GetDescription() = 0;
    00100
    00104   virtual Driver *CreateInstance() = 0;
    00105 };
    00106
    00107 #endif /* DRIVER_H */