LibreOffice Module sd (master) 1
IconCache.cxx
Go to the documentation of this file.
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
28namespace sd
29{
30//===== IconCache::Implementation =============================================
31
33{
34private:
35 friend class IconCache;
36
42
43 typedef std::unordered_map<OUString, Image> ImageContainer;
45
46 Image GetIcon(const OUString& rResourceId);
47};
48
50
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
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();
78 ::std::unique_ptr<SdGlobalResource>(pCache));
79 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
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");
90}
91
92Image IconCache::GetIcon(const OUString& rResourceId) { return mpImpl->GetIcon(rResourceId); }
93
96{
97}
98
100{
101 // empty
102}
103
104} // end of namespace sd
105
106/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unordered_map< OUString, Image > ImageContainer
Definition: IconCache.cxx:43
static IconCache * s_pIconCache
This pointer holds a valid reference from first time that IconCache::Instance() is called to the end ...
Definition: IconCache.cxx:41
Image GetIcon(const OUString &rResourceId)
Definition: IconCache.cxx:51
This simple class stores frequently used icons so that the classes that use the icons do not have to ...
Definition: IconCache.hxx:38
Image GetIcon(const OUString &rResourceId)
Return the icon with the given resource id.
Definition: IconCache.cxx:92
static IconCache & Instance()
The lifetime of the returned reference is limited to that of the sd module.
Definition: IconCache.cxx:68
virtual ~IconCache() override
This destructor is called by SdGlobalResourceContainer.
Definition: IconCache.cxx:99
IconCache()
The constructor creates the one instance of the cache and registers it at the SdGlobalResourceContain...
Definition: IconCache.cxx:94
::std::unique_ptr< Implementation > mpImpl
Definition: IconCache.hxx:54
void AddResource(::std::unique_ptr< SdGlobalResource > pResource)
Add a resource to the container.
static SdGlobalResourceContainer & Instance()
#define DBG_ASSERT(sCon, aError)