]> git.lyx.org Git - lyx.git/blobdiff - src/graphics/GraphicsCache.C
add <string> and other small fixes to make
[lyx.git] / src / graphics / GraphicsCache.C
index b83a78f97a1c39c9530d9b71a18808752239850e..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;
 
-#include "support/LAssert.h"
 
-GraphicsCache * GraphicsCache::singleton = 0;
+namespace lyx {
+namespace graphics {
 
+/** 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()
 {
-    if (! singleton) {
-        singleton = new GraphicsCache;
-               Assert(singleton != 0);
-    }
+       // Now return the cache
+       static Cache singleton;
+       return singleton;
+}
+
+
+Cache::Cache()
+       : pimpl_(new Impl)
+{}
 
-    return singleton;
+
+Cache::~Cache()
+{}
+
+
+std::vector<string> Cache::loadableFormats() const
+{
+       return Image::loadableFormats();
+}
+
+
+void Cache::add(string const & file) const
+{
+       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));
 }
 
 
-GraphicsCache::~GraphicsCache()
+void Cache::remove(string 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).
+       CacheType::iterator it = pimpl_->cache.find(file);
+       if (it == pimpl_->cache.end())
+               return;
 
-    delete singleton;
+       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 *
-GraphicsCache::addFile(string const & filename)
+bool Cache::inCache(string const & file) const
 {
-    CacheType::const_iterator it = cache.find(filename);
-    
-    if (it != cache.end()) {
-        return new GraphicsCacheItem( *((*it).second) );
-    }
-       
-       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 );
+       return pimpl_->cache.find(file) != pimpl_->cache.end();
 }
 
 
-void
-GraphicsCache::removeFile(string const & filename)
+Cache::ItemPtr const Cache::item(string const & file) const
 {
-       // We do not destroy the GraphicsCacheItem since we are here because
-       // the last copy of it is being erased.
+       CacheType::const_iterator it = pimpl_->cache.find(file);
+       if (it == pimpl_->cache.end())
+               return ItemPtr();
 
-       if (cache.find(filename) != cache.end())
-               cache.erase(filename);
+       return it->second;
 }
+
+} // namespace graphics
+} // namespace lyx