-// -*- 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 "support/LAssert.h"
+#include "debug.h"
-GraphicsCache * GraphicsCache::singleton = 0;
+#include "support/filetools.h"
+#include <map>
-GraphicsCache *
-GraphicsCache::getInstance()
-{
- if (!singleton) {
- singleton = new GraphicsCache;
- Assert(singleton != 0);
- }
+using std::string;
+
+
+namespace lyx {
+
+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()
+{
+ // Now return the cache
+ static Cache singleton;
return singleton;
}
-GraphicsCache::~GraphicsCache()
+Cache::Cache()
+ : pimpl_(new Impl)
+{}
+
+
+Cache::~Cache()
+{}
+
+
+std::vector<string> Cache::loadableFormats() const
{
- // Free the map.
- //std::foreach(map.begin(), map.end(), ...);
-//#warning This is a bogus reason to not clean up after your self. (Lgb)
- // TODO: Clean up here (BE)
-
- // This is not really needed, it will only happen on program close and in
- // any case the OS will release those resources (not doing it may have
- // a good effect on closing time).
-
- delete singleton;
+ 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));
}
-GraphicsCacheItem *
-GraphicsCache::addFile(string const & filename)
+void Cache::remove(FileName const & file) const
{
- CacheType::iterator it = cache.find(filename);
-
- if (it != cache.end()) {
- return new GraphicsCacheItem( *((*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);
}
-
- GraphicsCacheItem * cacheItem = new GraphicsCacheItem();
- if (cacheItem == 0)
- return 0;
-
- cacheItem->setFilename(filename);
-
- 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 );
}
-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();
+}
+
- if (cache.find(filename) != cache.end())
- cache.erase(filename);
+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