bump product version to 6.3.0.0.beta1
[LibreOffice.git] / sd / source / ui / tools / IconCache.cxx
blob514e513a1b428810d2e887f3b802050fa3a497fb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <memory>
21 #include <tools/IconCache.hxx>
23 #include <tools/debug.hxx>
24 #include <osl/doublecheckedlocking.h>
25 #include <osl/getglobalmutex.hxx>
26 #include <unordered_map>
28 namespace sd {
30 //===== IconCache::Implementation =============================================
32 class IconCache::Implementation
34 private:
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)
53 Image aResult;
54 ImageContainer::iterator iImage = maContainer.find(rResourceId);
55 if (iImage == maContainer.end())
57 aResult = Image(StockImage::Yes, rResourceId);
58 maContainer[rResourceId] = aResult;
60 else
61 aResult = iImage->second;
62 return aResult;
65 //===== IconCache =============================================================
67 //static
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;
83 else
85 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
88 DBG_ASSERT(Implementation::s_pIconCache != nullptr,
89 "IconCache::Instance(): instance is NULL");
90 return *Implementation::s_pIconCache;
93 Image IconCache::GetIcon(const OUString& rResourceId)
95 return mpImpl->GetIcon(rResourceId);
98 IconCache::IconCache()
99 : mpImpl (new Implementation)
103 IconCache::~IconCache()
105 // empty
108 } // end of namespace sd
110 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */