/src/libreoffice/sd/source/ui/tools/IconCache.cxx
Line | Count | Source |
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 | | |
51 | | Image IconCache::Implementation::GetIcon(const OUString& rResourceId) |
52 | 0 | { |
53 | 0 | Image aResult; |
54 | 0 | ImageContainer::iterator iImage = maContainer.find(rResourceId); |
55 | 0 | if (iImage == maContainer.end()) |
56 | 0 | { |
57 | 0 | aResult = Image(StockImage::Yes, rResourceId); |
58 | 0 | maContainer[rResourceId] = aResult; |
59 | 0 | } |
60 | 0 | else |
61 | 0 | aResult = iImage->second; |
62 | 0 | return aResult; |
63 | 0 | } |
64 | | |
65 | | //===== IconCache ============================================================= |
66 | | |
67 | | //static |
68 | | IconCache& IconCache::Instance() |
69 | 0 | { |
70 | 0 | if (Implementation::s_pIconCache == nullptr) |
71 | 0 | { |
72 | 0 | ::osl::GetGlobalMutex aMutexFunctor; |
73 | 0 | ::osl::MutexGuard aGuard(aMutexFunctor()); |
74 | 0 | if (Implementation::s_pIconCache == nullptr) |
75 | 0 | { |
76 | 0 | IconCache* pCache = new IconCache(); |
77 | 0 | SdGlobalResourceContainer::Instance().AddResource( |
78 | 0 | ::std::unique_ptr<SdGlobalResource>(pCache)); |
79 | 0 | OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER(); |
80 | 0 | Implementation::s_pIconCache = pCache; |
81 | 0 | } |
82 | 0 | } |
83 | 0 | else |
84 | 0 | { |
85 | 0 | OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER(); |
86 | 0 | } |
87 | |
|
88 | 0 | DBG_ASSERT(Implementation::s_pIconCache != nullptr, "IconCache::Instance(): instance is NULL"); |
89 | 0 | return *Implementation::s_pIconCache; |
90 | 0 | } |
91 | | |
92 | 0 | Image IconCache::GetIcon(const OUString& rResourceId) { return mpImpl->GetIcon(rResourceId); } |
93 | | |
94 | | IconCache::IconCache() |
95 | 0 | : mpImpl(new Implementation) |
96 | 0 | { |
97 | 0 | } |
98 | | |
99 | | IconCache::~IconCache() |
100 | 0 | { |
101 | | // empty |
102 | 0 | } |
103 | | |
104 | | } // end of namespace sd |
105 | | |
106 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |