Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sd / source / ui / tools / SdGlobalResourceContainer.cxx
bloba8c07915b54c9f3c508207c5c8afa540aea81ac3
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 .
20 #include <tools/SdGlobalResourceContainer.hxx>
22 #include <../cache/SlsCacheConfiguration.hxx>
24 #include <comphelper/processfactory.hxx>
25 #include <comphelper/unique_disposing_ptr.hxx>
27 #include <com/sun/star/frame/Desktop.hpp>
29 #include <rtl/instance.hxx>
31 #include <algorithm>
32 #include <memory>
33 #include <vector>
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::uno;
38 namespace sd {
40 class SdGlobalResourceContainerInstance
41 : public comphelper::unique_disposing_solar_mutex_reset_ptr<SdGlobalResourceContainer>
43 public:
44 SdGlobalResourceContainerInstance()
45 : comphelper::unique_disposing_solar_mutex_reset_ptr<SdGlobalResourceContainer>(
46 uno::Reference<lang::XComponent>(frame::Desktop::create(comphelper::getProcessComponentContext()), uno::UNO_QUERY_THROW),
47 new SdGlobalResourceContainer, true)
52 namespace {
54 struct theSdGlobalResourceContainerInstance : public rtl::Static<SdGlobalResourceContainerInstance, theSdGlobalResourceContainerInstance> {};
56 } // namespace
58 //===== SdGlobalResourceContainer::Implementation =============================
60 class SdGlobalResourceContainer::Implementation
62 private:
63 friend class SdGlobalResourceContainer;
65 ::osl::Mutex maMutex;
67 /** All instances of SdGlobalResource in this vector are owned by the
68 container and will be destroyed when the container is destroyed.
70 typedef ::std::vector<SdGlobalResource*> ResourceList;
71 ResourceList maResources;
73 typedef ::std::vector<std::shared_ptr<SdGlobalResource> > SharedResourceList;
74 SharedResourceList maSharedResources;
76 typedef ::std::vector<Reference<XInterface> > XInterfaceResourceList;
77 XInterfaceResourceList maXInterfaceResources;
80 // static
81 SdGlobalResourceContainer& SdGlobalResourceContainer::Instance()
83 SdGlobalResourceContainer *const pRet(theSdGlobalResourceContainerInstance::get().get());
84 assert(pRet); // error if it has been deleted and is null
85 return *pRet;
88 //===== SdGlobalResourceContainer =============================================
90 void SdGlobalResourceContainer::AddResource (
91 ::std::unique_ptr<SdGlobalResource> && pResource)
93 ::osl::MutexGuard aGuard (mpImpl->maMutex);
95 Implementation::ResourceList::iterator iResource;
96 iResource = ::std::find (
97 mpImpl->maResources.begin(),
98 mpImpl->maResources.end(),
99 pResource.get());
100 if (iResource == mpImpl->maResources.end())
101 mpImpl->maResources.push_back(pResource.get());
102 else
104 // Because the given resource is a unique_ptr it is highly unlikely
105 // that we come here. But who knows?
106 SAL_WARN ( "sd.tools",
107 "SdGlobalResourceContainer:AddResource(): Resource added twice.");
109 // We can not put the unique_ptr into the vector so we release the
110 // unique_ptr and document that we take ownership explicitly.
111 pResource.release();
114 void SdGlobalResourceContainer::AddResource (
115 const std::shared_ptr<SdGlobalResource>& pResource)
117 ::osl::MutexGuard aGuard (mpImpl->maMutex);
119 Implementation::SharedResourceList::iterator iResource;
120 iResource = ::std::find (
121 mpImpl->maSharedResources.begin(),
122 mpImpl->maSharedResources.end(),
123 pResource);
124 if (iResource == mpImpl->maSharedResources.end())
125 mpImpl->maSharedResources.push_back(pResource);
126 else
128 SAL_WARN ("sd.tools",
129 "SdGlobalResourceContainer:AddResource(): Resource added twice.");
133 void SdGlobalResourceContainer::AddResource (const Reference<XInterface>& rxResource)
135 ::osl::MutexGuard aGuard (mpImpl->maMutex);
137 Implementation::XInterfaceResourceList::iterator iResource;
138 iResource = ::std::find (
139 mpImpl->maXInterfaceResources.begin(),
140 mpImpl->maXInterfaceResources.end(),
141 rxResource);
142 if (iResource == mpImpl->maXInterfaceResources.end())
143 mpImpl->maXInterfaceResources.push_back(rxResource);
144 else
146 SAL_WARN ("sd.tools",
147 "SdGlobalResourceContainer:AddResource(): Resource added twice.");
151 SdGlobalResourceContainer::SdGlobalResourceContainer()
152 : mpImpl (new SdGlobalResourceContainer::Implementation)
156 SdGlobalResourceContainer::~SdGlobalResourceContainer()
158 ::osl::MutexGuard aGuard (mpImpl->maMutex);
160 // Release the resources in reversed order of their addition to the
161 // container. This is because a resource A added before resource B
162 // may have been created due to a request of B. Thus B depends on A and
163 // should be destroyed first.
164 Implementation::ResourceList::reverse_iterator iResource;
165 for (iResource = mpImpl->maResources.rbegin();
166 iResource != mpImpl->maResources.rend();
167 ++iResource)
169 delete *iResource;
172 // The SharedResourceList has not to be released manually. We just
173 // assert resources that are still held by someone other than us.
174 Implementation::SharedResourceList::reverse_iterator iSharedResource;
175 for (iSharedResource = mpImpl->maSharedResources.rbegin();
176 iSharedResource != mpImpl->maSharedResources.rend();
177 ++iSharedResource)
179 if (iSharedResource->use_count() > 1)
181 SdGlobalResource* pResource = iSharedResource->get();
182 SAL_INFO(
183 "sd.tools", pResource << " " << iSharedResource->use_count());
184 DBG_ASSERT(iSharedResource->use_count() == 1,
185 "SdGlobalResource still held in ~SdGlobalResourceContainer");
189 Implementation::XInterfaceResourceList::reverse_iterator iXInterfaceResource;
190 for (iXInterfaceResource = mpImpl->maXInterfaceResources.rbegin();
191 iXInterfaceResource != mpImpl->maXInterfaceResources.rend();
192 ++iXInterfaceResource)
194 Reference<lang::XComponent> xComponent (*iXInterfaceResource, UNO_QUERY);
195 *iXInterfaceResource = nullptr;
196 if (xComponent.is())
197 xComponent->dispose();
200 sd::slidesorter::cache::CacheConfiguration::Shutdown();
203 } // end of namespace sd
205 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */