]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCache.C
The free_spacing patch and fix to the mess that Rob discovered.
[lyx.git] / src / graphics / GraphicsCache.C
1 /**
2  * \file GraphicsCache.C
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 "debug.h"
19
20 #include "support/filetools.h"
21
22 #include "frontends/lyx_gui.h"
23
24 #include <map>
25
26 namespace grfx {
27
28 /** The cache contains one item per file, so use a map to find the
29  *  cache item quickly by filename.
30  */
31 typedef std::map<string, Cache::ItemPtr> CacheType;
32
33 struct Cache::Impl {
34         ///
35         CacheType cache;
36 };
37
38
39 Cache & Cache::get()
40 {
41         // Now return the cache
42         static Cache singleton;
43         return singleton;
44 }
45
46
47 Cache::Cache()
48         : pimpl_(new Impl())
49 {}
50
51
52 Cache::~Cache()
53 {}
54
55
56 std::vector<string> Cache::loadableFormats() const
57 {
58         return Image::loadableFormats();
59 }
60
61
62 void Cache::add(string const & file) const
63 {
64         if (!AbsolutePath(file)) {
65                 lyxerr << "Cache::add(" << file << "):\n"
66                        << "The file must be have an absolute path."
67                        << std::endl;
68                 return;
69         }
70
71         // Is the file in the cache already?
72         if (inCache(file)) {
73                 lyxerr[Debug::GRAPHICS] << "Cache::add(" << file << "):\n"
74                                         << "The file is already in the cache."
75                                         << std::endl;
76                 return;
77         }
78
79         pimpl_->cache[file] = ItemPtr(new CacheItem(file));
80 }
81
82
83 void Cache::remove(string 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(string const & file) const
100 {
101         return pimpl_->cache.find(file) != pimpl_->cache.end();
102 }
103
104
105 Cache::ItemPtr const Cache::item(string 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 grfx