Line data Source code
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 "tools/IconCache.hxx"
21 :
22 : #include "sdresid.hxx"
23 : #include <osl/doublecheckedlocking.h>
24 : #include <osl/getglobalmutex.hxx>
25 : #include <unordered_map>
26 :
27 : namespace sd {
28 :
29 : //===== IconCache::Implementation =============================================
30 :
31 17 : class IconCache::Implementation
32 : {
33 : private:
34 : friend class IconCache;
35 :
36 : /** This pointer holds a valid reference from first time that
37 : IconCache::Instance() is called to the end of the sd module when the
38 : cache is destroyed from SdGlobalResourceContainer.
39 : */
40 : static IconCache* mpInstance;
41 :
42 : typedef std::unordered_map<sal_uInt16,Image> ImageContainer;
43 : ImageContainer maContainer;
44 :
45 : Image GetIcon (sal_uInt16 nResourceId);
46 : };
47 :
48 : IconCache* IconCache::Implementation::mpInstance = NULL;
49 :
50 652 : Image IconCache::Implementation::GetIcon (sal_uInt16 nResourceId)
51 : {
52 652 : Image aResult;
53 652 : ImageContainer::iterator iImage;
54 652 : iImage = maContainer.find (nResourceId);
55 652 : if (iImage == maContainer.end())
56 : {
57 18 : aResult = Image(BitmapEx(SdResId(nResourceId)));
58 18 : maContainer[nResourceId] = aResult;
59 : }
60 : else
61 634 : aResult = iImage->second;
62 652 : return aResult;
63 : }
64 :
65 : //===== IconCache =============================================================
66 :
67 : //static
68 652 : IconCache& IconCache::Instance()
69 : {
70 652 : if (Implementation::mpInstance == NULL)
71 : {
72 : ::osl::GetGlobalMutex aMutexFunctor;
73 9 : ::osl::MutexGuard aGuard (aMutexFunctor());
74 9 : if (Implementation::mpInstance == NULL)
75 : {
76 9 : IconCache* pCache = new IconCache ();
77 9 : SdGlobalResourceContainer::Instance().AddResource (
78 18 : ::std::unique_ptr<SdGlobalResource>(pCache));
79 : OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
80 9 : Implementation::mpInstance = pCache;
81 9 : }
82 : }
83 : else
84 : {
85 : OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
86 : }
87 :
88 : DBG_ASSERT(Implementation::mpInstance!=NULL,
89 : "IconCache::Instance(): instance is NULL");
90 652 : return *Implementation::mpInstance;
91 : }
92 :
93 652 : Image IconCache::GetIcon (sal_uInt16 nResourceId)
94 : {
95 652 : return mpImpl->GetIcon (nResourceId);
96 : }
97 :
98 9 : IconCache::IconCache()
99 9 : : mpImpl (new Implementation())
100 : {
101 9 : }
102 :
103 16 : IconCache::~IconCache()
104 : {
105 : // empty
106 16 : }
107 :
108 : } // end of namespace sd
109 :
110 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|