-/*
+/**
* \file GraphicsCache.C
- * Copyright 2002 the LyX Team
- * Read the file COPYING
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
*
- * \author Baruch Even <baruch.even@writeme.com>
- * \author Angus Leeming <a.leeming@ic.ac.uk>
+ * \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/filetools.h"
-#include "frontends/lyx_gui.h"
+#include <map>
-namespace grfx {
+using std::string;
-GCache & GCache::get()
-{
- static bool start = true;
- if (start) {
- start = false;
- lyx_gui::init_graphics();
- }
+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 GCache singleton;
+ static Cache singleton;
return singleton;
}
-GCache::GCache()
-{
- cache = new CacheType;
-}
+Cache::Cache()
+ : pimpl_(new Impl)
+{}
-// all elements are destroyed by the shared_ptr's in the map.
-GCache::~GCache()
-{
- delete cache;
-}
+Cache::~Cache()
+{}
-std::vector<string> GCache::loadableFormats() const
+std::vector<string> Cache::loadableFormats() const
{
- return GImage::loadableFormats();
+ return Image::loadableFormats();
}
-void GCache::add(string const & file)
+void Cache::add(FileName const & file) const
{
- if (!AbsolutePath(file)) {
- lyxerr << "GCacheItem::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] << "GCache::add(" << file << "):\n"
+ lyxerr[Debug::GRAPHICS] << "Cache::add(" << file << "):\n"
<< "The file is already in the cache."
<< std::endl;
return;
}
-
- (*cache)[file] = GraphicPtr(new GCacheItem(file));
+ pimpl_->cache[file] = ItemPtr(new CacheItem(file));
}
-void GCache::remove(string const & file)
+void Cache::remove(FileName const & file) const
{
- CacheType::iterator it = cache->find(file);
- if (it == cache->end())
+ CacheType::iterator it = pimpl_->cache.find(file);
+ if (it == pimpl_->cache.end())
return;
- GraphicPtr item = it->second;
-
+ ItemPtr & item = it->second;
+
if (item.use_count() == 1) {
// The graphics file is in the cache, but nothing else
// references it.
- cache->erase(it);
+ pimpl_->cache.erase(it);
}
}
-bool GCache::inCache(string const & file) const
+bool Cache::inCache(FileName const & file) const
{
- return cache->find(file) != cache->end();
+ return pimpl_->cache.find(file) != pimpl_->cache.end();
}
-GraphicPtr const GCache::graphic(string const & file) const
+Cache::ItemPtr const Cache::item(FileName const & file) const
{
- CacheType::const_iterator it = cache->find(file);
- if (it == cache->end())
- return GraphicPtr();
+ CacheType::const_iterator it = pimpl_->cache.find(file);
+ if (it == pimpl_->cache.end())
+ return ItemPtr();
return it->second;
}
-
-ImagePtr const GCache::image(string const & file) const
-{
- CacheType::const_iterator it = cache->find(file);
- if (it == cache->end())
- return ImagePtr();
-
- return it->second->image();
-}
-
-
-} // namespace grfx
+} // namespace graphics
+} // namespace lyx