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