Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include "tools/IconCache.hxx"
31 : :
32 : : #include "sdresid.hxx"
33 : : #include <boost/unordered_map.hpp>
34 : : #include <osl/doublecheckedlocking.h>
35 : : #include <osl/getglobalmutex.hxx>
36 : :
37 : : namespace sd {
38 : :
39 : : //===== IconCache::Implementation =============================================
40 : :
41 [ + - ]: 32 : class IconCache::Implementation
42 : : {
43 : : private:
44 : : friend class IconCache;
45 : :
46 : : /** This pointer holds a valid reference from first time that
47 : : IconCache::Instance() is called to the end of the sd module when the
48 : : cache is destroyed from SdGlobalResourceContainer.
49 : : */
50 : : static IconCache* mpInstance;
51 : :
52 : : typedef ::boost::unordered_map<sal_uInt16,Image> ImageContainer;
53 : : ImageContainer maContainer;
54 : :
55 : : Image GetIcon (sal_uInt16 nResourceId);
56 : : };
57 : :
58 : : IconCache* IconCache::Implementation::mpInstance = NULL;
59 : :
60 : :
61 : :
62 : 644 : Image IconCache::Implementation::GetIcon (sal_uInt16 nResourceId)
63 : : {
64 [ + - ]: 644 : Image aResult;
65 : 644 : ImageContainer::iterator iImage;
66 [ + - ]: 644 : iImage = maContainer.find (nResourceId);
67 [ + + ]: 644 : if (iImage == maContainer.end())
68 : : {
69 [ + - ][ + - ]: 16 : aResult = Image(BitmapEx(SdResId(nResourceId)));
[ + - ][ + - ]
[ + - ][ + - ]
70 [ + - ][ + - ]: 16 : maContainer[nResourceId] = aResult;
71 : : }
72 : : else
73 [ + - ][ + - ]: 644 : aResult = iImage->second;
74 : 644 : return aResult;
75 : : }
76 : :
77 : :
78 : :
79 : :
80 : : //===== IconCache =============================================================
81 : :
82 : : //static
83 : 644 : IconCache& IconCache::Instance (void)
84 : : {
85 [ + + ]: 644 : if (Implementation::mpInstance == NULL)
86 : : {
87 : : ::osl::GetGlobalMutex aMutexFunctor;
88 [ + - ][ + - ]: 16 : ::osl::MutexGuard aGuard (aMutexFunctor());
89 [ + - ]: 16 : if (Implementation::mpInstance == NULL)
90 : : {
91 [ + - ][ + - ]: 16 : IconCache* pCache = new IconCache ();
92 [ + - ]: 16 : SdGlobalResourceContainer::Instance().AddResource (
93 [ + - ][ + - ]: 32 : ::std::auto_ptr<SdGlobalResource>(pCache));
[ + - ]
94 : : OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
95 : 16 : Implementation::mpInstance = pCache;
96 [ + - ]: 16 : }
97 : : }
98 : : else
99 : : {
100 : : OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
101 : : }
102 : :
103 : : DBG_ASSERT(Implementation::mpInstance!=NULL,
104 : : "IconCache::Instance(): instance is NULL");
105 : 644 : return *Implementation::mpInstance;
106 : : }
107 : :
108 : :
109 : :
110 : :
111 : 644 : Image IconCache::GetIcon (sal_uInt16 nResourceId)
112 : : {
113 : 644 : return mpImpl->GetIcon (nResourceId);
114 : : }
115 : :
116 : :
117 : :
118 : :
119 : 16 : IconCache::IconCache (void)
120 [ + - ][ + - ]: 16 : : mpImpl (new Implementation())
121 : : {
122 : 16 : }
123 : :
124 : :
125 : :
126 : :
127 [ + - ]: 16 : IconCache::~IconCache (void)
128 : : {
129 : : // empty
130 [ - + ]: 32 : }
131 : :
132 : : } // end of namespace sd
133 : :
134 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|