00001 #ifndef RESSOURCEMANAGER_H 00002 #define RESSOURCEMANAGER_H 00003 00004 #include <map> 00005 #include <string> 00006 #include <iostream> 00007 00008 class Ressource; 00009 00010 using namespace std; 00011 00012 class RessourceManager{ 00013 private: 00014 RessourceManager(); 00015 ~RessourceManager(); 00016 static RessourceManager* instance; 00017 map<string,Ressource*> ressourceRegistery; 00018 00019 template< class T > 00020 T* createAndLoad(const string& path){ 00021 T* newRessource = new T(); 00022 newRessource->load(path); 00023 return newRessource; 00024 } 00025 00026 public: 00027 00028 static RessourceManager* getInstance(){ 00029 if(!instance){ 00030 try{ 00031 instance = new RessourceManager(); 00032 }catch( char * str ) { 00033 cout << "Exception raised: " << str << '\n'; 00034 } 00035 } 00036 return instance; 00037 }; 00038 00039 template< class T > 00040 T* loadRessource(const string& path){ 00041 map<string, Ressource*>::iterator resIt = ressourceRegistery.find(path); 00042 00043 if( resIt != ressourceRegistery.end() ) 00044 return static_cast<T*>( resIt->second ); 00045 else{ 00046 Ressource* newRessource = createAndLoad<T>( path ); 00047 ressourceRegistery.insert( pair<string,Ressource*>(path,newRessource) ); 00048 return static_cast<T*>(newRessource); 00049 } 00050 } 00051 }; 00052 00053 #endif 00054