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