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 <sal/log.hxx>
30 #include <tools/debug.hxx>
37 using namespace ::com::sun::star
;
38 using namespace ::com::sun::star::uno
;
42 class SdGlobalResourceContainerInstance
43 : public comphelper::unique_disposing_solar_mutex_reset_ptr
<SdGlobalResourceContainer
>
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)
56 SdGlobalResourceContainerInstance
& theSdGlobalResourceContainerInstance()
58 static SdGlobalResourceContainerInstance SINGLETON
;
64 //===== SdGlobalResourceContainer::Implementation =============================
66 class SdGlobalResourceContainer::Implementation
69 friend class SdGlobalResourceContainer
;
73 /** All instances of SdGlobalResource in this vector are owned by the
74 container and will be destroyed when the container is destroyed.
76 std::vector
<std::unique_ptr
<SdGlobalResource
>> maResources
;
78 typedef ::std::vector
<std::shared_ptr
<SdGlobalResource
> > SharedResourceList
;
79 SharedResourceList maSharedResources
;
81 typedef ::std::vector
<Reference
<XInterface
> > XInterfaceResourceList
;
82 XInterfaceResourceList maXInterfaceResources
;
86 SdGlobalResourceContainer
& SdGlobalResourceContainer::Instance()
88 SdGlobalResourceContainer
*const pRet(theSdGlobalResourceContainerInstance().get());
89 assert(pRet
); // error if it has been deleted and is null
93 //===== SdGlobalResourceContainer =============================================
95 void SdGlobalResourceContainer::AddResource (
96 ::std::unique_ptr
<SdGlobalResource
> pResource
)
98 std::unique_lock
aGuard (mpImpl
->maMutex
);
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?");
106 mpImpl
->maResources
.push_back(std::move(pResource
));
109 void SdGlobalResourceContainer::AddResource (
110 const std::shared_ptr
<SdGlobalResource
>& pResource
)
112 std::unique_lock
aGuard (mpImpl
->maMutex
);
114 Implementation::SharedResourceList::iterator iResource
= ::std::find (
115 mpImpl
->maSharedResources
.begin(),
116 mpImpl
->maSharedResources
.end(),
118 if (iResource
== mpImpl
->maSharedResources
.end())
119 mpImpl
->maSharedResources
.push_back(pResource
);
122 SAL_WARN ("sd.tools",
123 "SdGlobalResourceContainer:AddResource(): Resource added twice.");
127 void SdGlobalResourceContainer::AddResource (const Reference
<XInterface
>& rxResource
)
129 std::unique_lock
aGuard (mpImpl
->maMutex
);
131 Implementation::XInterfaceResourceList::iterator iResource
= ::std::find (
132 mpImpl
->maXInterfaceResources
.begin(),
133 mpImpl
->maXInterfaceResources
.end(),
135 if (iResource
== mpImpl
->maXInterfaceResources
.end())
136 mpImpl
->maXInterfaceResources
.push_back(rxResource
);
139 SAL_WARN ("sd.tools",
140 "SdGlobalResourceContainer:AddResource(): Resource added twice.");
144 SdGlobalResourceContainer::SdGlobalResourceContainer()
145 : mpImpl (new SdGlobalResourceContainer::Implementation
)
149 SdGlobalResourceContainer::~SdGlobalResourceContainer()
151 std::unique_lock
aGuard (mpImpl
->maMutex
);
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();
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();
172 if (iSharedResource
->use_count() > 1)
174 SdGlobalResource
* pResource
= iSharedResource
->get();
176 "sd.tools", pResource
<< " " << iSharedResource
->use_count());
177 DBG_ASSERT(iSharedResource
->use_count() == 1,
178 "SdGlobalResource still held in ~SdGlobalResourceContainer");
182 Implementation::XInterfaceResourceList::reverse_iterator iXInterfaceResource
;
183 for (iXInterfaceResource
= mpImpl
->maXInterfaceResources
.rbegin();
184 iXInterfaceResource
!= mpImpl
->maXInterfaceResources
.rend();
185 ++iXInterfaceResource
)
187 Reference
<lang::XComponent
> xComponent (*iXInterfaceResource
, UNO_QUERY
);
188 *iXInterfaceResource
= nullptr;
190 xComponent
->dispose();
193 sd::slidesorter::cache::CacheConfiguration::Shutdown();
196 } // end of namespace sd
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */