]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCache.C
Changed the singleton implementation in GraphicsCache, the former version
[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 #include "GraphicsCacheItem.h"
20
21 #include "support/LAssert.h"
22
23 GraphicsCache &
24 GraphicsCache::getInstance()
25 {
26         static GraphicsCache singleton;
27         return singleton;
28 }
29
30
31 GraphicsCache::~GraphicsCache()
32 {
33         // The map elements should have already been eliminated.
34         Assert(cache.empty());
35 }
36
37
38 GraphicsCache::shared_ptr_item
39 GraphicsCache::addFile(string const & filename)
40 {
41         CacheType::iterator it = cache.find(filename);
42         
43         if (it != cache.end()) {
44                 return (*it).second;
45         }
46         
47         shared_ptr_item cacheItem(new GraphicsCacheItem(filename));
48         if (cacheItem.get() == 0)
49                 return cacheItem;
50         
51         cache[filename] = cacheItem;
52
53         // GraphicsCacheItem_ptr is a shared_ptr and thus reference counted,
54         // it is safe to return it directly.
55         return cacheItem;
56 }
57
58
59 void
60 GraphicsCache::removeFile(string const & filename)
61 {
62         // We do not destroy the GraphicsCacheItem since we are here because
63         // the last copy of it is being erased.
64
65         CacheType::iterator it = cache.find(filename);
66         if (it != cache.end())
67                 cache.erase(it);
68 }