]> git.lyx.org Git - lyx.git/blobdiff - src/graphics/GraphicsCache.C
The std::string mammoth path.
[lyx.git] / src / graphics / GraphicsCache.C
index cda36db9fc6b13059ce32149e82567222408a171..124141604e3fa115fb0f642d0e9b0ef48d892735 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
- * ================================================= */
+ * \author Baruch Even
+ * \author Angus Leeming
+ *
+ * Full author contact details are available in file CREDITS.
+ */
 
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
 #include "GraphicsCache.h"
 #include "GraphicsCacheItem.h"
+#include "GraphicsImage.h"
+
+#include "debug.h"
+
+#include "support/filetools.h"
+
+namespace support = lyx::support;
+
+using std::string;
+
+
+namespace lyx {
+namespace graphics {
 
-#include "support/LAssert.h"
+/** The cache contains one item per file, so use a map to find the
+ *  cache item quickly by filename.
+ */
+typedef std::map<string, Cache::ItemPtr> CacheType;
 
-GraphicsCache &
-GraphicsCache::getInstance()
+struct Cache::Impl {
+       ///
+       CacheType cache;
+};
+
+
+Cache & Cache::get()
 {
-       static GraphicsCache singleton;
+       // Now return the cache
+       static Cache singleton;
        return singleton;
 }
 
 
-GraphicsCache::~GraphicsCache()
+Cache::Cache()
+       : pimpl_(new Impl)
+{}
+
+
+Cache::~Cache()
+{}
+
+
+std::vector<string> Cache::loadableFormats() const
 {
-       // The map elements should have already been eliminated.
-       Assert(cache.empty());
+       return Image::loadableFormats();
 }
 
 
-GraphicsCache::shared_ptr_item
-GraphicsCache::addFile(string const & filename)
+void Cache::add(string const & file) const
 {
-       CacheType::iterator it = cache.find(filename);
-       
-       if (it != cache.end()) {
-               return (*it).second;
+       if (!support::AbsolutePath(file)) {
+               lyxerr << "Cache::add(" << file << "):\n"
+                      << "The file must be have an absolute path."
+                      << std::endl;
+               return;
+       }
+
+       // 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 Cache::remove(string const & file) const
+{
+       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);
        }
-       
-       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 const & filename)
+bool Cache::inCache(string const & file) const
 {
-       // We do not destroy the GraphicsCacheItem since we are here because
-       // the last copy of it is being erased.
+       return pimpl_->cache.find(file) != pimpl_->cache.end();
+}
+
 
-       CacheType::iterator it = cache.find(filename);
-       if (it != cache.end())
-               cache.erase(it);
+Cache::ItemPtr const Cache::item(string 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