]> 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 534c984760c8bf6f330cf879fb13b89729e20f3d..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;
-               Assert(singleton != 0);
-       }
-
+       static GraphicsCache singleton;
        return singleton;
 }
 
 
 GraphicsCache::~GraphicsCache()
 {
-       // Free the map.
-       //std::foreach(map.begin(), map.end(), ...);
-#warning This is a bogus reason to not clean up after your self. (Lgb)
-       // Clean up and be done with it. (Lgb)
-       
-       // This is not really needed, it will only happen on program close and in
-       // any case the OS will release those resources (not doing it may have 
-       // a good effect on closing time).
-       
-       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::iterator it = cache.find(filename);
        
        if (it != cache.end()) {
-               return new GraphicsCacheItem( *((*it).second) );
+               return it->second;
        }
        
-       GraphicsCacheItem * cacheItem = new GraphicsCacheItem();
-       if (cacheItem == 0)
-               return 0;
-       
-       cacheItem->setFilename(filename);
+       shared_ptr_item cacheItem(new GraphicsCacheItem(filename));
+       if (cacheItem.get() == 0)
+               return cacheItem;
        
        cache[filename] = cacheItem;
-       
-       // We do not want to return the main cache object, otherwise when the
-       // will destroy their copy they will destroy the main copy.
-       return new GraphicsCacheItem( *cacheItem );
+
+       // GraphicsCacheItem_ptr is a shared_ptr and thus reference counted,
+       // it is safe to return it directly.
+       return cacheItem;
 }
 
 
@@ -78,6 +61,7 @@ GraphicsCache::removeFile(string const & filename)
        // We do not destroy the GraphicsCacheItem since we are here because
        // the last copy of it is being erased.
 
-       if (cache.find(filename) != cache.end())
-               cache.erase(filename);
+       CacheType::iterator it = cache.find(filename);
+       if (it != cache.end())
+               cache.erase(it);
 }