Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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 : */
9 :
10 : #ifndef INCLUDED_COMPHELPER_UNIQUE_DISPOSING_PTR_HXX
11 : #define INCLUDED_COMPHELPER_UNIQUE_DISPOSING_PTR_HXX
12 :
13 : #include <cppuhelper/implbase1.hxx>
14 :
15 : #include <com/sun/star/lang/XComponent.hpp>
16 : #include <com/sun/star/frame/XDesktop.hpp>
17 :
18 : #include <vcl/svapp.hxx>
19 : #include <osl/mutex.hxx>
20 :
21 : namespace comphelper
22 : {
23 : //Similar to std::unique_ptr, except additionally releases the ptr on XComponent::disposing and/or XTerminateListener::notifyTermination if supported
24 : template<class T> class unique_disposing_ptr
25 : {
26 : private:
27 : std::unique_ptr<T> m_xItem;
28 : ::com::sun::star::uno::Reference< ::com::sun::star::frame::XTerminateListener> m_xTerminateListener;
29 :
30 : unique_disposing_ptr(const unique_disposing_ptr&) SAL_DELETED_FUNCTION;
31 : unique_disposing_ptr& operator=(const unique_disposing_ptr&) SAL_DELETED_FUNCTION;
32 : public:
33 110 : unique_disposing_ptr( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > &rComponent, T * p = 0 )
34 110 : : m_xItem(p)
35 : {
36 110 : m_xTerminateListener = new TerminateListener(rComponent, *this);
37 110 : }
38 :
39 272 : virtual void reset(T * p = 0)
40 : {
41 272 : m_xItem.reset(p);
42 272 : }
43 :
44 : T & operator*() const
45 : {
46 : return *m_xItem;
47 : }
48 :
49 43 : T * get() const
50 : {
51 43 : return m_xItem.get();
52 : }
53 :
54 56734 : T * operator->() const
55 : {
56 56734 : return m_xItem.get();
57 : }
58 :
59 28367 : operator bool () const
60 : {
61 28367 : return static_cast< bool >(m_xItem);
62 : }
63 :
64 110 : virtual ~unique_disposing_ptr()
65 : {
66 110 : reset();
67 220 : }
68 : private:
69 : class TerminateListener : public ::cppu::WeakImplHelper1< ::com::sun::star::frame::XTerminateListener >
70 : {
71 : private:
72 : ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > m_xComponent;
73 : unique_disposing_ptr<T>& m_rItem;
74 : public:
75 110 : TerminateListener(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > &rComponent,
76 110 : unique_disposing_ptr<T>& rItem) : m_xComponent(rComponent), m_rItem(rItem)
77 : {
78 110 : if (m_xComponent.is())
79 : {
80 110 : ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDesktop> xDesktop(m_xComponent, ::com::sun::star::uno::UNO_QUERY);
81 110 : if (xDesktop.is())
82 59 : xDesktop->addTerminateListener(this);
83 : else
84 51 : m_xComponent->addEventListener(this);
85 : }
86 110 : }
87 :
88 218 : virtual ~TerminateListener()
89 : {
90 109 : if ( m_xComponent.is() )
91 : {
92 0 : ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDesktop> xDesktop(m_xComponent, ::com::sun::star::uno::UNO_QUERY);
93 0 : if (xDesktop.is())
94 0 : xDesktop->removeTerminateListener(this);
95 : else
96 0 : m_xComponent->removeEventListener(this);
97 : }
98 327 : }
99 :
100 : private:
101 : // XEventListener
102 109 : virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& rEvt )
103 : throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
104 : {
105 109 : bool shutDown = (rEvt.Source == m_xComponent);
106 :
107 109 : if (shutDown && m_xComponent.is())
108 : {
109 109 : ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDesktop> xDesktop(m_xComponent, ::com::sun::star::uno::UNO_QUERY);
110 109 : if (xDesktop.is())
111 58 : xDesktop->removeTerminateListener(this);
112 : else
113 51 : m_xComponent->removeEventListener(this);
114 109 : m_xComponent.clear();
115 : }
116 :
117 109 : if (shutDown)
118 109 : m_rItem.reset();
119 109 : }
120 :
121 : // XTerminateListener
122 26 : virtual void SAL_CALL queryTermination( const ::com::sun::star::lang::EventObject& )
123 : throw(::com::sun::star::frame::TerminationVetoException,
124 : ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
125 : {
126 26 : }
127 :
128 26 : virtual void SAL_CALL notifyTermination( const ::com::sun::star::lang::EventObject& rEvt )
129 : throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
130 : {
131 26 : disposing(rEvt);
132 26 : }
133 : };
134 : };
135 :
136 : //Something like an OutputDevice requires the SolarMutex to be taken before use
137 : //for threadsafety. The user can ensure this, except in the case of its dtor
138 : //being called from reset due to a terminate on the XComponent being called
139 : //from an aribitrary thread
140 59 : template<class T> class unique_disposing_solar_mutex_reset_ptr
141 : : public unique_disposing_ptr<T>
142 : {
143 : public:
144 59 : unique_disposing_solar_mutex_reset_ptr( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > &rComponent, T * p = 0 )
145 59 : : unique_disposing_ptr<T>(rComponent, p)
146 : {
147 59 : }
148 :
149 58 : virtual void reset(T * p = 0)
150 : {
151 58 : SolarMutexGuard aGuard;
152 58 : unique_disposing_ptr<T>::reset(p);
153 58 : }
154 : };
155 :
156 : }
157 :
158 : #endif
159 :
160 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|