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 <boost/unordered_map.hpp>
24 : #include <osl/doublecheckedlocking.h>
25 : #include <osl/getglobalmutex.hxx>
26 :
27 : namespace sd {
28 :
29 : //===== IconCache::Implementation =============================================
30 :
31 32 : 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 ::boost::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 1140 : Image IconCache::Implementation::GetIcon (sal_uInt16 nResourceId)
51 : {
52 1140 : Image aResult;
53 1140 : ImageContainer::iterator iImage;
54 1140 : iImage = maContainer.find (nResourceId);
55 1140 : if (iImage == maContainer.end())
56 : {
57 32 : aResult = Image(BitmapEx(SdResId(nResourceId)));
58 32 : maContainer[nResourceId] = aResult;
59 : }
60 : else
61 1108 : aResult = iImage->second;
62 1140 : return aResult;
63 : }
64 :
65 : //===== IconCache =============================================================
66 :
67 : //static
68 1140 : IconCache& IconCache::Instance (void)
69 : {
70 1140 : if (Implementation::mpInstance == NULL)
71 : {
72 : ::osl::GetGlobalMutex aMutexFunctor;
73 16 : ::osl::MutexGuard aGuard (aMutexFunctor());
74 16 : if (Implementation::mpInstance == NULL)
75 : {
76 16 : IconCache* pCache = new IconCache ();
77 16 : SdGlobalResourceContainer::Instance().AddResource (
78 32 : ::std::unique_ptr<SdGlobalResource>(pCache));
79 : OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
80 16 : Implementation::mpInstance = pCache;
81 16 : }
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 1140 : return *Implementation::mpInstance;
91 : }
92 :
93 1140 : Image IconCache::GetIcon (sal_uInt16 nResourceId)
94 : {
95 1140 : return mpImpl->GetIcon (nResourceId);
96 : }
97 :
98 16 : IconCache::IconCache (void)
99 16 : : mpImpl (new Implementation())
100 : {
101 16 : }
102 :
103 32 : IconCache::~IconCache (void)
104 : {
105 : // empty
106 32 : }
107 :
108 114 : } // end of namespace sd
109 :
110 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|