Avoid potential negative array index access to cached text.
[LibreOffice.git] / include / comphelper / unique_disposing_ptr.hxx
blob5994799fc4bbee84d7296c8e5f5ab3ab8214ba18
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 <utility>
22 #include <vcl/svapp.hxx>
24 namespace comphelper
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
29 private:
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;
35 public:
36 unique_disposing_ptr( const css::uno::Reference< css::lang::XComponent > &rComponent, T * p = nullptr, bool bComponent = false)
37 : m_xItem(p)
39 m_xTerminateListener = new TerminateListener(rComponent, *this, bComponent);
42 virtual void reset(T * p = nullptr)
44 m_xItem.reset(p);
47 T & operator*() const
49 return *m_xItem;
52 T * get() const
54 return m_xItem.get();
57 T * operator->() const
59 return m_xItem.get();
62 operator bool () const
64 return static_cast< bool >(m_xItem);
67 virtual ~unique_disposing_ptr() COVERITY_NOEXCEPT_FALSE
69 reset();
71 private:
72 class TerminateListener final : public ::cppu::WeakImplHelper< css::frame::XTerminateListener,
73 css::lang::XServiceInfo>
75 private:
76 css::uno::Reference< css::lang::XComponent > m_xComponent;
77 unique_disposing_ptr<T>& m_rItem;
78 bool const mbComponentDLL;
79 public:
80 TerminateListener(css::uno::Reference< css::lang::XComponent > xComponent,
81 unique_disposing_ptr<T>& rItem, bool bComponentDLL) :
82 m_xComponent(std::move(xComponent)),
83 m_rItem(rItem),
84 mbComponentDLL(bComponentDLL)
86 if (m_xComponent.is())
88 css::uno::Reference< css::frame::XDesktop> xDesktop(m_xComponent, css::uno::UNO_QUERY);
89 if (xDesktop.is())
90 xDesktop->addTerminateListener(this);
91 else
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);
101 if (xDesktop.is())
102 xDesktop->removeTerminateListener(this);
103 else
104 m_xComponent->removeEventListener(this);
108 // XEventListener
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);
116 if (xDesktop.is())
117 xDesktop->removeTerminateListener(this);
118 else
119 m_xComponent->removeEventListener(this);
120 m_xComponent.clear();
123 if (shutDown)
124 m_rItem.reset();
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
134 disposing(rEvt);
137 virtual OUString SAL_CALL getImplementationName() override
139 if (mbComponentDLL)
140 return "com.sun.star.comp.ComponentDLLListener";
141 else
142 return "com.sun.star.comp.DisposingTerminateListener";
145 virtual sal_Bool SAL_CALL supportsService(const OUString& /*rName*/) override
147 return false;
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>
164 public:
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())
179 reset();
185 #endif
187 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */