Drop CacheConfiguration in favor of officecfg
[LibreOffice.git] / sd / source / ui / tools / SdGlobalResourceContainer.cxx
blob16ea15dcb63e3bd4b42dd9a49685508efaeba663
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 <comphelper/processfactory.hxx>
23 #include <comphelper/unique_disposing_ptr.hxx>
25 #include <com/sun/star/frame/Desktop.hpp>
27 #include <sal/log.hxx>
28 #include <tools/debug.hxx>
30 #include <algorithm>
31 #include <memory>
32 #include <mutex>
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 SdGlobalResourceContainerInstance& theSdGlobalResourceContainerInstance()
56 static SdGlobalResourceContainerInstance SINGLETON;
57 return SINGLETON;
60 } // namespace
62 //===== SdGlobalResourceContainer::Implementation =============================
64 class SdGlobalResourceContainer::Implementation
66 private:
67 friend class SdGlobalResourceContainer;
69 std::mutex maMutex;
71 /** All instances of SdGlobalResource in this vector are owned by the
72 container and will be destroyed when the container is destroyed.
74 std::vector<std::unique_ptr<SdGlobalResource>> maResources;
76 typedef ::std::vector<std::shared_ptr<SdGlobalResource> > SharedResourceList;
77 SharedResourceList maSharedResources;
79 typedef ::std::vector<Reference<XInterface> > XInterfaceResourceList;
80 XInterfaceResourceList maXInterfaceResources;
83 // static
84 SdGlobalResourceContainer& SdGlobalResourceContainer::Instance()
86 SdGlobalResourceContainer *const pRet(theSdGlobalResourceContainerInstance().get());
87 assert(pRet); // error if it has been deleted and is null
88 return *pRet;
91 //===== SdGlobalResourceContainer =============================================
93 void SdGlobalResourceContainer::AddResource (
94 ::std::unique_ptr<SdGlobalResource> pResource)
96 std::unique_lock aGuard (mpImpl->maMutex);
98 assert( std::none_of(
99 mpImpl->maResources.begin(),
100 mpImpl->maResources.end(),
101 [&](const std::unique_ptr<SdGlobalResource>& p) { return p == pResource; })
102 && "duplicate resource?");
104 mpImpl->maResources.push_back(std::move(pResource));
107 void SdGlobalResourceContainer::AddResource (
108 const std::shared_ptr<SdGlobalResource>& pResource)
110 std::unique_lock aGuard (mpImpl->maMutex);
112 Implementation::SharedResourceList::iterator iResource = ::std::find (
113 mpImpl->maSharedResources.begin(),
114 mpImpl->maSharedResources.end(),
115 pResource);
116 if (iResource == mpImpl->maSharedResources.end())
117 mpImpl->maSharedResources.push_back(pResource);
118 else
120 SAL_WARN ("sd.tools",
121 "SdGlobalResourceContainer:AddResource(): Resource added twice.");
125 void SdGlobalResourceContainer::AddResource (const Reference<XInterface>& rxResource)
127 std::unique_lock aGuard (mpImpl->maMutex);
129 Implementation::XInterfaceResourceList::iterator iResource = ::std::find (
130 mpImpl->maXInterfaceResources.begin(),
131 mpImpl->maXInterfaceResources.end(),
132 rxResource);
133 if (iResource == mpImpl->maXInterfaceResources.end())
134 mpImpl->maXInterfaceResources.push_back(rxResource);
135 else
137 SAL_WARN ("sd.tools",
138 "SdGlobalResourceContainer:AddResource(): Resource added twice.");
142 SdGlobalResourceContainer::SdGlobalResourceContainer()
143 : mpImpl (new SdGlobalResourceContainer::Implementation)
147 SdGlobalResourceContainer::~SdGlobalResourceContainer()
149 std::unique_lock aGuard (mpImpl->maMutex);
151 // Release the resources in reversed order of their addition to the
152 // container. This is because a resource A added before resource B
153 // may have been created due to a request of B. Thus B depends on A and
154 // should be destroyed first.
155 for (auto iResource = mpImpl->maResources.rbegin();
156 iResource != mpImpl->maResources.rend();
157 ++iResource)
159 iResource->reset();
163 // The SharedResourceList has not to be released manually. We just
164 // assert resources that are still held by someone other than us.
165 Implementation::SharedResourceList::reverse_iterator iSharedResource;
166 for (iSharedResource = mpImpl->maSharedResources.rbegin();
167 iSharedResource != mpImpl->maSharedResources.rend();
168 ++iSharedResource)
170 if (iSharedResource->use_count() > 1)
172 SdGlobalResource* pResource = iSharedResource->get();
173 SAL_INFO(
174 "sd.tools", pResource << " " << iSharedResource->use_count());
175 DBG_ASSERT(iSharedResource->use_count() == 1,
176 "SdGlobalResource still held in ~SdGlobalResourceContainer");
180 Implementation::XInterfaceResourceList::reverse_iterator iXInterfaceResource;
181 for (iXInterfaceResource = mpImpl->maXInterfaceResources.rbegin();
182 iXInterfaceResource != mpImpl->maXInterfaceResources.rend();
183 ++iXInterfaceResource)
185 Reference<lang::XComponent> xComponent (*iXInterfaceResource, UNO_QUERY);
186 *iXInterfaceResource = nullptr;
187 if (xComponent.is())
188 xComponent->dispose();
192 } // end of namespace sd
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */