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 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef INCLUDED_COMPHELPER_WEAKEVENTLISTENER_HXX
21 : #define INCLUDED_COMPHELPER_WEAKEVENTLISTENER_HXX
22 :
23 : #include <cppuhelper/compbase1.hxx>
24 : #include <com/sun/star/lang/XComponent.hpp>
25 : #include <com/sun/star/uno/XWeak.hpp>
26 : #include <cppuhelper/weakref.hxx>
27 : #include <comphelper/broadcasthelper.hxx>
28 : #include <comphelper/comphelperdllapi.h>
29 :
30 :
31 : namespace comphelper
32 : {
33 :
34 :
35 :
36 : //= OWeakListenerAdapterBase
37 :
38 : /** (the base for) an adapter which allows to add as listener to a foreign component, without
39 : being held hard.
40 :
41 : <p>The idea is that this adapter is added as listener to a foreign component, which usually
42 : holds it's listener hard. The adapter itself knows the real listener as weak reference,
43 : thus not affecting its life time.</p>
44 : */
45 : class OWeakListenerAdapterBase : public OBaseMutex
46 : {
47 : private:
48 : ::com::sun::star::uno::WeakReference< ::com::sun::star::uno::XInterface >
49 : m_aListener;
50 : ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
51 : m_xBroadcaster;
52 :
53 : protected:
54 : inline ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
55 0 : getListener( ) const
56 : {
57 0 : return m_aListener.get();
58 : }
59 :
60 : inline const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >&
61 0 : getBroadcaster( ) const
62 : {
63 0 : return m_xBroadcaster;
64 : }
65 :
66 0 : inline void resetListener( )
67 : {
68 0 : m_aListener.clear();
69 0 : }
70 :
71 :
72 : protected:
73 0 : inline OWeakListenerAdapterBase(
74 : const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XWeak >& _rxListener,
75 : const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxBroadcaster
76 : )
77 : :m_aListener ( _rxListener )
78 0 : ,m_xBroadcaster ( _rxBroadcaster )
79 : {
80 0 : }
81 :
82 : protected:
83 : virtual ~OWeakListenerAdapterBase();
84 : };
85 :
86 :
87 :
88 : //= OWeakListenerAdapter
89 :
90 : template< class BROADCASTER, class LISTENER >
91 : /** yet another base for weak listener adapters, this time with some type safety
92 :
93 : <p>Note that derived classes need to overwrite all virtual methods of their interface
94 : except XEventListener::disposing, and forward it to their master listener.</p>
95 :
96 : <p>Addtionally, derived classes need to add themself as listener to the broadcaster,
97 : as this can't be done in a generic way</p>
98 : */
99 0 : class OWeakListenerAdapter
100 : :public ::cppu::WeakComponentImplHelper1 < LISTENER >
101 : ,public OWeakListenerAdapterBase
102 : {
103 : protected:
104 : /** ctor
105 : <p>Note that derived classes still need to add themself as listener to the broadcaster,
106 : as this can't be done in a generic way</p>
107 : */
108 : OWeakListenerAdapter(
109 : const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XWeak >& _rxListener,
110 : const ::com::sun::star::uno::Reference< BROADCASTER >& _rxBroadcaster
111 : );
112 :
113 : protected:
114 0 : inline ::com::sun::star::uno::Reference< LISTENER > getListener( ) const
115 : {
116 0 : return ::com::sun::star::uno::Reference< LISTENER >( OWeakListenerAdapterBase::getListener(), ::com::sun::star::uno::UNO_QUERY );
117 : }
118 :
119 : // XEventListener overridables
120 : virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
121 :
122 : protected:
123 : // OComponentHelper overridables
124 : // to be overridden, again - the derived class should revoke the listener from the broadcaster
125 : virtual void SAL_CALL disposing( ) SAL_OVERRIDE = 0;
126 : };
127 :
128 :
129 : //= OWeakEventListenerAdapter
130 :
131 : typedef OWeakListenerAdapter < ::com::sun::star::lang::XComponent
132 : , ::com::sun::star::lang::XEventListener
133 : > OWeakEventListenerAdapter_Base;
134 : /** the most simple listener adapter: for XEventListeners at XComponents
135 : */
136 0 : class COMPHELPER_DLLPUBLIC OWeakEventListenerAdapter : public OWeakEventListenerAdapter_Base
137 : {
138 : public:
139 : OWeakEventListenerAdapter(
140 : ::com::sun::star::uno::Reference< ::com::sun::star::uno::XWeak > _rxListener,
141 : ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > _rxBroadcaster
142 : );
143 :
144 : // nothing to do except an own ctor - the forwarding of the "disposing" is already done
145 : // in the base class
146 :
147 : protected:
148 : using OWeakEventListenerAdapter_Base::disposing;
149 : virtual void SAL_CALL disposing( ) SAL_OVERRIDE;
150 : };
151 :
152 :
153 : //= OWeakListenerAdapter
154 :
155 :
156 : template< class BROADCASTER, class LISTENER >
157 0 : OWeakListenerAdapter< BROADCASTER, LISTENER >::OWeakListenerAdapter(
158 : const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XWeak >& _rxListener,
159 : const ::com::sun::star::uno::Reference< BROADCASTER >& _rxBroadcaster
160 : )
161 : : ::cppu::WeakComponentImplHelper1< LISTENER >( m_aMutex )
162 0 : , OWeakListenerAdapterBase( _rxListener, _rxBroadcaster )
163 : {
164 0 : }
165 :
166 :
167 : template< class BROADCASTER, class LISTENER >
168 0 : void SAL_CALL OWeakListenerAdapter< BROADCASTER, LISTENER >::disposing( const ::com::sun::star::lang::EventObject& _rSource ) throw (::com::sun::star::uno::RuntimeException)
169 : {
170 0 : ::com::sun::star::uno::Reference< LISTENER > xListener( getListener() );
171 0 : if ( xListener.is() )
172 0 : xListener->disposing( _rSource );
173 0 : }
174 :
175 :
176 : } // namespace comphelper
177 :
178 :
179 : #endif // INCLUDED_COMPHELPER_WEAKEVENTLISTENER_HXX
180 :
181 :
182 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|