]> git.lyx.org Git - features.git/blob - src/graphics/GraphicsCache.h
* Split the graphics loader into a frontend and a backend.
[features.git] / src / graphics / GraphicsCache.h
1 // -*- C++ -*-
2 /**
3  *  \file GraphicsCache.h
4  *  Copyright 2002 the LyX Team
5  *  Read the file COPYING
6  *
7  * \author Baruch Even <baruch.even@writeme.com>
8  * \author Angus Leeming <a.leeming@ic.ac.uk>
9  *
10  *  grfx::GCache is the manager of the image cache.
11  *  It is responsible for creating the grfx::GCacheItem's and maintaining them.
12  *
13  *  grfx::GCache is a singleton class. It is possible to have only one
14  *  instance of it at any moment.
15  */
16
17 #ifndef GRAPHICSCACHE_H
18 #define GRAPHICSCACHE_H
19
20 #ifdef __GNUG__
21 #pragma interface
22 #endif
23
24 #include "LString.h"
25 #include "GraphicsTypes.h"
26 #include <map>
27 #include <vector>
28 #include <boost/utility.hpp>
29
30 namespace grfx {
31
32 class GCache : boost::noncopyable {
33 public:
34
35         /// This is a singleton class. Get the instance.
36         static GCache & get();
37
38         /** Which graphics formats can be loaded directly by the image loader.
39          *  Other formats can be loaded if a converter to a loadable format
40          *  can be defined.
41          */
42         std::vector<string> loadableFormats() const;
43
44         /// Add a graphics file to the cache.
45         void add(string const & file);
46
47         /** Remove a file from the cache.
48          *  Called from the InsetGraphics d-tor.
49          *  If we use reference counting, then this may become redundant.
50          */
51         void remove(string const & file);
52
53         /// Returns \c true if the file is in the cache.
54         bool inCache(string const & file) const;
55
56         /** Get the cache item associated with file.
57          *  Returns an empty container if there is no such item.
58          *
59          *  IMPORTANT: whatever uses an image must make a local copy of this
60          *  GraphicPtr. The boost::shared_ptr<>::use_count() function is
61          *  used to ascertain whether or not to remove the item from the cache
62          *  when remove(file) is called.
63          *
64          *  You have been warned!
65          */
66         GraphicPtr const graphic(string const & file) const;
67
68         /** Get the image associated with file.
69             If the image is not yet loaded, (or is not in the cache!) return
70             an empty container.
71          */
72         ImagePtr const image(string const & file) const;
73
74 private:
75         /** Make the c-tor, d-tor private so we can control how many objects
76          *  are instantiated.
77          */
78         GCache();
79         ///
80         ~GCache();
81
82         /** The cache contains one item per file, so use a map to find the
83          *  cache item quickly by filename.
84          *  Note that each cache item can have multiple views, potentially one
85          *  per inset that references the original file.
86          */
87         typedef std::map<string, GraphicPtr> CacheType;
88
89         /** Store a pointer to the cache so that we can forward declare
90          *  GCacheItem.
91          */
92         CacheType * cache;
93 };
94
95 } // namespace grfx
96
97
98 #endif // GRAPHICSCACHE_H