]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCache.cpp
Move debug.{cpp,h}, Messages.{cpp,h} and gettext.{cpp,h} to support/.
[lyx.git] / src / graphics / GraphicsCache.cpp
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.
5  *
6  * \author Baruch Even
7  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "GraphicsCache.h"
15 #include "GraphicsCacheItem.h"
16 #include "GraphicsImage.h"
17
18 #include "support/debug.h"
19
20 #include "support/filetools.h"
21
22 #include <map>
23
24 using std::string;
25
26
27 namespace lyx {
28
29 using support::FileName;
30
31 namespace graphics {
32
33 /** The cache contains one item per file, so use a map to find the
34  *  cache item quickly by filename.
35  */
36 typedef std::map<FileName, Cache::ItemPtr> CacheType;
37
38 class Cache::Impl {
39 public:
40         ///
41         CacheType cache;
42 };
43
44
45 Cache & Cache::get()
46 {
47         // Now return the cache
48         static Cache singleton;
49         return singleton;
50 }
51
52
53 Cache::Cache()
54         : pimpl_(new Impl)
55 {}
56
57
58 Cache::~Cache()
59 {
60         delete pimpl_;
61 }
62
63
64 std::vector<string> Cache::loadableFormats() const
65 {
66         return Image::loadableFormats();
67 }
68
69
70 void Cache::add(FileName const & file) const
71 {
72         // Is the file in the cache already?
73         if (inCache(file)) {
74                 LYXERR(Debug::GRAPHICS, "Cache::add(" << file << "):\n"
75                                         << "The file is already in the cache.");
76                 return;
77         }
78
79         pimpl_->cache[file] = ItemPtr(new CacheItem(file));
80 }
81
82
83 void Cache::remove(FileName const & file) const
84 {
85         CacheType::iterator it = pimpl_->cache.find(file);
86         if (it == pimpl_->cache.end())
87                 return;
88
89         ItemPtr & item = it->second;
90
91         if (item.use_count() == 1) {
92                 // The graphics file is in the cache, but nothing else
93                 // references it.
94                 pimpl_->cache.erase(it);
95         }
96 }
97
98
99 bool Cache::inCache(FileName const & file) const
100 {
101         return pimpl_->cache.find(file) != pimpl_->cache.end();
102 }
103
104
105 Cache::ItemPtr const Cache::item(FileName const & file) const
106 {
107         CacheType::const_iterator it = pimpl_->cache.find(file);
108         if (it == pimpl_->cache.end())
109                 return ItemPtr();
110
111         return it->second;
112 }
113
114 } // namespace graphics
115 } // namespace lyx