Bump version to 21.06.18.1
[LibreOffice.git] / sd / source / ui / tools / SdGlobalResourceContainer.cxx
blobda46c1d986729d8b5ec49277e9bd738d1525c23d
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>
30 #include <sal/log.hxx>
31 #include <tools/debug.hxx>
33 #include <algorithm>
34 #include <memory>
35 #include <vector>
37 using namespace ::com::sun::star;
38 using namespace ::com::sun::star::uno;
40 namespace sd {
42 class SdGlobalResourceContainerInstance
43 : public comphelper::unique_disposing_solar_mutex_reset_ptr<SdGlobalResourceContainer>
45 public:
46 SdGlobalResourceContainerInstance()
47 : comphelper::unique_disposing_solar_mutex_reset_ptr<SdGlobalResourceContainer>(
48 uno::Reference<lang::XComponent>(frame::Desktop::create(comphelper::getProcessComponentContext()), uno::UNO_QUERY_THROW),
49 new SdGlobalResourceContainer, true)
54 namespace {
56 struct theSdGlobalResourceContainerInstance : public rtl::Static<SdGlobalResourceContainerInstance, theSdGlobalResourceContainerInstance> {};
58 } // namespace
60 //===== SdGlobalResourceContainer::Implementation =============================
62 class SdGlobalResourceContainer::Implementation
64 private:
65 friend class SdGlobalResourceContainer;
67 ::osl::Mutex maMutex;
69 /** All instances of SdGlobalResource in this vector are owned by the
70 container and will be destroyed when the container is destroyed.
72 std::vector<std::unique_ptr<SdGlobalResource>> maResources;
74 typedef ::std::vector<std::shared_ptr<SdGlobalResource> > SharedResourceList;
75 SharedResourceList maSharedResources;
77 typedef ::std::vector<Reference<XInterface> > XInterfaceResourceList;
78 XInterfaceResourceList maXInterfaceResources;
81 // static
82 SdGlobalResourceContainer& SdGlobalResourceContainer::Instance()
84 SdGlobalResourceContainer *const pRet(theSdGlobalResourceContainerInstance::get().get());
85 assert(pRet); // error if it has been deleted and is null
86 return *pRet;
89 //===== SdGlobalResourceContainer =============================================
91 void SdGlobalResourceContainer::AddResource (
92 ::std::unique_ptr<SdGlobalResource> pResource)
94 ::osl::MutexGuard aGuard (mpImpl->maMutex);
96 assert( std::none_of(
97 mpImpl->maResources.begin(),
98 mpImpl->maResources.end(),
99 [&](const std::unique_ptr<SdGlobalResource>& p) { return p == pResource; })
100 && "duplicate resource?");
102 mpImpl->maResources.push_back(std::move(pResource));
105 void SdGlobalResourceContainer::AddResource (
106 const std::shared_ptr<SdGlobalResource>& pResource)
108 ::osl::MutexGuard aGuard (mpImpl->maMutex);
110 Implementation::SharedResourceList::iterator iResource = ::std::find (
111 mpImpl->maSharedResources.begin(),
112 mpImpl->maSharedResources.end(),
113 pResource);
114 if (iResource == mpImpl->maSharedResources.end())
115 mpImpl->maSharedResources.push_back(pResource);
116 else
118 SAL_WARN ("sd.tools",
119 "SdGlobalResourceContainer:AddResource(): Resource added twice.");
123 void SdGlobalResourceContainer::AddResource (const Reference<XInterface>& rxResource)
125 ::osl::MutexGuard aGuard (mpImpl->maMutex);
127 Implementation::XInterfaceResourceList::iterator iResource = ::std::find (
128 mpImpl->maXInterfaceResources.begin(),
129 mpImpl->maXInterfaceResources.end(),
130 rxResource);
131 if (iResource == mpImpl->maXInterfaceResources.end())
132 mpImpl->maXInterfaceResources.push_back(rxResource);
133 else
135 SAL_WARN ("sd.tools",
136 "SdGlobalResourceContainer:AddResource(): Resource added twice.");
140 SdGlobalResourceContainer::SdGlobalResourceContainer()
141 : mpImpl (new SdGlobalResourceContainer::Implementation)
145 SdGlobalResourceContainer::~SdGlobalResourceContainer()
147 ::osl::MutexGuard aGuard (mpImpl->maMutex);
149 // Release the resources in reversed order of their addition to the
150 // container. This is because a resource A added before resource B
151 // may have been created due to a request of B. Thus B depends on A and
152 // should be destroyed first.
153 for (auto iResource = mpImpl->maResources.rbegin();
154 iResource != mpImpl->maResources.rend();
155 ++iResource)
157 iResource->reset();
161 // The SharedResourceList has not to be released manually. We just
162 // assert resources that are still held by someone other than us.
163 Implementation::SharedResourceList::reverse_iterator iSharedResource;
164 for (iSharedResource = mpImpl->maSharedResources.rbegin();
165 iSharedResource != mpImpl->maSharedResources.rend();
166 ++iSharedResource)
168 if (iSharedResource->use_count() > 1)
170 SdGlobalResource* pResource = iSharedResource->get();
171 SAL_INFO(
172 "sd.tools", pResource << " " << iSharedResource->use_count());
173 DBG_ASSERT(iSharedResource->use_count() == 1,
174 "SdGlobalResource still held in ~SdGlobalResourceContainer");
178 Implementation::XInterfaceResourceList::reverse_iterator iXInterfaceResource;
179 for (iXInterfaceResource = mpImpl->maXInterfaceResources.rbegin();
180 iXInterfaceResource != mpImpl->maXInterfaceResources.rend();
181 ++iXInterfaceResource)
183 Reference<lang::XComponent> xComponent (*iXInterfaceResource, UNO_QUERY);
184 *iXInterfaceResource = nullptr;
185 if (xComponent.is())
186 xComponent->dispose();
189 sd::slidesorter::cache::CacheConfiguration::Shutdown();
192 } // end of namespace sd
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */