xref: /aoo41x/main/sd/source/ui/tools/IconCache.cxx (revision 79aad27f)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sd.hxx"
26 
27 #include "tools/IconCache.hxx"
28 
29 #include "sdresid.hxx"
30 #include <hash_map>
31 #include <osl/doublecheckedlocking.h>
32 #include <osl/getglobalmutex.hxx>
33 
34 namespace sd {
35 
36 //===== IconCache::Implementation =============================================
37 
38 class IconCache::Implementation
39 {
40 private:
41     friend class IconCache;
42 
43     /** This pointer holds a valid reference from first time that
44         IconCache::Instance() is called to the end of the sd module when the
45         cache is destroyed from SdGlobalResourceContainer.
46     */
47     static IconCache* mpInstance;
48 
49     typedef ::std::hash_map<sal_uInt16,Image> ImageContainer;
50     ImageContainer maContainer;
51 
52     Image GetIcon (sal_uInt16 nResourceId);
53 };
54 
55 IconCache* IconCache::Implementation::mpInstance = NULL;
56 
57 
58 
GetIcon(sal_uInt16 nResourceId)59 Image IconCache::Implementation::GetIcon (sal_uInt16 nResourceId)
60 {
61     Image aResult;
62     ImageContainer::iterator iImage;
63     iImage = maContainer.find (nResourceId);
64     if (iImage == maContainer.end())
65     {
66         aResult = Image(BitmapEx(SdResId(nResourceId)));
67         maContainer[nResourceId] = aResult;
68     }
69     else
70         aResult = iImage->second;
71     return aResult;
72 }
73 
74 
75 
76 
77 //===== IconCache =============================================================
78 
79 //static
Instance(void)80 IconCache& IconCache::Instance (void)
81 {
82     if (Implementation::mpInstance == NULL)
83     {
84         ::osl::GetGlobalMutex aMutexFunctor;
85         ::osl::MutexGuard aGuard (aMutexFunctor());
86         if (Implementation::mpInstance == NULL)
87         {
88             IconCache* pCache = new IconCache ();
89             SdGlobalResourceContainer::Instance().AddResource (
90                 ::std::auto_ptr<SdGlobalResource>(pCache));
91             OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
92             Implementation::mpInstance = pCache;
93         }
94     }
95     else
96     {
97         OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
98     }
99 
100     DBG_ASSERT(Implementation::mpInstance!=NULL,
101         "IconCache::Instance(): instance is NULL");
102     return *Implementation::mpInstance;
103 }
104 
105 
106 
107 
GetIcon(sal_uInt16 nResourceId)108 Image IconCache::GetIcon (sal_uInt16 nResourceId)
109 {
110     return mpImpl->GetIcon (nResourceId);
111 }
112 
113 
114 
115 
IconCache(void)116 IconCache::IconCache (void)
117     : mpImpl (new Implementation())
118 {
119 }
120 
121 
122 
123 
~IconCache(void)124 IconCache::~IconCache (void)
125 {
126     // empty
127 }
128 
129 } // end of namespace sd
130