]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCache.C
Baruch's patch + some fixes to it.
[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         // Free the map.
40         //std::foreach(map.begin(), map.end(), ...);
41         // This is not really needed, it will only happen on program close and in
42         // any case the OS will release those resources (not doing it may have 
43         // a good effect on closing time).
44
45     delete singleton;
46 }
47
48
49 GraphicsCacheItem *
50 GraphicsCache::addFile(string const & filename)
51 {
52     CacheType::const_iterator it = cache.find(filename);
53     
54     if (it != cache.end()) {
55         return new GraphicsCacheItem( *((*it).second) );
56     }
57         
58         GraphicsCacheItem * cacheItem = new GraphicsCacheItem();
59         if (cacheItem == 0)
60                 return 0;
61
62         cacheItem->setFilename(filename);
63
64         cache[filename] = cacheItem;
65
66         // We do not want to return the main cache object, otherwise when the
67         // will destroy their copy they will destroy the main copy.
68     return new GraphicsCacheItem( *cacheItem );
69 }
70
71
72 void
73 GraphicsCache::removeFile(string const & filename)
74 {
75         // We do not destroy the GraphicsCacheItem since we are here because
76         // the last copy of it is being erased.
77
78         if (cache.find(filename) != cache.end())
79                 cache.erase(filename);
80 }