#endif
#include "GraphicsCache.h"
+#include "GraphicsCacheItem.h"
+#include "support/LAssert.h"
-GraphicsCache * GraphicsCache::singleton = 0;
-
-
-GraphicsCache *
+GraphicsCache &
GraphicsCache::getInstance()
{
- if (! singleton) {
- singleton = new GraphicsCache;
- }
-
- return singleton;
+ static GraphicsCache singleton;
+ return singleton;
}
GraphicsCache::~GraphicsCache()
{
- delete singleton;
+ // All elements are destroyed by the shared_ptr's in the map.
}
-GraphicsCacheItem *
+GraphicsCache::shared_ptr_item
GraphicsCache::addFile(string const & filename)
{
- CacheType::const_iterator it = cache.find(filename);
-
- if (it != cache.end()) {
- return (*it).second;
- }
- // INCOMPLETE!
- return 0;
+ CacheType::iterator it = cache.find(filename);
+
+ if (it != cache.end()) {
+ return it->second;
+ }
+
+ shared_ptr_item cacheItem(new GraphicsCacheItem(filename));
+ if (cacheItem.get() == 0)
+ return cacheItem;
+
+ cache[filename] = cacheItem;
+
+ // GraphicsCacheItem_ptr is a shared_ptr and thus reference counted,
+ // it is safe to return it directly.
+ return cacheItem;
}
void
GraphicsCache::removeFile(string const & filename)
{
- CacheType::const_iterator it = cache.find(filename);
-
- if (it != cache.end()) {
- // INCOMPLETE!
-// cache.erase(it);
- }
+ // We do not destroy the GraphicsCacheItem since we are here because
+ // the last copy of it is being erased.
+
+ CacheType::iterator it = cache.find(filename);
+ if (it != cache.end())
+ cache.erase(it);
}