bump product version to 4.1.6.2
[LibreOffice.git] / sd / source / ui / tools / SdGlobalResourceContainer.cxx
blob655638ad1741dec45dcc91c1d0f651d3589adacc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "tools/SdGlobalResourceContainer.hxx"
23 #include <algorithm>
24 #include <vector>
26 using namespace ::com::sun::star;
27 using namespace ::com::sun::star::uno;
30 namespace sd {
33 //===== SdGlobalResourceContainer::Implementation =============================
35 class SdGlobalResourceContainer::Implementation
37 private:
38 friend class SdGlobalResourceContainer;
39 static SdGlobalResourceContainer* mpInstance;
41 ::osl::Mutex maMutex;
43 /** All instances of SdGlobalResource in this vector are owned by the
44 container and will be destroyed when the container is destroyed.
46 typedef ::std::vector<SdGlobalResource*> ResourceList;
47 ResourceList maResources;
49 typedef ::std::vector<boost::shared_ptr<SdGlobalResource> > SharedResourceList;
50 SharedResourceList maSharedResources;
52 typedef ::std::vector<Reference<XInterface> > XInterfaceResourceList;
53 XInterfaceResourceList maXInterfaceResources;
59 // static
60 SdGlobalResourceContainer& SdGlobalResourceContainer::Instance (void)
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 return *Implementation::mpInstance;
68 SdGlobalResourceContainer*
69 SdGlobalResourceContainer::Implementation::mpInstance = NULL;
74 //===== SdGlobalResourceContainer =============================================
76 void SdGlobalResourceContainer::AddResource (
77 ::std::auto_ptr<SdGlobalResource> pResource)
79 ::osl::MutexGuard aGuard (mpImpl->maMutex);
81 Implementation::ResourceList::iterator iResource;
82 iResource = ::std::find (
83 mpImpl->maResources.begin(),
84 mpImpl->maResources.end(),
85 pResource.get());
86 if (iResource == mpImpl->maResources.end())
87 mpImpl->maResources.push_back(pResource.get());
88 else
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.");
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 pResource.release();
103 void SdGlobalResourceContainer::AddResource (
104 ::boost::shared_ptr<SdGlobalResource> pResource)
106 ::osl::MutexGuard aGuard (mpImpl->maMutex);
108 Implementation::SharedResourceList::iterator iResource;
109 iResource = ::std::find (
110 mpImpl->maSharedResources.begin(),
111 mpImpl->maSharedResources.end(),
112 pResource);
113 if (iResource == mpImpl->maSharedResources.end())
114 mpImpl->maSharedResources.push_back(pResource);
115 else
117 DBG_ASSERT (false,
118 "SdGlobalResourceContainer:AddResource(): Resource added twice.");
125 void SdGlobalResourceContainer::AddResource (const Reference<XInterface>& rxResource)
127 ::osl::MutexGuard aGuard (mpImpl->maMutex);
129 Implementation::XInterfaceResourceList::iterator iResource;
130 iResource = ::std::find (
131 mpImpl->maXInterfaceResources.begin(),
132 mpImpl->maXInterfaceResources.end(),
133 rxResource);
134 if (iResource == mpImpl->maXInterfaceResources.end())
135 mpImpl->maXInterfaceResources.push_back(rxResource);
136 else
138 DBG_ASSERT (false,
139 "SdGlobalResourceContainer:AddResource(): Resource added twice.");
145 SdGlobalResourceContainer::SdGlobalResourceContainer (void)
146 : mpImpl (new SdGlobalResourceContainer::Implementation())
148 Implementation::mpInstance = this;
154 SdGlobalResourceContainer::~SdGlobalResourceContainer (void)
156 ::osl::MutexGuard aGuard (mpImpl->maMutex);
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 Implementation::ResourceList::reverse_iterator iResource;
163 for (iResource = mpImpl->maResources.rbegin();
164 iResource != mpImpl->maResources.rend();
165 ++iResource)
167 delete *iResource;
170 // The SharedResourceList has not to be released manually. We just
171 // assert resources that are still held by someone other than us.
172 Implementation::SharedResourceList::reverse_iterator iSharedResource;
173 for (iSharedResource = mpImpl->maSharedResources.rbegin();
174 iSharedResource != mpImpl->maSharedResources.rend();
175 ++iSharedResource)
177 if ( ! iSharedResource->unique())
179 SdGlobalResource* pResource = iSharedResource->get();
180 OSL_TRACE(" %p %d", pResource, iSharedResource->use_count());
181 DBG_ASSERT(iSharedResource->unique(),
182 "SdGlobalResource still held in ~SdGlobalResourceContainer");
186 Implementation::XInterfaceResourceList::reverse_iterator iXInterfaceResource;
187 for (iXInterfaceResource = mpImpl->maXInterfaceResources.rbegin();
188 iXInterfaceResource != mpImpl->maXInterfaceResources.rend();
189 ++iXInterfaceResource)
191 Reference<lang::XComponent> xComponent (*iXInterfaceResource, UNO_QUERY);
192 *iXInterfaceResource = NULL;
193 if (xComponent.is())
194 xComponent->dispose();
197 DBG_ASSERT(Implementation::mpInstance == this,
198 "~SdGlobalResourceContainer(): more than one instance of singleton");
199 Implementation::mpInstance = NULL;
205 } // end of namespace sd
207 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */