]> git.lyx.org Git - lyx.git/blobdiff - src/graphics/GraphicsCache.C
small fix with footnote, use stringstream some more
[lyx.git] / src / graphics / GraphicsCache.C
index 1721b0c6f38835ca056c95da816d4b7daf9d77be..93c3f9a822d3db868a1a0081bf0e48f82eb33c05 100644 (file)
 #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);
 }