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 <o3tl/deleter.hxx>
30 #include <rtl/instance.hxx>
31 #include <sal/log.hxx>
32 #include <tools/debug.hxx>
38 using namespace ::com::sun::star
;
39 using namespace ::com::sun::star::uno
;
43 class SdGlobalResourceContainerInstance
44 : public comphelper::unique_disposing_solar_mutex_reset_ptr
<SdGlobalResourceContainer
>
47 SdGlobalResourceContainerInstance()
48 : comphelper::unique_disposing_solar_mutex_reset_ptr
<SdGlobalResourceContainer
>(
49 uno::Reference
<lang::XComponent
>(frame::Desktop::create(comphelper::getProcessComponentContext()), uno::UNO_QUERY_THROW
),
50 new SdGlobalResourceContainer
, true)
57 struct theSdGlobalResourceContainerInstance
: public rtl::Static
<SdGlobalResourceContainerInstance
, theSdGlobalResourceContainerInstance
> {};
61 //===== SdGlobalResourceContainer::Implementation =============================
63 class SdGlobalResourceContainer::Implementation
66 friend class SdGlobalResourceContainer
;
70 /** All instances of SdGlobalResource in this vector are owned by the
71 container and will be destroyed when the container is destroyed.
73 std::vector
<std::unique_ptr
<SdGlobalResource
>> maResources
;
75 typedef ::std::vector
<std::shared_ptr
<SdGlobalResource
> > SharedResourceList
;
76 SharedResourceList maSharedResources
;
78 typedef ::std::vector
<Reference
<XInterface
> > XInterfaceResourceList
;
79 XInterfaceResourceList maXInterfaceResources
;
83 SdGlobalResourceContainer
& SdGlobalResourceContainer::Instance()
85 SdGlobalResourceContainer
*const pRet(theSdGlobalResourceContainerInstance::get().get());
86 assert(pRet
); // error if it has been deleted and is null
90 //===== SdGlobalResourceContainer =============================================
92 void SdGlobalResourceContainer::AddResource (
93 ::std::unique_ptr
<SdGlobalResource
> pResource
)
95 ::osl::MutexGuard
aGuard (mpImpl
->maMutex
);
98 mpImpl
->maResources
.begin(),
99 mpImpl
->maResources
.end(),
100 [&](const std::unique_ptr
<SdGlobalResource
>& p
) { return p
== pResource
; })
101 && "duplicate resource?");
103 mpImpl
->maResources
.push_back(std::move(pResource
));
106 void SdGlobalResourceContainer::AddResource (
107 const std::shared_ptr
<SdGlobalResource
>& pResource
)
109 ::osl::MutexGuard
aGuard (mpImpl
->maMutex
);
111 Implementation::SharedResourceList::iterator iResource
= ::std::find (
112 mpImpl
->maSharedResources
.begin(),
113 mpImpl
->maSharedResources
.end(),
115 if (iResource
== mpImpl
->maSharedResources
.end())
116 mpImpl
->maSharedResources
.push_back(pResource
);
119 SAL_WARN ("sd.tools",
120 "SdGlobalResourceContainer:AddResource(): Resource added twice.");
124 void SdGlobalResourceContainer::AddResource (const Reference
<XInterface
>& rxResource
)
126 ::osl::MutexGuard
aGuard (mpImpl
->maMutex
);
128 Implementation::XInterfaceResourceList::iterator iResource
= ::std::find (
129 mpImpl
->maXInterfaceResources
.begin(),
130 mpImpl
->maXInterfaceResources
.end(),
132 if (iResource
== mpImpl
->maXInterfaceResources
.end())
133 mpImpl
->maXInterfaceResources
.push_back(rxResource
);
136 SAL_WARN ("sd.tools",
137 "SdGlobalResourceContainer:AddResource(): Resource added twice.");
141 SdGlobalResourceContainer::SdGlobalResourceContainer()
142 : mpImpl (new SdGlobalResourceContainer::Implementation
)
146 SdGlobalResourceContainer::~SdGlobalResourceContainer()
148 ::osl::MutexGuard
aGuard (mpImpl
->maMutex
);
150 // Release the resources in reversed order of their addition to the
151 // container. This is because a resource A added before resource B
152 // may have been created due to a request of B. Thus B depends on A and
153 // should be destroyed first.
154 for (auto iResource
= mpImpl
->maResources
.rbegin();
155 iResource
!= mpImpl
->maResources
.rend();
162 // The SharedResourceList has not to be released manually. We just
163 // assert resources that are still held by someone other than us.
164 Implementation::SharedResourceList::reverse_iterator iSharedResource
;
165 for (iSharedResource
= mpImpl
->maSharedResources
.rbegin();
166 iSharedResource
!= mpImpl
->maSharedResources
.rend();
169 if (iSharedResource
->use_count() > 1)
171 SdGlobalResource
* pResource
= iSharedResource
->get();
173 "sd.tools", pResource
<< " " << iSharedResource
->use_count());
174 DBG_ASSERT(iSharedResource
->use_count() == 1,
175 "SdGlobalResource still held in ~SdGlobalResourceContainer");
179 Implementation::XInterfaceResourceList::reverse_iterator iXInterfaceResource
;
180 for (iXInterfaceResource
= mpImpl
->maXInterfaceResources
.rbegin();
181 iXInterfaceResource
!= mpImpl
->maXInterfaceResources
.rend();
182 ++iXInterfaceResource
)
184 Reference
<lang::XComponent
> xComponent (*iXInterfaceResource
, UNO_QUERY
);
185 *iXInterfaceResource
= nullptr;
187 xComponent
->dispose();
190 sd::slidesorter::cache::CacheConfiguration::Shutdown();
193 } // end of namespace sd
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */