xref: /aoo42x/main/sd/source/ui/tools/IconCache.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sd.hxx"
30 
31 #include "tools/IconCache.hxx"
32 
33 #include "sdresid.hxx"
34 #include <hash_map>
35 #include <osl/doublecheckedlocking.h>
36 #include <osl/getglobalmutex.hxx>
37 
38 namespace sd {
39 
40 //===== IconCache::Implementation =============================================
41 
42 class IconCache::Implementation
43 {
44 private:
45     friend class IconCache;
46 
47     /** This pointer holds a valid reference from first time that
48         IconCache::Instance() is called to the end of the sd module when the
49         cache is destroyed from SdGlobalResourceContainer.
50     */
51     static IconCache* mpInstance;
52 
53     typedef ::std::hash_map<sal_uInt16,Image> ImageContainer;
54     ImageContainer maContainer;
55 
56     Image GetIcon (sal_uInt16 nResourceId);
57 };
58 
59 IconCache* IconCache::Implementation::mpInstance = NULL;
60 
61 
62 
63 Image IconCache::Implementation::GetIcon (sal_uInt16 nResourceId)
64 {
65     Image aResult;
66     ImageContainer::iterator iImage;
67     iImage = maContainer.find (nResourceId);
68     if (iImage == maContainer.end())
69     {
70         aResult = Image(BitmapEx(SdResId(nResourceId)));
71         maContainer[nResourceId] = aResult;
72     }
73     else
74         aResult = iImage->second;
75     return aResult;
76 }
77 
78 
79 
80 
81 //===== IconCache =============================================================
82 
83 //static
84 IconCache& IconCache::Instance (void)
85 {
86     if (Implementation::mpInstance == NULL)
87     {
88         ::osl::GetGlobalMutex aMutexFunctor;
89         ::osl::MutexGuard aGuard (aMutexFunctor());
90         if (Implementation::mpInstance == NULL)
91         {
92             IconCache* pCache = new IconCache ();
93             SdGlobalResourceContainer::Instance().AddResource (
94                 ::std::auto_ptr<SdGlobalResource>(pCache));
95             OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
96             Implementation::mpInstance = pCache;
97         }
98     }
99     else
100     {
101         OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
102     }
103 
104     DBG_ASSERT(Implementation::mpInstance!=NULL,
105         "IconCache::Instance(): instance is NULL");
106     return *Implementation::mpInstance;
107 }
108 
109 
110 
111 
112 Image IconCache::GetIcon (sal_uInt16 nResourceId)
113 {
114     return mpImpl->GetIcon (nResourceId);
115 }
116 
117 
118 
119 
120 IconCache::IconCache (void)
121     : mpImpl (new Implementation())
122 {
123 }
124 
125 
126 
127 
128 IconCache::~IconCache (void)
129 {
130     // empty
131 }
132 
133 } // end of namespace sd
134