]> git.lyx.org Git - lyx.git/blobdiff - src/graphics/GraphicsCache.C
* src/LaTeX.C
[lyx.git] / src / graphics / GraphicsCache.C
index a52e7291f33937a264413a7bb50d2c6aa23156d7..7201c7bfbfb6295429ae6f6b503b2273b7d2b572 100644 (file)
-// -*- C++ -*-
-/* This file is part of
- * =================================================
- * 
- *          LyX, The Document Processor
- *          Copyright 1995 Matthias Ettrich.
- *          Copyright 1995-2000 The LyX Team.
+/**
+ * \file GraphicsCache.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- *          This file Copyright 2000 Baruch Even
- * ================================================= */
-
-#ifdef __GNUG__
-#pragma implementation
-#endif
+ * \author Baruch Even
+ * \author Angus Leeming
+ *
+ * Full author contact details are available in file CREDITS.
+ */
 
 #include <config.h>
+
 #include "GraphicsCache.h"
+#include "GraphicsCacheItem.h"
+#include "GraphicsImage.h"
+
+#include "debug.h"
+
+#include "support/filetools.h"
+
+#include <map>
+
+using std::string;
+
+
+namespace lyx {
 
-GraphicsCache * 
-GraphicsCache::getInstance()
+using support::FileName;
+
+namespace graphics {
+
+/** The cache contains one item per file, so use a map to find the
+ *  cache item quickly by filename.
+ */
+typedef std::map<FileName, Cache::ItemPtr> CacheType;
+
+class Cache::Impl {
+public:
+       ///
+       CacheType cache;
+};
+
+
+Cache & Cache::get()
 {
-    if (! singleton) {
-        singleton = new GraphicsCache;
-    }
+       // Now return the cache
+       static Cache singleton;
+       return singleton;
+}
+
+
+Cache::Cache()
+       : pimpl_(new Impl)
+{}
+
 
-    return singleton;
+Cache::~Cache()
+{}
+
+
+std::vector<string> Cache::loadableFormats() const
+{
+       return Image::loadableFormats();
 }
 
 
-GraphicsCacheItem * 
-GraphicsCache::addFile(string filename)
+void Cache::add(FileName const & file) const
 {
-    CacheType::const_iterator it = cache.find(filename);
-    
-    if (it != cache.end()) {
-        return (*it).second;
-    }
-    // INCOMPLETE!
-    return 0;
+       // Is the file in the cache already?
+       if (inCache(file)) {
+               lyxerr[Debug::GRAPHICS] << "Cache::add(" << file << "):\n"
+                                       << "The file is already in the cache."
+                                       << std::endl;
+               return;
+       }
+
+       pimpl_->cache[file] = ItemPtr(new CacheItem(file));
 }
 
-void
-GraphicsCache::removeFile(string filename)
+
+void Cache::remove(FileName const & file) const
 {
-    CacheType::const_iterator it = cache.find(filename);
-    
-    if (it != cache.end()) {
-        // INCOMPLETE!
-//        cache.erase(it);
-    }
+       CacheType::iterator it = pimpl_->cache.find(file);
+       if (it == pimpl_->cache.end())
+               return;
+
+       ItemPtr & item = it->second;
+
+       if (item.use_count() == 1) {
+               // The graphics file is in the cache, but nothing else
+               // references it.
+               pimpl_->cache.erase(it);
+       }
 }
+
+
+bool Cache::inCache(FileName const & file) const
+{
+       return pimpl_->cache.find(file) != pimpl_->cache.end();
+}
+
+
+Cache::ItemPtr const Cache::item(FileName const & file) const
+{
+       CacheType::const_iterator it = pimpl_->cache.find(file);
+       if (it == pimpl_->cache.end())
+               return ItemPtr();
+
+       return it->second;
+}
+
+} // namespace graphics
+} // namespace lyx