1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "tools/IconCache.hxx"
23 #include "sdresid.hxx"
24 #include <boost/unordered_map.hpp>
25 #include <osl/doublecheckedlocking.h>
26 #include <osl/getglobalmutex.hxx>
30 //===== IconCache::Implementation =============================================
32 class IconCache::Implementation
35 friend class IconCache
;
37 /** This pointer holds a valid reference from first time that
38 IconCache::Instance() is called to the end of the sd module when the
39 cache is destroyed from SdGlobalResourceContainer.
41 static IconCache
* mpInstance
;
43 typedef ::boost::unordered_map
<sal_uInt16
,Image
> ImageContainer
;
44 ImageContainer maContainer
;
46 Image
GetIcon (sal_uInt16 nResourceId
);
49 IconCache
* IconCache::Implementation::mpInstance
= NULL
;
53 Image
IconCache::Implementation::GetIcon (sal_uInt16 nResourceId
)
56 ImageContainer::iterator iImage
;
57 iImage
= maContainer
.find (nResourceId
);
58 if (iImage
== maContainer
.end())
60 aResult
= Image(BitmapEx(SdResId(nResourceId
)));
61 maContainer
[nResourceId
] = aResult
;
64 aResult
= iImage
->second
;
71 //===== IconCache =============================================================
74 IconCache
& IconCache::Instance (void)
76 if (Implementation::mpInstance
== NULL
)
78 ::osl::GetGlobalMutex aMutexFunctor
;
79 ::osl::MutexGuard
aGuard (aMutexFunctor());
80 if (Implementation::mpInstance
== NULL
)
82 IconCache
* pCache
= new IconCache ();
83 SdGlobalResourceContainer::Instance().AddResource (
84 ::std::auto_ptr
<SdGlobalResource
>(pCache
));
85 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
86 Implementation::mpInstance
= pCache
;
91 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
94 DBG_ASSERT(Implementation::mpInstance
!=NULL
,
95 "IconCache::Instance(): instance is NULL");
96 return *Implementation::mpInstance
;
102 Image
IconCache::GetIcon (sal_uInt16 nResourceId
)
104 return mpImpl
->GetIcon (nResourceId
);
110 IconCache::IconCache (void)
111 : mpImpl (new Implementation())
118 IconCache::~IconCache (void)
123 } // end of namespace sd
125 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */