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>
21 #include <vcl/svapp.hxx>
25 //Similar to std::unique_ptr, except additionally releases the ptr on XComponent::disposing and/or XTerminateListener::notifyTermination if supported
26 template<class T
> class unique_disposing_ptr
29 std::unique_ptr
<T
, o3tl::default_delete
<T
>> m_xItem
;
30 css::uno::Reference
< css::frame::XTerminateListener
> m_xTerminateListener
;
32 unique_disposing_ptr(const unique_disposing_ptr
&) = delete;
33 unique_disposing_ptr
& operator=(const unique_disposing_ptr
&) = delete;
35 unique_disposing_ptr( const css::uno::Reference
< css::lang::XComponent
> &rComponent
, T
* p
= nullptr, bool bComponent
= false)
38 m_xTerminateListener
= new TerminateListener(rComponent
, *this, bComponent
);
41 virtual void reset(T
* p
= nullptr)
56 T
* operator->() const
61 operator bool () const
63 return static_cast< bool >(m_xItem
);
66 virtual ~unique_disposing_ptr() COVERITY_NOEXCEPT_FALSE
71 class TerminateListener final
: public ::cppu::WeakImplHelper
< css::frame::XTerminateListener
,
72 css::lang::XServiceInfo
>
75 css::uno::Reference
< css::lang::XComponent
> m_xComponent
;
76 unique_disposing_ptr
<T
>& m_rItem
;
77 bool const mbComponentDLL
;
79 TerminateListener(const css::uno::Reference
< css::lang::XComponent
> &rComponent
,
80 unique_disposing_ptr
<T
>& rItem
, bool bComponentDLL
) :
81 m_xComponent(rComponent
),
83 mbComponentDLL(bComponentDLL
)
85 if (m_xComponent
.is())
87 css::uno::Reference
< css::frame::XDesktop
> xDesktop(m_xComponent
, css::uno::UNO_QUERY
);
89 xDesktop
->addTerminateListener(this);
91 m_xComponent
->addEventListener(this);
95 virtual ~TerminateListener() override
97 if ( m_xComponent
.is() )
99 css::uno::Reference
< css::frame::XDesktop
> xDesktop(m_xComponent
, css::uno::UNO_QUERY
);
101 xDesktop
->removeTerminateListener(this);
103 m_xComponent
->removeEventListener(this);
108 virtual void SAL_CALL
disposing( const css::lang::EventObject
& rEvt
) override
110 bool shutDown
= (rEvt
.Source
== m_xComponent
);
112 if (shutDown
&& m_xComponent
.is())
114 css::uno::Reference
< css::frame::XDesktop
> xDesktop(m_xComponent
, css::uno::UNO_QUERY
);
116 xDesktop
->removeTerminateListener(this);
118 m_xComponent
->removeEventListener(this);
119 m_xComponent
.clear();
126 // XTerminateListener
127 virtual void SAL_CALL
queryTermination( const css::lang::EventObject
& ) override
131 virtual void SAL_CALL
notifyTermination( const css::lang::EventObject
& rEvt
) override
136 virtual OUString SAL_CALL
getImplementationName() override
139 return "com.sun.star.comp.ComponentDLLListener";
141 return "com.sun.star.comp.DisposingTerminateListener";
144 virtual sal_Bool SAL_CALL
supportsService(const OUString
& /*rName*/) override
149 virtual css::uno::Sequence
<OUString
> SAL_CALL
getSupportedServiceNames() override
151 return css::uno::Sequence
<OUString
>();
156 //Something like an OutputDevice requires the SolarMutex to be taken before use
157 //for threadsafety. The user can ensure this, except in the case of its dtor
158 //being called from reset due to a terminate on the XComponent being called
159 //from an arbitrary thread
160 template<class T
> class unique_disposing_solar_mutex_reset_ptr
161 : public unique_disposing_ptr
<T
>
164 unique_disposing_solar_mutex_reset_ptr( const css::uno::Reference
< css::lang::XComponent
> &rComponent
, T
* p
= nullptr, bool bComponent
= false)
165 : unique_disposing_ptr
<T
>(rComponent
, p
, bComponent
)
169 virtual void reset(T
* p
= nullptr) override
171 SolarMutexGuard aGuard
;
172 unique_disposing_ptr
<T
>::reset(p
);
175 virtual ~unique_disposing_solar_mutex_reset_ptr() override
177 if (unique_disposing_ptr
<T
>::get() && comphelper::SolarMutex::get())
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */