]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCache.C
clean code to export between different flavours, output different code for sgml to...
[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 namespace support = lyx::support;
23
24 using std::string;
25
26
27 namespace lyx {
28 namespace graphics {
29
30 /** The cache contains one item per file, so use a map to find the
31  *  cache item quickly by filename.
32  */
33 typedef std::map<string, Cache::ItemPtr> CacheType;
34
35 struct Cache::Impl {
36         ///
37         CacheType cache;
38 };
39
40
41 Cache & Cache::get()
42 {
43         // Now return the cache
44         static Cache singleton;
45         return singleton;
46 }
47
48
49 Cache::Cache()
50         : pimpl_(new Impl)
51 {}
52
53
54 Cache::~Cache()
55 {}
56
57
58 std::vector<string> Cache::loadableFormats() const
59 {
60         return Image::loadableFormats();
61 }
62
63
64 void Cache::add(string const & file) const
65 {
66         if (!support::AbsolutePath(file)) {
67                 lyxerr << "Cache::add(" << file << "):\n"
68                        << "The file must be have an absolute path."
69                        << std::endl;
70                 return;
71         }
72
73         // Is the file in the cache already?
74         if (inCache(file)) {
75                 lyxerr[Debug::GRAPHICS] << "Cache::add(" << file << "):\n"
76                                         << "The file is already in the cache."
77                                         << std::endl;
78                 return;
79         }
80
81         pimpl_->cache[file] = ItemPtr(new CacheItem(file));
82 }
83
84
85 void Cache::remove(string const & file) const
86 {
87         CacheType::iterator it = pimpl_->cache.find(file);
88         if (it == pimpl_->cache.end())
89                 return;
90
91         ItemPtr & item = it->second;
92
93         if (item.use_count() == 1) {
94                 // The graphics file is in the cache, but nothing else
95                 // references it.
96                 pimpl_->cache.erase(it);
97         }
98 }
99
100
101 bool Cache::inCache(string const & file) const
102 {
103         return pimpl_->cache.find(file) != pimpl_->cache.end();
104 }
105
106
107 Cache::ItemPtr const Cache::item(string const & file) const
108 {
109         CacheType::const_iterator it = pimpl_->cache.find(file);
110         if (it == pimpl_->cache.end())
111                 return ItemPtr();
112
113         return it->second;
114 }
115
116 } // namespace graphics
117 } // namespace lyx