-// -*- 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;
-#include "support/LAssert.h"
-GraphicsCache * GraphicsCache::singleton = 0;
+namespace lyx {
+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()
{
- if (! singleton) {
- singleton = new GraphicsCache;
- Assert(singleton != 0);
- }
+ // Now return the cache
+ static Cache singleton;
+ return singleton;
+}
+
- return singleton;
+Cache::Cache()
+ : pimpl_(new Impl)
+{}
+
+
+Cache::~Cache()
+{}
+
+
+std::vector<string> Cache::loadableFormats() const
+{
+ return Image::loadableFormats();
}
-GraphicsCache::~GraphicsCache()
+void Cache::add(FileName const & file) const
{
- // Free the map.
- //std::foreach(map.begin(), map.end(), ...);
- // 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).
+ // 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;
+ }
- delete singleton;
+ pimpl_->cache[file] = ItemPtr(new CacheItem(file));
}
-GraphicsCacheItem *
-GraphicsCache::addFile(string const & filename)
+void Cache::remove(FileName const & file) const
{
- CacheType::const_iterator it = cache.find(filename);
-
- if (it != cache.end()) {
- return (*it).second;
- }
-
- GraphicsCacheItem * cacheItem = new GraphicsCacheItem();
- if (cacheItem == 0) {
- return 0;
+ 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 result = cacheItem->setFilename(filename);
- if (!result)
- return 0;
- cache[filename] = cacheItem;
-
- return cacheItem;
+bool Cache::inCache(FileName const & file) const
+{
+ return pimpl_->cache.find(file) != pimpl_->cache.end();
}
-void
-GraphicsCache::removeFile(string const & filename)
+Cache::ItemPtr const Cache::item(FileName const & file) const
{
- CacheType::const_iterator it = cache.find(filename);
-
- if (it != cache.end()) {
- // INCOMPLETE!
-// cache.erase(it);
- }
+ CacheType::const_iterator it = pimpl_->cache.find(file);
+ if (it == pimpl_->cache.end())
+ return ItemPtr();
+
+ return it->second;
}
+
+} // namespace graphics
+} // namespace lyx