ResourceManager.h
Go to the documentation of this file.00001 /**<!--------------------------------------------------------------------> 00002 @class ResourceManager 00003 @author Travis Fischer (fisch0920@gmail.com) 00004 @author Matthew Jacobs (jacobs.mh@gmail.com) 00005 @date Fall 2008 00006 00007 @brief 00008 Static resource manager synchronized across all Milton threads, 00009 containing references to loaded images, global logging utilities, and 00010 user-definable options which may be used to affect Milton functionality. 00011 Many user-definable options that are supported are aimed at dynamic, 00012 on-the-fly debugging changes (whether or not to preview kd-Trees built 00013 from Meshes, for instance) 00014 <!-------------------------------------------------------------------->**/ 00015 00016 #ifndef RESOURCE_MANAGER_H_ 00017 #define RESOURCE_MANAGER_H_ 00018 00019 #include <utils/PropertyMap.h> 00020 #include <common/image/Image.h> 00021 #include <utils/Log.h> 00022 00023 #include <QThreadStorage> 00024 #include <QMutex> 00025 00026 DECLARE_STL_TYPEDEF2(std::map<std::string, ImagePtr>, ImagePtrMap); 00027 00028 typedef QThreadStorage<PropertyMap*> ThreadLocalStorage; 00029 00030 class SharedLibraryManager; 00031 class SharedLibraryPlatform; 00032 00033 class ResourceManager { 00034 public: 00035 ///@name External image cache 00036 //@{----------------------------------------------------------------- 00037 00038 /** 00039 * @brief 00040 * Loads the requested image from the given file, retaining a 00041 * global cache of all previously loaded images to default to 00042 * upon future calls to 'getImage' 00043 * 00044 * @returns a reference to an Image upon success or an empty 00045 * reference upon failure 00046 */ 00047 static ImagePtr getImage(const std::string &filename) { 00048 QMutexLocker lock(&s_mutex); 00049 00050 // attempt to find previously loaded, image in cache 00051 ImagePtrMapIter iter = s_imagePtrMap.find(filename); 00052 00053 if (iter != s_imagePtrMap.end()) 00054 return iter->second; 00055 00056 ImagePtr imagePtr = ImagePtr(Image::load(filename)); 00057 00058 if (!imagePtr) { 00059 ResourceManager::log.error << "failed to load image '" << 00060 filename << "'" << std::endl; 00061 } 00062 00063 s_imagePtrMap.insert(ImagePtrMap::value_type(filename, imagePtr)); 00064 return imagePtr; 00065 } 00066 00067 /** 00068 * @brief 00069 * Attempts to synchronously clean up all resources which are not 00070 * currently in use, flushing the static cache of any currently 00071 * unused images. 00072 */ 00073 static void cleanup(); 00074 00075 00076 //@}----------------------------------------------------------------- 00077 ///@name Thread-local resources map 00078 //@{----------------------------------------------------------------- 00079 00080 static bool containsThreadLocal(const std::string &key) { 00081 const PropertyMap *properties = 00082 (const PropertyMap *) s_threadLocalStorage.localData(); 00083 00084 if (NULL == properties) 00085 return false; 00086 00087 return properties->contains(key); 00088 } 00089 00090 template <typename T> 00091 static void insertThreadLocal(const std::string &key, const T &value) { 00092 PropertyMap *properties = _getThreadLocalProperties(); 00093 ASSERT(properties); 00094 00095 properties->insert<T>(key, value); 00096 ASSERT(properties->getValue<T>(key) == value); 00097 } 00098 00099 template <typename T> 00100 static T &getValueThreadLocal(const std::string &key) { 00101 PropertyMap *properties = _getThreadLocalProperties(); 00102 ASSERT(properties); 00103 00104 return properties->getValue<T>(key); 00105 } 00106 00107 template <typename T> 00108 static T &getValueThreadLocal(const std::string &key, const T &defaultValue) { 00109 PropertyMap *properties = _getThreadLocalProperties(); 00110 ASSERT(properties); 00111 00112 return properties->getValue<T>(key, defaultValue); 00113 } 00114 00115 00116 //@}----------------------------------------------------------------- 00117 ///@name Global logging interface 00118 //@{----------------------------------------------------------------- 00119 00120 static Log log; 00121 00122 00123 //@}----------------------------------------------------------------- 00124 ///@name Global user-definable options 00125 //@{----------------------------------------------------------------- 00126 00127 static bool contains(const std::string &key) { 00128 QMutexLocker lock(&s_mutex); 00129 00130 return s_propertyMap.contains(key); 00131 } 00132 00133 template <typename T> 00134 static void insert(const std::string &key, const T &value) { 00135 QMutexLocker lock(&s_mutex); 00136 00137 s_propertyMap.insert<T>(key, value); 00138 } 00139 00140 template <typename T> 00141 static T &getValue(const std::string &key) { 00142 QMutexLocker lock(&s_mutex); 00143 00144 return s_propertyMap.getValue<T>(key); 00145 } 00146 00147 template <typename T> 00148 static T &getValue(const std::string &key, const T &defaultValue) { 00149 QMutexLocker lock(&s_mutex); 00150 00151 return s_propertyMap.getValue<T>(key, defaultValue); 00152 } 00153 00154 00155 //@}----------------------------------------------------------------- 00156 ///@name Global external shared library manager (for plugins) 00157 //@{----------------------------------------------------------------- 00158 00159 static SharedLibraryManager *manager; 00160 00161 00162 //@}----------------------------------------------------------------- 00163 00164 private: 00165 static PropertyMap *_getThreadLocalProperties() { 00166 PropertyMap *properties = (PropertyMap *) s_threadLocalStorage.localData(); 00167 00168 if (NULL == properties) { 00169 properties = new PropertyMap(); 00170 s_threadLocalStorage.setLocalData(properties); 00171 } 00172 00173 return properties; 00174 } 00175 00176 private: 00177 static ImagePtrMap s_imagePtrMap; 00178 static ThreadLocalStorage s_threadLocalStorage; 00179 static PropertyMap s_propertyMap; 00180 static QMutex s_mutex; 00181 static SharedLibraryPlatform *s_sharedLibraryPlatform; 00182 }; 00183 00184 #endif // RESOURCE_MANAGER_H_ 00185
Generated on 28 Feb 2009 for Milton by
1.5.6