nss: upgrade to release 3.73
[LibreOffice.git] / include / comphelper / unique_disposing_ptr.hxx
blob7bed79c431103470b639ba61652787efef651f59
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #ifndef INCLUDED_COMPHELPER_UNIQUE_DISPOSING_PTR_HXX
11 #define INCLUDED_COMPHELPER_UNIQUE_DISPOSING_PTR_HXX
13 #include <memory>
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>
23 namespace comphelper
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
28 private:
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;
34 public:
35 unique_disposing_ptr( const css::uno::Reference< css::lang::XComponent > &rComponent, T * p = nullptr, bool bComponent = false)
36 : m_xItem(p)
38 m_xTerminateListener = new TerminateListener(rComponent, *this, bComponent);
41 virtual void reset(T * p = nullptr)
43 m_xItem.reset(p);
46 T & operator*() const
48 return *m_xItem;
51 T * get() const
53 return m_xItem.get();
56 T * operator->() const
58 return m_xItem.get();
61 operator bool () const
63 return static_cast< bool >(m_xItem);
66 virtual ~unique_disposing_ptr() COVERITY_NOEXCEPT_FALSE
68 reset();
70 private:
71 class TerminateListener final : public ::cppu::WeakImplHelper< css::frame::XTerminateListener,
72 css::lang::XServiceInfo>
74 private:
75 css::uno::Reference< css::lang::XComponent > m_xComponent;
76 unique_disposing_ptr<T>& m_rItem;
77 bool const mbComponentDLL;
78 public:
79 TerminateListener(const css::uno::Reference< css::lang::XComponent > &rComponent,
80 unique_disposing_ptr<T>& rItem, bool bComponentDLL) :
81 m_xComponent(rComponent),
82 m_rItem(rItem),
83 mbComponentDLL(bComponentDLL)
85 if (m_xComponent.is())
87 css::uno::Reference< css::frame::XDesktop> xDesktop(m_xComponent, css::uno::UNO_QUERY);
88 if (xDesktop.is())
89 xDesktop->addTerminateListener(this);
90 else
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);
100 if (xDesktop.is())
101 xDesktop->removeTerminateListener(this);
102 else
103 m_xComponent->removeEventListener(this);
107 // XEventListener
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);
115 if (xDesktop.is())
116 xDesktop->removeTerminateListener(this);
117 else
118 m_xComponent->removeEventListener(this);
119 m_xComponent.clear();
122 if (shutDown)
123 m_rItem.reset();
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
133 disposing(rEvt);
136 virtual OUString SAL_CALL getImplementationName() override
138 if (mbComponentDLL)
139 return "com.sun.star.comp.ComponentDLLListener";
140 else
141 return "com.sun.star.comp.DisposingTerminateListener";
144 virtual sal_Bool SAL_CALL supportsService(const OUString& /*rName*/) override
146 return false;
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>
163 public:
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())
178 reset();
184 #endif
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */