Branch data 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/SdGlobalResourceContainer.hxx"
22 : :
23 : : #include <algorithm>
24 : : #include <vector>
25 : :
26 : : using namespace ::com::sun::star;
27 : : using namespace ::com::sun::star::uno;
28 : :
29 : :
30 : : namespace sd {
31 : :
32 : :
33 : : //===== SdGlobalResourceContainer::Implementation =============================
34 : :
35 [ + - ][ + - ]: 41 : class SdGlobalResourceContainer::Implementation
[ + - ]
36 : : {
37 : : private:
38 : : friend class SdGlobalResourceContainer;
39 : : static SdGlobalResourceContainer* mpInstance;
40 : :
41 : : ::osl::Mutex maMutex;
42 : :
43 : : /** All instances of SdGlobalResource in this vector are owned by the
44 : : container and will be destroyed when the container is destroyed.
45 : : */
46 : : typedef ::std::vector<SdGlobalResource*> ResourceList;
47 : : ResourceList maResources;
48 : :
49 : : typedef ::std::vector<boost::shared_ptr<SdGlobalResource> > SharedResourceList;
50 : : SharedResourceList maSharedResources;
51 : :
52 : : typedef ::std::vector<Reference<XInterface> > XInterfaceResourceList;
53 : : XInterfaceResourceList maXInterfaceResources;
54 : : };
55 : :
56 : :
57 : :
58 : :
59 : : // static
60 : 170 : SdGlobalResourceContainer& SdGlobalResourceContainer::Instance (void)
61 : : {
62 : : DBG_ASSERT(Implementation::mpInstance!=NULL,
63 : : "SdGlobalResourceContainer::Instance(): instance has been deleted");
64 : : // Maybe we should throw an exception when the instance has been deleted.
65 : 170 : return *Implementation::mpInstance;
66 : : }
67 : :
68 : : SdGlobalResourceContainer*
69 : : SdGlobalResourceContainer::Implementation::mpInstance = NULL;
70 : :
71 : :
72 : :
73 : :
74 : : //===== SdGlobalResourceContainer =============================================
75 : :
76 : 24 : void SdGlobalResourceContainer::AddResource (
77 : : ::std::auto_ptr<SdGlobalResource> pResource)
78 : : {
79 [ + - ]: 24 : ::osl::MutexGuard aGuard (mpImpl->maMutex);
80 : :
81 : 24 : Implementation::ResourceList::iterator iResource;
82 : : iResource = ::std::find (
83 : 24 : mpImpl->maResources.begin(),
84 : 24 : mpImpl->maResources.end(),
85 [ + - ]: 72 : pResource.get());
86 [ + - ][ + - ]: 24 : if (iResource == mpImpl->maResources.end())
87 [ + - ]: 24 : mpImpl->maResources.push_back(pResource.get());
88 : : else
89 : : {
90 : : // Because the given resource is an auto_ptr it is highly unlikely
91 : : // that we come here. But who knows?
92 : : DBG_ASSERT (false,
93 : : "SdGlobalResourceContainer:AddResource(): Resource added twice.");
94 : : }
95 : : // We can not put the auto_ptr into the vector so we release the
96 : : // auto_ptr and document that we take ownership explicitly.
97 [ + - ]: 24 : pResource.release();
98 : 24 : }
99 : :
100 : :
101 : :
102 : :
103 : 130 : void SdGlobalResourceContainer::AddResource (
104 : : ::boost::shared_ptr<SdGlobalResource> pResource)
105 : : {
106 [ + - ]: 130 : ::osl::MutexGuard aGuard (mpImpl->maMutex);
107 : :
108 : 130 : Implementation::SharedResourceList::iterator iResource;
109 : : iResource = ::std::find (
110 : 130 : mpImpl->maSharedResources.begin(),
111 : 130 : mpImpl->maSharedResources.end(),
112 [ + - ]: 260 : pResource);
113 [ + - ][ + - ]: 130 : if (iResource == mpImpl->maSharedResources.end())
114 [ + - ]: 130 : mpImpl->maSharedResources.push_back(pResource);
115 : : else
116 : : {
117 : : DBG_ASSERT (false,
118 : : "SdGlobalResourceContainer:AddResource(): Resource added twice.");
119 [ + - ]: 130 : }
120 : 130 : }
121 : :
122 : :
123 : :
124 : :
125 : 16 : void SdGlobalResourceContainer::AddResource (const Reference<XInterface>& rxResource)
126 : : {
127 [ + - ]: 16 : ::osl::MutexGuard aGuard (mpImpl->maMutex);
128 : :
129 : 16 : Implementation::XInterfaceResourceList::iterator iResource;
130 : : iResource = ::std::find (
131 : 16 : mpImpl->maXInterfaceResources.begin(),
132 : 16 : mpImpl->maXInterfaceResources.end(),
133 [ + - ]: 32 : rxResource);
134 [ + - ][ + - ]: 16 : if (iResource == mpImpl->maXInterfaceResources.end())
135 [ + - ]: 16 : mpImpl->maXInterfaceResources.push_back(rxResource);
136 : : else
137 : : {
138 : : DBG_ASSERT (false,
139 : : "SdGlobalResourceContainer:AddResource(): Resource added twice.");
140 [ + - ]: 16 : }
141 : 16 : }
142 : :
143 : :
144 : :
145 : 25 : SdGlobalResourceContainer::SdGlobalResourceContainer (void)
146 [ + - ]: 25 : : mpImpl (new SdGlobalResourceContainer::Implementation())
147 : : {
148 : 25 : Implementation::mpInstance = this;
149 : 25 : }
150 : :
151 : :
152 : :
153 : :
154 : 16 : SdGlobalResourceContainer::~SdGlobalResourceContainer (void)
155 : : {
156 [ + - ]: 16 : ::osl::MutexGuard aGuard (mpImpl->maMutex);
157 : :
158 : : // Release the resources in reversed order of their addition to the
159 : : // container. This is because a resource A added before resource B
160 : : // may have been created due to a request of B. Thus B depends on A and
161 : : // should be destroyed first.
162 : 16 : Implementation::ResourceList::reverse_iterator iResource;
163 [ + - + - ]: 80 : for (iResource = mpImpl->maResources.rbegin();
[ + + ]
164 : 40 : iResource != mpImpl->maResources.rend();
165 : : ++iResource)
166 : : {
167 [ + - ][ + - ]: 24 : delete *iResource;
[ + - ]
168 : : }
169 : :
170 : : // The SharedResourceList has not to be released manually. We just
171 : : // assert resources that are still held by someone other than us.
172 : 16 : Implementation::SharedResourceList::reverse_iterator iSharedResource;
173 [ + - + - ]: 292 : for (iSharedResource = mpImpl->maSharedResources.rbegin();
[ + + ]
174 : 146 : iSharedResource != mpImpl->maSharedResources.rend();
175 : : ++iSharedResource)
176 : : {
177 [ + - ][ - + ]: 130 : if ( ! iSharedResource->unique())
178 : : {
179 [ # # ]: 0 : SdGlobalResource* pResource = iSharedResource->get();
180 : : OSL_TRACE(" %p %d", pResource, iSharedResource->use_count());
181 : : DBG_ASSERT(iSharedResource->unique(),
182 : : "SdGlobalResource still held in ~SdGlobalResourceContainer");
183 : : }
184 : : }
185 : :
186 : 16 : Implementation::XInterfaceResourceList::reverse_iterator iXInterfaceResource;
187 [ + + ]: 64 : for (iXInterfaceResource = mpImpl->maXInterfaceResources.rbegin();
[ + - + - ]
188 : 32 : iXInterfaceResource != mpImpl->maXInterfaceResources.rend();
189 : : ++iXInterfaceResource)
190 : : {
191 [ + - ][ + - ]: 16 : Reference<lang::XComponent> xComponent (*iXInterfaceResource, UNO_QUERY);
192 [ + - ][ + - ]: 16 : *iXInterfaceResource = NULL;
193 [ - + ]: 16 : if (xComponent.is())
194 [ # # ][ # # ]: 0 : xComponent->dispose();
195 : 16 : }
196 : :
197 : : DBG_ASSERT(Implementation::mpInstance == this,
198 : : "~SdGlobalResourceContainer(): more than one instance of singleton");
199 [ + - ]: 16 : Implementation::mpInstance = NULL;
200 : 16 : }
201 : :
202 : :
203 : :
204 : :
205 : : } // end of namespace sd
206 : :
207 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|