]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCache.C
Asymptotic approach to a well-designed graphics loader.
[lyx.git] / src / graphics / GraphicsCache.C
1 /*
2  * \file GraphicsCache.C
3  * Copyright 2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Baruch Even <baruch.even@writeme.com>
7  * \author Angus Leeming <a.leeming@ic.ac.uk>
8  */
9
10 #include <config.h>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "GraphicsCache.h"
17 #include "GraphicsCacheItem.h"
18 #include "GraphicsImage.h"
19
20 #include "debug.h"
21
22 #include "support/filetools.h"
23
24 #include "frontends/lyx_gui.h"
25
26 #include <map>
27
28 namespace grfx {
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         static bool start = true;
44         if (start) {
45                 start = false;
46                 lyx_gui::init_graphics();
47         }
48
49         // Now return the cache
50         static Cache singleton;
51         return singleton;
52 }
53
54
55 Cache::Cache()
56         : pimpl_(new Impl())
57 {}
58
59
60 Cache::~Cache()
61 {}
62
63
64 std::vector<string> Cache::loadableFormats() const
65 {
66         return Image::loadableFormats();
67 }
68
69
70 void Cache::add(string const & file)
71 {
72         if (!AbsolutePath(file)) {
73                 lyxerr << "Cache::add(" << file << "):\n"
74                        << "The file must be have an absolute path."
75                        << std::endl;
76                 return;
77         }
78
79         // Is the file in the cache already?
80         if (inCache(file)) {
81                 lyxerr[Debug::GRAPHICS] << "Cache::add(" << file << "):\n"
82                                         << "The file is already in the cache."
83                                         << std::endl;
84                 return;
85         }
86
87         pimpl_->cache[file] = ItemPtr(new CacheItem(file));
88 }
89
90
91 void Cache::remove(string const & file)
92 {
93         CacheType::iterator it = pimpl_->cache.find(file);
94         if (it == pimpl_->cache.end())
95                 return;
96
97         ItemPtr & item = it->second;
98
99         if (item.use_count() == 1) {
100                 // The graphics file is in the cache, but nothing else
101                 // references it.
102                 pimpl_->cache.erase(it);
103         }
104 }
105
106
107 bool Cache::inCache(string const & file) const
108 {
109         return pimpl_->cache.find(file) != pimpl_->cache.end();
110 }
111
112
113 Cache::ItemPtr const Cache::item(string const & file) const
114 {
115         CacheType::const_iterator it = pimpl_->cache.find(file);
116         if (it == pimpl_->cache.end())
117                 return ItemPtr();
118
119         return it->second;
120 }
121
122 } // namespace grfx