]> git.lyx.org Git - lyx.git/blobdiff - src/graphics/GraphicsCache.C
remove more forms.h cruft
[lyx.git] / src / graphics / GraphicsCache.C
index a2956ddf6e12091584b365cacc58b0c9905754b3..fe2de807c940174056b3a7564b71faff8be744c2 100644 (file)
@@ -1,10 +1,9 @@
-// -*- 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
  * ================================================= */
 #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()
 {
-       // The map elements should have already been eliminated.
-       Assert(cache.empty());
-       
-       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;
 }