]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCache.C
prepare for 1.1.6pre2
[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 //#warning This is a bogus reason to not clean up after your self. (Lgb)
42         // TODO: Clean up here (BE)
43         
44         // This is not really needed, it will only happen on program close and in
45         // any case the OS will release those resources (not doing it may have 
46         // a good effect on closing time).
47         
48         delete singleton;
49 }
50
51
52 GraphicsCacheItem *
53 GraphicsCache::addFile(string const & filename)
54 {
55         CacheType::iterator it = cache.find(filename);
56         
57         if (it != cache.end()) {
58                 return new GraphicsCacheItem( *((*it).second) );
59         }
60         
61         GraphicsCacheItem * cacheItem = new GraphicsCacheItem();
62         if (cacheItem == 0)
63                 return 0;
64         
65         cacheItem->setFilename(filename);
66         
67         cache[filename] = cacheItem;
68         
69         // We do not want to return the main cache object, otherwise when the
70         // will destroy their copy they will destroy the main copy.
71         return new GraphicsCacheItem( *cacheItem );
72 }
73
74
75 void
76 GraphicsCache::removeFile(string const & filename)
77 {
78         // We do not destroy the GraphicsCacheItem since we are here because
79         // the last copy of it is being erased.
80
81         if (cache.find(filename) != cache.end())
82                 cache.erase(filename);
83 }