1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
35 using namespace ::com::sun::star
;
36 using namespace ::com::sun::star::uno
;
40 class SdGlobalResourceContainerInstance
41 : public comphelper::unique_disposing_solar_mutex_reset_ptr
<SdGlobalResourceContainer
>
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)
54 struct theSdGlobalResourceContainerInstance
: public rtl::Static
<SdGlobalResourceContainerInstance
, theSdGlobalResourceContainerInstance
> {};
58 //===== SdGlobalResourceContainer::Implementation =============================
60 class SdGlobalResourceContainer::Implementation
63 friend class SdGlobalResourceContainer
;
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
;
81 SdGlobalResourceContainer
& SdGlobalResourceContainer::Instance()
83 SdGlobalResourceContainer
*const pRet(theSdGlobalResourceContainerInstance::get().get());
84 assert(pRet
); // error if it has been deleted and is null
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(),
100 if (iResource
== mpImpl
->maResources
.end())
101 mpImpl
->maResources
.push_back(pResource
.get());
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.
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(),
124 if (iResource
== mpImpl
->maSharedResources
.end())
125 mpImpl
->maSharedResources
.push_back(pResource
);
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(),
142 if (iResource
== mpImpl
->maXInterfaceResources
.end())
143 mpImpl
->maXInterfaceResources
.push_back(rxResource
);
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();
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();
179 if (iSharedResource
->use_count() > 1)
181 SdGlobalResource
* pResource
= iSharedResource
->get();
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;
197 xComponent
->dispose();
200 sd::slidesorter::cache::CacheConfiguration::Shutdown();
203 } // end of namespace sd
205 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */