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 :
21 : #include "tools/IconCache.hxx"
22 :
23 : #include "sdresid.hxx"
24 : #include <boost/unordered_map.hpp>
25 : #include <osl/doublecheckedlocking.h>
26 : #include <osl/getglobalmutex.hxx>
27 :
28 : namespace sd {
29 :
30 : //===== IconCache::Implementation =============================================
31 :
32 0 : 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* mpInstance;
42 :
43 : typedef ::boost::unordered_map<sal_uInt16,Image> ImageContainer;
44 : ImageContainer maContainer;
45 :
46 : Image GetIcon (sal_uInt16 nResourceId);
47 : };
48 :
49 : IconCache* IconCache::Implementation::mpInstance = NULL;
50 :
51 :
52 :
53 0 : Image IconCache::Implementation::GetIcon (sal_uInt16 nResourceId)
54 : {
55 0 : Image aResult;
56 0 : ImageContainer::iterator iImage;
57 0 : iImage = maContainer.find (nResourceId);
58 0 : if (iImage == maContainer.end())
59 : {
60 0 : aResult = Image(BitmapEx(SdResId(nResourceId)));
61 0 : maContainer[nResourceId] = aResult;
62 : }
63 : else
64 0 : aResult = iImage->second;
65 0 : return aResult;
66 : }
67 :
68 :
69 :
70 :
71 : //===== IconCache =============================================================
72 :
73 : //static
74 0 : IconCache& IconCache::Instance (void)
75 : {
76 0 : if (Implementation::mpInstance == NULL)
77 : {
78 : ::osl::GetGlobalMutex aMutexFunctor;
79 0 : ::osl::MutexGuard aGuard (aMutexFunctor());
80 0 : if (Implementation::mpInstance == NULL)
81 : {
82 0 : IconCache* pCache = new IconCache ();
83 0 : SdGlobalResourceContainer::Instance().AddResource (
84 0 : ::std::auto_ptr<SdGlobalResource>(pCache));
85 : OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
86 0 : Implementation::mpInstance = pCache;
87 0 : }
88 : }
89 : else
90 : {
91 : OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
92 : }
93 :
94 : DBG_ASSERT(Implementation::mpInstance!=NULL,
95 : "IconCache::Instance(): instance is NULL");
96 0 : return *Implementation::mpInstance;
97 : }
98 :
99 :
100 :
101 :
102 0 : Image IconCache::GetIcon (sal_uInt16 nResourceId)
103 : {
104 0 : return mpImpl->GetIcon (nResourceId);
105 : }
106 :
107 :
108 :
109 :
110 0 : IconCache::IconCache (void)
111 0 : : mpImpl (new Implementation())
112 : {
113 0 : }
114 :
115 :
116 :
117 :
118 0 : IconCache::~IconCache (void)
119 : {
120 : // empty
121 0 : }
122 :
123 : } // end of namespace sd
124 :
125 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|