1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 /* 3 * This file is part of the LibreOffice project. 4 * 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/. 8 * 9 * This file incorporates work covered by the following license notice: 10 * 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 . 18 */ 19 20 #include <memory> 21 #include <tools/IconCache.hxx> 22 23 #include <tools/debug.hxx> 24 #include <osl/doublecheckedlocking.h> 25 #include <osl/getglobalmutex.hxx> 26 #include <unordered_map> 27 28 namespace sd 29 { 30 //===== IconCache::Implementation ============================================= 31 32 class IconCache::Implementation 33 { 34 private: 35 friend class IconCache; 36 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. 40 */ 41 static IconCache* s_pIconCache; 42 43 typedef std::unordered_map<OUString, Image> ImageContainer; 44 ImageContainer maContainer; 45 46 Image GetIcon(const OUString& rResourceId); 47 }; 48 49 IconCache* IconCache::Implementation::s_pIconCache = nullptr; 50 GetIcon(const OUString & rResourceId)51Image IconCache::Implementation::GetIcon(const OUString& rResourceId) 52 { 53 Image aResult; 54 ImageContainer::iterator iImage = maContainer.find(rResourceId); 55 if (iImage == maContainer.end()) 56 { 57 aResult = Image(StockImage::Yes, rResourceId); 58 maContainer[rResourceId] = aResult; 59 } 60 else 61 aResult = iImage->second; 62 return aResult; 63 } 64 65 //===== IconCache ============================================================= 66 67 //static Instance()68IconCache& IconCache::Instance() 69 { 70 if (Implementation::s_pIconCache == nullptr) 71 { 72 ::osl::GetGlobalMutex aMutexFunctor; 73 ::osl::MutexGuard aGuard(aMutexFunctor()); 74 if (Implementation::s_pIconCache == nullptr) 75 { 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; 81 } 82 } 83 else 84 { 85 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER(); 86 } 87 88 DBG_ASSERT(Implementation::s_pIconCache != nullptr, "IconCache::Instance(): instance is NULL"); 89 return *Implementation::s_pIconCache; 90 } 91 GetIcon(const OUString & rResourceId)92Image IconCache::GetIcon(const OUString& rResourceId) { return mpImpl->GetIcon(rResourceId); } 93 IconCache()94IconCache::IconCache() 95 : mpImpl(new Implementation) 96 { 97 } 98 ~IconCache()99IconCache::~IconCache() 100 { 101 // empty 102 } 103 104 } // end of namespace sd 105 106 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 107