]> git.lyx.org Git - lyx.git/blobdiff - src/graphics/GraphicsCache.C
prepare for 1.1.6pre2
[lyx.git] / src / graphics / GraphicsCache.C
index a52e7291f33937a264413a7bb50d2c6aa23156d7..af2233cfb8784e4206a9caee5ede1de3e0256251 100644 (file)
@@ -9,43 +9,75 @@
  *          This file Copyright 2000 Baruch Even
  * ================================================= */
 
+#include <config.h>
+
 #ifdef __GNUG__
 #pragma implementation
 #endif
 
-#include <config.h>
 #include "GraphicsCache.h"
 
+#include "support/LAssert.h"
+
+GraphicsCache * GraphicsCache::singleton = 0;
+
+
 GraphicsCache * 
 GraphicsCache::getInstance()
 {
-    if (! singleton) {
-        singleton = new GraphicsCache;
-    }
+       if (!singleton) {
+               singleton = new GraphicsCache;
+               Assert(singleton != 0);
+       }
 
-    return singleton;
+       return singleton;
 }
 
 
-GraphicsCacheItem * 
-GraphicsCache::addFile(string filename)
+GraphicsCache::~GraphicsCache()
 {
-    CacheType::const_iterator it = cache.find(filename);
-    
-    if (it != cache.end()) {
-        return (*it).second;
-    }
-    // INCOMPLETE!
-    return 0;
+       // Free the map.
+       //std::foreach(map.begin(), map.end(), ...);
+//#warning This is a bogus reason to not clean up after your self. (Lgb)
+       // TODO: Clean up here (BE)
+       
+       // 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;
 }
 
+
+GraphicsCacheItem *
+GraphicsCache::addFile(string const & filename)
+{
+       CacheType::iterator it = cache.find(filename);
+       
+       if (it != cache.end()) {
+               return new GraphicsCacheItem( *((*it).second) );
+       }
+       
+       GraphicsCacheItem * cacheItem = new GraphicsCacheItem();
+       if (cacheItem == 0)
+               return 0;
+       
+       cacheItem->setFilename(filename);
+       
+       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 );
+}
+
+
 void
-GraphicsCache::removeFile(string filename)
+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.
+
+       if (cache.find(filename) != cache.end())
+               cache.erase(filename);
 }