]> git.lyx.org Git - features.git/blob - src/graphics/GraphicsCache.cpp
'using namespace std' instead of 'using std::xxx'
[features.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 namespace std;
25
26 namespace lyx {
27
28 using support::FileName;
29
30 namespace graphics {
31
32 /** The cache contains one item per file, so use a map to find the
33  *  cache item quickly by filename.
34  */
35 typedef std::map<FileName, Cache::ItemPtr> CacheType;
36
37 class Cache::Impl {
38 public:
39         ///
40         CacheType cache;
41 };
42
43
44 Cache & Cache::get()
45 {
46         // Now return the cache
47         static Cache singleton;
48         return singleton;
49 }
50
51
52 Cache::Cache()
53         : pimpl_(new Impl)
54 {}
55
56
57 Cache::~Cache()
58 {
59         delete pimpl_;
60 }
61
62
63 std::vector<string> Cache::loadableFormats() const
64 {
65         return Image::loadableFormats();
66 }
67
68
69 void Cache::add(FileName const & file) const
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                 return;
76         }
77
78         pimpl_->cache[file] = ItemPtr(new CacheItem(file));
79 }
80
81
82 void Cache::remove(FileName const & file) const
83 {
84         CacheType::iterator it = pimpl_->cache.find(file);
85         if (it == pimpl_->cache.end())
86                 return;
87
88         ItemPtr & item = it->second;
89
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);
94         }
95 }
96
97
98 bool Cache::inCache(FileName const & file) const
99 {
100         return pimpl_->cache.find(file) != pimpl_->cache.end();
101 }
102
103
104 Cache::ItemPtr const Cache::item(FileName const & file) const
105 {
106         CacheType::const_iterator it = pimpl_->cache.find(file);
107         if (it == pimpl_->cache.end())
108                 return ItemPtr();
109
110         return it->second;
111 }
112
113 } // namespace graphics
114 } // namespace lyx