scramble email addresses
[lyx.git] / src / graphics / GraphicsCache.cpp
blob10f9fc7a73b8f8141a77f7ffa9faeebafdf9ad32
1 /**
2 * \file GraphicsCache.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Baruch Even
7 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS.
12 #include <config.h>
14 #include "GraphicsCache.h"
15 #include "GraphicsCacheItem.h"
16 #include "GraphicsImage.h"
18 #include "debug.h"
20 #include "support/filetools.h"
22 #include <map>
24 using std::string;
27 namespace lyx {
29 using support::FileName;
31 namespace graphics {
33 /** The cache contains one item per file, so use a map to find the
34 * cache item quickly by filename.
36 typedef std::map<FileName, Cache::ItemPtr> CacheType;
38 class Cache::Impl {
39 public:
40 ///
41 CacheType cache;
45 Cache & Cache::get()
47 // Now return the cache
48 static Cache singleton;
49 return singleton;
53 Cache::Cache()
54 : pimpl_(new Impl)
58 Cache::~Cache()
62 std::vector<string> Cache::loadableFormats() const
64 return Image::loadableFormats();
68 void Cache::add(FileName const & file) const
70 // Is the file in the cache already?
71 if (inCache(file)) {
72 LYXERR(Debug::GRAPHICS) << "Cache::add(" << file << "):\n"
73 << "The file is already in the cache."
74 << std::endl;
75 return;
78 pimpl_->cache[file] = ItemPtr(new CacheItem(file));
82 void Cache::remove(FileName const & file) const
84 CacheType::iterator it = pimpl_->cache.find(file);
85 if (it == pimpl_->cache.end())
86 return;
88 ItemPtr & item = it->second;
90 if (item.use_count() == 1) {
91 // The graphics file is in the cache, but nothing else
92 // references it.
93 pimpl_->cache.erase(it);
98 bool Cache::inCache(FileName const & file) const
100 return pimpl_->cache.find(file) != pimpl_->cache.end();
104 Cache::ItemPtr const Cache::item(FileName const & file) const
106 CacheType::const_iterator it = pimpl_->cache.find(file);
107 if (it == pimpl_->cache.end())
108 return ItemPtr();
110 return it->second;
113 } // namespace graphics
114 } // namespace lyx