-// -*- 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"
+
+#include <map>
+
+using std::string;
+
+
+namespace lyx {
-#include "support/LAssert.h"
+using support::FileName;
-GraphicsCache &
-GraphicsCache::getInstance()
+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()
{
- 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
{
- // All elements are destroyed by the shared_ptr's in the map.
+ return Image::loadableFormats();
+}
+
+
+void Cache::add(FileName const & file) const
+{
+ // 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));
}
-GraphicsCache::shared_ptr_item
-GraphicsCache::addFile(string const & filename)
+void Cache::remove(FileName const & file) const
{
- CacheType::iterator it = cache.find(filename);
-
- if (it != cache.end()) {
- return (*it).second;
+ 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(FileName 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(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