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/.
10 #ifndef INCLUDED_COMPHELPER_UNIQUE_DISPOSING_PTR_HXX
11 #define INCLUDED_COMPHELPER_UNIQUE_DISPOSING_PTR_HXX
14 #include <cppuhelper/implbase.hxx>
16 #include <com/sun/star/lang/XComponent.hpp>
17 #include <com/sun/star/frame/XDesktop.hpp>
18 #include <com/sun/star/lang/XServiceInfo.hpp>
20 #include <o3tl/deleter.hxx>
22 #include <vcl/svapp.hxx>
26 //Similar to std::unique_ptr, except additionally releases the ptr on XComponent::disposing and/or XTerminateListener::notifyTermination if supported
27 template<class T
> class unique_disposing_ptr
30 std::unique_ptr
<T
, o3tl::default_delete
<T
>> m_xItem
;
31 css::uno::Reference
< css::frame::XTerminateListener
> m_xTerminateListener
;
33 unique_disposing_ptr(const unique_disposing_ptr
&) = delete;
34 unique_disposing_ptr
& operator=(const unique_disposing_ptr
&) = delete;
36 unique_disposing_ptr( const css::uno::Reference
< css::lang::XComponent
> &rComponent
, T
* p
= nullptr, bool bComponent
= false)
39 m_xTerminateListener
= new TerminateListener(rComponent
, *this, bComponent
);
42 virtual void reset(T
* p
= nullptr)
57 T
* operator->() const
62 operator bool () const
64 return static_cast< bool >(m_xItem
);
67 virtual ~unique_disposing_ptr() COVERITY_NOEXCEPT_FALSE
72 class TerminateListener final
: public ::cppu::WeakImplHelper
< css::frame::XTerminateListener
,
73 css::lang::XServiceInfo
>
76 css::uno::Reference
< css::lang::XComponent
> m_xComponent
;
77 unique_disposing_ptr
<T
>& m_rItem
;
78 bool const mbComponentDLL
;
80 TerminateListener(css::uno::Reference
< css::lang::XComponent
> xComponent
,
81 unique_disposing_ptr
<T
>& rItem
, bool bComponentDLL
) :
82 m_xComponent(std::move(xComponent
)),
84 mbComponentDLL(bComponentDLL
)
86 if (m_xComponent
.is())
88 css::uno::Reference
< css::frame::XDesktop
> xDesktop(m_xComponent
, css::uno::UNO_QUERY
);
90 xDesktop
->addTerminateListener(this);
92 m_xComponent
->addEventListener(this);
96 virtual ~TerminateListener() override
98 if ( m_xComponent
.is() )
100 css::uno::Reference
< css::frame::XDesktop
> xDesktop(m_xComponent
, css::uno::UNO_QUERY
);
102 xDesktop
->removeTerminateListener(this);
104 m_xComponent
->removeEventListener(this);
109 virtual void SAL_CALL
disposing( const css::lang::EventObject
& rEvt
) override
111 bool shutDown
= (rEvt
.Source
== m_xComponent
);
113 if (shutDown
&& m_xComponent
.is())
115 css::uno::Reference
< css::frame::XDesktop
> xDesktop(m_xComponent
, css::uno::UNO_QUERY
);
117 xDesktop
->removeTerminateListener(this);
119 m_xComponent
->removeEventListener(this);
120 m_xComponent
.clear();
127 // XTerminateListener
128 virtual void SAL_CALL
queryTermination( const css::lang::EventObject
& ) override
132 virtual void SAL_CALL
notifyTermination( const css::lang::EventObject
& rEvt
) override
137 virtual OUString SAL_CALL
getImplementationName() override
140 return "com.sun.star.comp.ComponentDLLListener";
142 return "com.sun.star.comp.DisposingTerminateListener";
145 virtual sal_Bool SAL_CALL
supportsService(const OUString
& /*rName*/) override
150 virtual css::uno::Sequence
<OUString
> SAL_CALL
getSupportedServiceNames() override
152 return css::uno::Sequence
<OUString
>();
157 //Something like an OutputDevice requires the SolarMutex to be taken before use
158 //for threadsafety. The user can ensure this, except in the case of its dtor
159 //being called from reset due to a terminate on the XComponent being called
160 //from an arbitrary thread
161 template<class T
> class unique_disposing_solar_mutex_reset_ptr
162 : public unique_disposing_ptr
<T
>
165 unique_disposing_solar_mutex_reset_ptr( const css::uno::Reference
< css::lang::XComponent
> &rComponent
, T
* p
= nullptr, bool bComponent
= false)
166 : unique_disposing_ptr
<T
>(rComponent
, p
, bComponent
)
170 virtual void reset(T
* p
= nullptr) override
172 SolarMutexGuard aGuard
;
173 unique_disposing_ptr
<T
>::reset(p
);
176 virtual ~unique_disposing_solar_mutex_reset_ptr() override
178 if (unique_disposing_ptr
<T
>::get() && comphelper::SolarMutex::get())
187 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */