]> git.lyx.org Git - lyx.git/blobdiff - src/graphics/GraphicsCache.C
remove more forms.h cruft
[lyx.git] / src / graphics / GraphicsCache.C
index a52e7291f33937a264413a7bb50d2c6aa23156d7..fe2de807c940174056b3a7564b71faff8be744c2 100644 (file)
@@ -1,51 +1,66 @@
-// -*- C++ -*-
 /* This file is part of
  * =================================================
  * 
  *          LyX, The Document Processor
  *          Copyright 1995 Matthias Ettrich.
- *          Copyright 1995-2000 The LyX Team.
+ *          Copyright 1995-2001 The LyX Team.
  *
  *          This file Copyright 2000 Baruch Even
  * ================================================= */
 
+#include <config.h>
+
 #ifdef __GNUG__
 #pragma implementation
 #endif
 
-#include <config.h>
 #include "GraphicsCache.h"
+#include "GraphicsCacheItem.h"
 
-GraphicsCache * 
+#include "support/LAssert.h"
+
+GraphicsCache &
 GraphicsCache::getInstance()
 {
-    if (! singleton) {
-        singleton = new GraphicsCache;
-    }
+       static GraphicsCache singleton;
+       return singleton;
+}
 
-    return singleton;
+
+GraphicsCache::~GraphicsCache()
+{
+       // All elements are destroyed by the shared_ptr's in the map.
 }
 
 
-GraphicsCacheItem * 
-GraphicsCache::addFile(string filename)
+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 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.
+
+       CacheType::iterator it = cache.find(filename);
+       if (it != cache.end())
+               cache.erase(it);
 }