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 0 : 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 0 : 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 0 : return *Implementation::mpInstance;
66 : }
67 :
68 : SdGlobalResourceContainer*
69 : SdGlobalResourceContainer::Implementation::mpInstance = NULL;
70 :
71 :
72 :
73 :
74 : //===== SdGlobalResourceContainer =============================================
75 :
76 0 : void SdGlobalResourceContainer::AddResource (
77 : ::std::auto_ptr<SdGlobalResource> pResource)
78 : {
79 0 : ::osl::MutexGuard aGuard (mpImpl->maMutex);
80 :
81 0 : Implementation::ResourceList::iterator iResource;
82 : iResource = ::std::find (
83 0 : mpImpl->maResources.begin(),
84 0 : mpImpl->maResources.end(),
85 0 : pResource.get());
86 0 : if (iResource == mpImpl->maResources.end())
87 0 : 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 0 : pResource.release();
98 0 : }
99 :
100 :
101 :
102 :
103 0 : void SdGlobalResourceContainer::AddResource (
104 : ::boost::shared_ptr<SdGlobalResource> pResource)
105 : {
106 0 : ::osl::MutexGuard aGuard (mpImpl->maMutex);
107 :
108 0 : Implementation::SharedResourceList::iterator iResource;
109 : iResource = ::std::find (
110 0 : mpImpl->maSharedResources.begin(),
111 0 : mpImpl->maSharedResources.end(),
112 0 : pResource);
113 0 : if (iResource == mpImpl->maSharedResources.end())
114 0 : mpImpl->maSharedResources.push_back(pResource);
115 : else
116 : {
117 : DBG_ASSERT (false,
118 : "SdGlobalResourceContainer:AddResource(): Resource added twice.");
119 0 : }
120 0 : }
121 :
122 :
123 :
124 :
125 0 : void SdGlobalResourceContainer::AddResource (const Reference<XInterface>& rxResource)
126 : {
127 0 : ::osl::MutexGuard aGuard (mpImpl->maMutex);
128 :
129 0 : Implementation::XInterfaceResourceList::iterator iResource;
130 : iResource = ::std::find (
131 0 : mpImpl->maXInterfaceResources.begin(),
132 0 : mpImpl->maXInterfaceResources.end(),
133 0 : rxResource);
134 0 : if (iResource == mpImpl->maXInterfaceResources.end())
135 0 : mpImpl->maXInterfaceResources.push_back(rxResource);
136 : else
137 : {
138 : DBG_ASSERT (false,
139 : "SdGlobalResourceContainer:AddResource(): Resource added twice.");
140 0 : }
141 0 : }
142 :
143 :
144 :
145 0 : SdGlobalResourceContainer::SdGlobalResourceContainer (void)
146 0 : : mpImpl (new SdGlobalResourceContainer::Implementation())
147 : {
148 0 : Implementation::mpInstance = this;
149 0 : }
150 :
151 :
152 :
153 :
154 0 : SdGlobalResourceContainer::~SdGlobalResourceContainer (void)
155 : {
156 0 : ::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 0 : Implementation::ResourceList::reverse_iterator iResource;
163 0 : for (iResource = mpImpl->maResources.rbegin();
164 0 : iResource != mpImpl->maResources.rend();
165 : ++iResource)
166 : {
167 0 : 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 0 : Implementation::SharedResourceList::reverse_iterator iSharedResource;
173 0 : for (iSharedResource = mpImpl->maSharedResources.rbegin();
174 0 : iSharedResource != mpImpl->maSharedResources.rend();
175 : ++iSharedResource)
176 : {
177 0 : 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 0 : Implementation::XInterfaceResourceList::reverse_iterator iXInterfaceResource;
187 0 : for (iXInterfaceResource = mpImpl->maXInterfaceResources.rbegin();
188 0 : iXInterfaceResource != mpImpl->maXInterfaceResources.rend();
189 : ++iXInterfaceResource)
190 : {
191 0 : Reference<lang::XComponent> xComponent (*iXInterfaceResource, UNO_QUERY);
192 0 : *iXInterfaceResource = NULL;
193 0 : if (xComponent.is())
194 0 : xComponent->dispose();
195 0 : }
196 :
197 : DBG_ASSERT(Implementation::mpInstance == this,
198 : "~SdGlobalResourceContainer(): more than one instance of singleton");
199 0 : Implementation::mpInstance = NULL;
200 0 : }
201 :
202 :
203 :
204 :
205 : } // end of namespace sd
206 :
207 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|