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 <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>
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 SdGlobalResourceContainerInstance
& theSdGlobalResourceContainerInstance()
56 static SdGlobalResourceContainerInstance SINGLETON
;
62 //===== SdGlobalResourceContainer::Implementation =============================
64 class SdGlobalResourceContainer::Implementation
67 friend class SdGlobalResourceContainer
;
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
;
84 SdGlobalResourceContainer
& SdGlobalResourceContainer::Instance()
86 SdGlobalResourceContainer
*const pRet(theSdGlobalResourceContainerInstance().get());
87 assert(pRet
); // error if it has been deleted and is null
91 //===== SdGlobalResourceContainer =============================================
93 void SdGlobalResourceContainer::AddResource (
94 ::std::unique_ptr
<SdGlobalResource
> pResource
)
96 std::unique_lock
aGuard (mpImpl
->maMutex
);
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(),
116 if (iResource
== mpImpl
->maSharedResources
.end())
117 mpImpl
->maSharedResources
.push_back(pResource
);
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(),
133 if (iResource
== mpImpl
->maXInterfaceResources
.end())
134 mpImpl
->maXInterfaceResources
.push_back(rxResource
);
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();
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();
170 if (iSharedResource
->use_count() > 1)
172 SdGlobalResource
* pResource
= iSharedResource
->get();
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;
188 xComponent
->dispose();
192 } // end of namespace sd
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */