]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCache.C
Implemented synchronous inline image viewing for InsetGraphics.
[lyx.git] / src / graphics / GraphicsCache.C
1 // -*- C++ -*-
2 /* This file is part of
3  * =================================================
4  * 
5  *          LyX, The Document Processor
6  *          Copyright 1995 Matthias Ettrich.
7  *          Copyright 1995-2000 The LyX Team.
8  *
9  *          This file Copyright 2000 Baruch Even
10  * ================================================= */
11
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "GraphicsCache.h"
19
20 #include "support/LAssert.h"
21
22 GraphicsCache * GraphicsCache::singleton = 0;
23
24
25 GraphicsCache * 
26 GraphicsCache::getInstance()
27 {
28         if (!singleton) {
29                 singleton = new GraphicsCache;
30                 Assert(singleton != 0);
31         }
32
33         return singleton;
34 }
35
36
37 GraphicsCache::~GraphicsCache()
38 {
39         // The map elements should have already been eliminated.
40         Assert(cache.empty());
41         
42         delete singleton;
43 }
44
45
46 GraphicsCacheItem *
47 GraphicsCache::addFile(string const & filename)
48 {
49         CacheType::iterator it = cache.find(filename);
50         
51         if (it != cache.end()) {
52                 return new GraphicsCacheItem( *((*it).second) );
53         }
54         
55         GraphicsCacheItem * cacheItem = new GraphicsCacheItem();
56         if (cacheItem == 0)
57                 return 0;
58         
59         cacheItem->setFilename(filename);
60         
61         cache[filename] = cacheItem;
62         
63         // We do not want to return the main cache object, otherwise when the
64         // will destroy their copy they will destroy the main copy.
65         return new GraphicsCacheItem( *cacheItem );
66 }
67
68
69 void
70 GraphicsCache::removeFile(string const & filename)
71 {
72         // We do not destroy the GraphicsCacheItem since we are here because
73         // the last copy of it is being erased.
74
75         CacheType::iterator it = cache.find(filename);
76         if (it != cache.end())
77                 cache.erase(it);
78 }