Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

CSLoadable.h

Go to the documentation of this file.
00001 #ifdef WIN32
00002 #pragma warning(disable : 4786 )
00003 #endif
00004 
00005 #ifndef CSLOADABLE_H
00006 #define CSLOADABLE_H
00007 
00008 #include <string>
00009 #include <map>
00010 //#include "CSUtility.h"
00011 #include "CSLog.h"
00012 
00013 /**
00014     A template class for loadable objects. These might be any
00015     kind of data loaded from somewhere.
00016 
00017     This class encapsulates the functionality of loading
00018     a needed resource only once. Upon a second call of the loader
00019     it gives back an allready loaded resource.
00020 
00021     A MUST for this to work is that this class is also responsible for
00022     freeing the (only) once loaded resources.
00023 
00024     If a resource is a
00025         virtual bool isSingleCreate(void) const;
00026     the resource will be put in a map, where it can be found later on for reuse.
00027     (map key is the filename of the resource)
00028     
00029     The object, that is created using this class (via a filename) must
00030     provide a 
00031         virtual T *create(const std::string &filename) = 0;
00032     -method.
00033 
00034     Per default an object is destroyed using c++ "delete".
00035     But the destroy-method can be overloaded by children!
00036 
00037   */
00038 
00039 template <class T>
00040 class Loadable
00041 {
00042     private:
00043         std::map<std::string, T *> mMap;
00044     protected:
00045         virtual std::map<std::string, T *> *getMap() {return &mMap;}
00046         virtual void destroy(T *object);
00047         virtual bool isSingleCreate(void) const;
00048         virtual T *create(const std::string &filename) = 0;
00049         Loadable()
00050         {
00051             mMap.clear();
00052         }
00053 
00054     public:
00055 
00056         virtual T *load(const std::string &filename);
00057 
00058         virtual void unload(T *image);
00059         virtual void quit();    //!< unloads all knonw (singleton) 
00060                                 //!< instances of this loader class
00061 
00062         virtual ~Loadable()
00063         {
00064             quit();
00065         }
00066 };
00067 
00068 //! only produce singletons (and add to mapping)
00069 //! if "isSingleCreate()" return true (default)
00070 template <class T>
00071 T *Loadable<T>::load(const std::string &filename)
00072 {
00073     if (isSingleCreate())
00074     {
00075         LOG_MESSAGE((LOG_LEVEL_DEBUG),"Getting file from map: " + filename)
00076         std::map<std::string, T *>::iterator iter = mMap.find(filename.c_str());
00077         if (iter != mMap.end())
00078         {
00079             return iter->second;
00080         }
00081     }
00082     
00083     LOG_MESSAGE((LOG_LEVEL_DEBUG),"Loading file from disk: " + filename)
00084     T *anObject = create(filename.c_str());
00085     if (isSingleCreate())
00086     {
00087         mMap.insert(std::map<std::string, T *>::value_type(filename.c_str(), anObject));
00088     }
00089     return anObject;
00090 }
00091 
00092 
00093 template <class T>
00094 void Loadable<T>::unload(T *anObject)
00095 {
00096     //! delete single object
00097     std::map<std::string, T *>::iterator iter;
00098     for (iter = mMap.begin(); iter!=mMap.end(); iter++)
00099     {
00100         if (iter->second == anObject)
00101         {
00102             destroy(iter->second);
00103             mMap.erase(iter);
00104             return;
00105         }
00106     }
00107     SDLMain::shutdown((std::string)"Couldn't properly delete map object." + SDL_GetError(), 2);
00108 }
00109 
00110 template <class T>
00111 void Loadable<T>::quit()
00112 {
00113     // delete all object
00114     std::map<std::string, T *>::iterator iter;
00115     for (iter = mMap.begin(); iter!=mMap.end(); iter = mMap.begin())
00116     {
00117         unload(iter->second);
00118     }
00119     mMap.clear();
00120 }
00121 
00122 template <class T>
00123 void Loadable<T>::destroy(T *object)
00124 {
00125     delete object;
00126 }
00127 
00128 template <class T>
00129 bool Loadable<T>::isSingleCreate(void) const
00130 {
00131     return true;
00132 }
00133 
00134 #endif //CSLOADABLE_H

Generated on Wed Jul 14 00:43:30 2004 for CSLib by doxygen 1.3.6