]> 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 16a3e2aa93225f98c8c3434b0e738c0085dac6a4..124141604e3fa115fb0f642d0e9b0ef48d892735 100644 (file)
@@ -1,18 +1,16 @@
-/*
+/**
  * \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"
+namespace support = lyx::support;
 
-namespace grfx {
+using std::string;
 
-GCache & GCache::get()
-{
-       static bool start = true;
-       if (start) {
-               start = false;
-               lyx_gui::init_graphics();
-       }
 
+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;
+
+struct Cache::Impl {
+       ///
+       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(string const & file) const
 {
-       if (!AbsolutePath(file)) {
-               lyxerr << "GCacheItem::add(" << file << "):\n"
+       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] << "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(string 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(string 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(string 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