LibreOffice Module sd (master) 1
SdGlobalResourceContainer.cxx
Go to the documentation of this file.
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
21
22#include <../cache/SlsCacheConfiguration.hxx>
23
26
27#include <com/sun/star/frame/Desktop.hpp>
28
29#include <sal/log.hxx>
30#include <tools/debug.hxx>
31
32#include <algorithm>
33#include <memory>
34#include <mutex>
35#include <vector>
36
37using namespace ::com::sun::star;
38using namespace ::com::sun::star::uno;
39
40namespace sd {
41
43 : public comphelper::unique_disposing_solar_mutex_reset_ptr<SdGlobalResourceContainer>
44{
45public:
48 uno::Reference<lang::XComponent>(frame::Desktop::create(comphelper::getProcessComponentContext()), uno::UNO_QUERY_THROW),
50 {
51 }
52};
53
54namespace {
55
56SdGlobalResourceContainerInstance& theSdGlobalResourceContainerInstance()
57{
58 static SdGlobalResourceContainerInstance SINGLETON;
59 return SINGLETON;
60}
61
62} // namespace
63
64//===== SdGlobalResourceContainer::Implementation =============================
65
67{
68private:
70
71 std::mutex maMutex;
72
76 std::vector<std::unique_ptr<SdGlobalResource>> maResources;
77
78 typedef ::std::vector<std::shared_ptr<SdGlobalResource> > SharedResourceList;
80
81 typedef ::std::vector<Reference<XInterface> > XInterfaceResourceList;
83};
84
85// static
87{
88 SdGlobalResourceContainer *const pRet(theSdGlobalResourceContainerInstance().get());
89 assert(pRet); // error if it has been deleted and is null
90 return *pRet;
91}
92
93//===== SdGlobalResourceContainer =============================================
94
96 ::std::unique_ptr<SdGlobalResource> pResource)
97{
98 std::unique_lock aGuard (mpImpl->maMutex);
99
100 assert( std::none_of(
101 mpImpl->maResources.begin(),
102 mpImpl->maResources.end(),
103 [&](const std::unique_ptr<SdGlobalResource>& p) { return p == pResource; })
104 && "duplicate resource?");
105
106 mpImpl->maResources.push_back(std::move(pResource));
107}
108
110 const std::shared_ptr<SdGlobalResource>& pResource)
111{
112 std::unique_lock aGuard (mpImpl->maMutex);
113
114 Implementation::SharedResourceList::iterator iResource = ::std::find (
115 mpImpl->maSharedResources.begin(),
116 mpImpl->maSharedResources.end(),
117 pResource);
118 if (iResource == mpImpl->maSharedResources.end())
119 mpImpl->maSharedResources.push_back(pResource);
120 else
121 {
122 SAL_WARN ("sd.tools",
123 "SdGlobalResourceContainer:AddResource(): Resource added twice.");
124 }
125}
126
127void SdGlobalResourceContainer::AddResource (const Reference<XInterface>& rxResource)
128{
129 std::unique_lock aGuard (mpImpl->maMutex);
130
131 Implementation::XInterfaceResourceList::iterator iResource = ::std::find (
132 mpImpl->maXInterfaceResources.begin(),
133 mpImpl->maXInterfaceResources.end(),
134 rxResource);
135 if (iResource == mpImpl->maXInterfaceResources.end())
136 mpImpl->maXInterfaceResources.push_back(rxResource);
137 else
138 {
139 SAL_WARN ("sd.tools",
140 "SdGlobalResourceContainer:AddResource(): Resource added twice.");
141 }
142}
143
146{
147}
148
150{
151 std::unique_lock aGuard (mpImpl->maMutex);
152
153 // Release the resources in reversed order of their addition to the
154 // container. This is because a resource A added before resource B
155 // may have been created due to a request of B. Thus B depends on A and
156 // should be destroyed first.
157 for (auto iResource = mpImpl->maResources.rbegin();
158 iResource != mpImpl->maResources.rend();
159 ++iResource)
160 {
161 iResource->reset();
162 }
163
164
165 // The SharedResourceList has not to be released manually. We just
166 // assert resources that are still held by someone other than us.
167 Implementation::SharedResourceList::reverse_iterator iSharedResource;
168 for (iSharedResource = mpImpl->maSharedResources.rbegin();
169 iSharedResource != mpImpl->maSharedResources.rend();
170 ++iSharedResource)
171 {
172 if (iSharedResource->use_count() > 1)
173 {
174 SdGlobalResource* pResource = iSharedResource->get();
175 SAL_INFO(
176 "sd.tools", pResource << " " << iSharedResource->use_count());
177 DBG_ASSERT(iSharedResource->use_count() == 1,
178 "SdGlobalResource still held in ~SdGlobalResourceContainer");
179 }
180 }
181
182 Implementation::XInterfaceResourceList::reverse_iterator iXInterfaceResource;
183 for (iXInterfaceResource = mpImpl->maXInterfaceResources.rbegin();
184 iXInterfaceResource != mpImpl->maXInterfaceResources.rend();
185 ++iXInterfaceResource)
186 {
187 Reference<lang::XComponent> xComponent (*iXInterfaceResource, UNO_QUERY);
188 *iXInterfaceResource = nullptr;
189 if (xComponent.is())
190 xComponent->dispose();
191 }
192
194}
195
196} // end of namespace sd
197
198/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
unique_disposing_solar_mutex_reset_ptr(const css::uno::Reference< css::lang::XComponent > &rComponent, T *p=nullptr, bool bComponent=false)
::std::vector< Reference< XInterface > > XInterfaceResourceList
std::vector< std::unique_ptr< SdGlobalResource > > maResources
All instances of SdGlobalResource in this vector are owned by the container and will be destroyed whe...
::std::vector< std::shared_ptr< SdGlobalResource > > SharedResourceList
The purpose of this container is to hold references to resources that are globally available to all i...
void AddResource(::std::unique_ptr< SdGlobalResource > pResource)
Add a resource to the container.
static SdGlobalResourceContainer & Instance()
::std::unique_ptr< Implementation > mpImpl
#define DBG_ASSERT(sCon, aError)
void * p
#define SAL_WARN(area, stream)
#define SAL_INFO(area, stream)
Reference< XComponentContext > getProcessComponentContext()
css::uno::Reference< css::deployment::XPackageRegistry > create(css::uno::Reference< css::deployment::XPackageRegistry > const &xRootRegistry, OUString const &context, OUString const &cachePath, css::uno::Reference< css::uno::XComponentContext > const &xComponentContext)
Reference
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)