Update ooo320-m1
[ooovba.git] / sd / source / ui / tools / IconCache.cxx
blob222a34742baed9bd5208b85ef2c8035c64b26cf7
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: IconCache.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sd.hxx"
34 #include "tools/IconCache.hxx"
36 #include "sdresid.hxx"
37 #include <hash_map>
38 #include <osl/doublecheckedlocking.h>
39 #include <osl/getglobalmutex.hxx>
41 namespace sd {
43 //===== IconCache::Implementation =============================================
45 class IconCache::Implementation
47 private:
48 friend class IconCache;
50 /** This pointer holds a valid reference from first time that
51 IconCache::Instance() is called to the end of the sd module when the
52 cache is destroyed from SdGlobalResourceContainer.
54 static IconCache* mpInstance;
56 typedef ::std::hash_map<USHORT,Image> ImageContainer;
57 ImageContainer maContainer;
59 Image GetIcon (USHORT nResourceId);
62 IconCache* IconCache::Implementation::mpInstance = NULL;
66 Image IconCache::Implementation::GetIcon (USHORT nResourceId)
68 Image aResult;
69 ImageContainer::iterator iImage;
70 iImage = maContainer.find (nResourceId);
71 if (iImage == maContainer.end())
73 aResult = Image(BitmapEx(SdResId(nResourceId)));
74 maContainer[nResourceId] = aResult;
76 else
77 aResult = iImage->second;
78 return aResult;
84 //===== IconCache =============================================================
86 //static
87 IconCache& IconCache::Instance (void)
89 if (Implementation::mpInstance == NULL)
91 ::osl::GetGlobalMutex aMutexFunctor;
92 ::osl::MutexGuard aGuard (aMutexFunctor());
93 if (Implementation::mpInstance == NULL)
95 IconCache* pCache = new IconCache ();
96 SdGlobalResourceContainer::Instance().AddResource (
97 ::std::auto_ptr<SdGlobalResource>(pCache));
98 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
99 Implementation::mpInstance = pCache;
102 else
104 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
107 DBG_ASSERT(Implementation::mpInstance!=NULL,
108 "IconCache::Instance(): instance is NULL");
109 return *Implementation::mpInstance;
115 Image IconCache::GetIcon (USHORT nResourceId)
117 return mpImpl->GetIcon (nResourceId);
123 IconCache::IconCache (void)
124 : mpImpl (new Implementation())
131 IconCache::~IconCache (void)
133 // empty
136 } // end of namespace sd