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 <tools/debug.hxx>
24 #include <osl/doublecheckedlocking.h>
25 #include <osl/getglobalmutex.hxx>
26 #include <unordered_map>
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
* s_pIconCache
;
43 typedef std::unordered_map
<OUString
, Image
> ImageContainer
;
44 ImageContainer maContainer
;
46 Image
GetIcon(const OUString
& rResourceId
);
49 IconCache
* IconCache::Implementation::s_pIconCache
= nullptr;
51 Image
IconCache::Implementation::GetIcon(const OUString
& rResourceId
)
54 ImageContainer::iterator iImage
= maContainer
.find(rResourceId
);
55 if (iImage
== maContainer
.end())
57 aResult
= Image(StockImage::Yes
, rResourceId
);
58 maContainer
[rResourceId
] = aResult
;
61 aResult
= iImage
->second
;
65 //===== IconCache =============================================================
68 IconCache
& IconCache::Instance()
70 if (Implementation::s_pIconCache
== nullptr)
72 ::osl::GetGlobalMutex aMutexFunctor
;
73 ::osl::MutexGuard
aGuard(aMutexFunctor());
74 if (Implementation::s_pIconCache
== nullptr)
76 IconCache
* pCache
= new IconCache();
77 SdGlobalResourceContainer::Instance().AddResource(
78 ::std::unique_ptr
<SdGlobalResource
>(pCache
));
79 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
80 Implementation::s_pIconCache
= pCache
;
85 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
88 DBG_ASSERT(Implementation::s_pIconCache
!= nullptr, "IconCache::Instance(): instance is NULL");
89 return *Implementation::s_pIconCache
;
92 Image
IconCache::GetIcon(const OUString
& rResourceId
) { return mpImpl
->GetIcon(rResourceId
); }
94 IconCache::IconCache()
95 : mpImpl(new Implementation
)
99 IconCache::~IconCache()
104 } // end of namespace sd
106 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */