1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_COMPHELPER_WEAKEVENTLISTENER_HXX
21 #define INCLUDED_COMPHELPER_WEAKEVENTLISTENER_HXX
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>
30 //.........................................................................
33 //.........................................................................
35 //=====================================================================
36 //= OWeakListenerAdapterBase
37 //=====================================================================
38 /** (the base for) an adapter which allows to add as listener to a foreign component, without
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 it's life time.</p>
45 class OWeakListenerAdapterBase
: public OBaseMutex
48 ::com::sun::star::uno::WeakReference
< ::com::sun::star::uno::XInterface
>
50 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>
54 inline ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>
57 return m_aListener
.get();
60 inline const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>&
61 getBroadcaster( ) const
63 return m_xBroadcaster
;
66 inline void resetListener( )
73 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
77 :m_aListener ( _rxListener
)
78 ,m_xBroadcaster ( _rxBroadcaster
)
83 virtual ~OWeakListenerAdapterBase();
87 //=====================================================================
88 //= OWeakListenerAdapter
89 //=====================================================================
90 template< class BROADCASTER
, class LISTENER
>
91 /** yet another base for weak listener adapters, this time with some type safety
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>
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>
99 class OWeakListenerAdapter
100 :public ::cppu::WeakComponentImplHelper1
< LISTENER
>
101 ,public OWeakListenerAdapterBase
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>
108 OWeakListenerAdapter(
109 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XWeak
>& _rxListener
,
110 const ::com::sun::star::uno::Reference
< BROADCASTER
>& _rxBroadcaster
114 inline ::com::sun::star::uno::Reference
< LISTENER
> getListener( ) const
116 return ::com::sun::star::uno::Reference
< LISTENER
>( OWeakListenerAdapterBase::getListener(), ::com::sun::star::uno::UNO_QUERY
);
119 // XEventListener overridables
120 virtual void SAL_CALL
disposing( const ::com::sun::star::lang::EventObject
& Source
) throw (::com::sun::star::uno::RuntimeException
);
123 // OComponentHelper overridables
124 // to be overridden, again - the derived class should revoke the listener from the broadcaster
125 virtual void SAL_CALL
disposing( ) = 0;
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
136 class COMPHELPER_DLLPUBLIC OWeakEventListenerAdapter
: public OWeakEventListenerAdapter_Base
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
144 // nothing to do except an own ctor - the forwarding of the "disposing" is already done
148 using OWeakEventListenerAdapter_Base::disposing
;
149 virtual void SAL_CALL
disposing( );
152 //=====================================================================
153 //= OWeakListenerAdapter
154 //=====================================================================
155 //---------------------------------------------------------------------
156 template< class BROADCASTER
, class LISTENER
>
157 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
161 : ::cppu::WeakComponentImplHelper1
< LISTENER
>( m_aMutex
)
162 , OWeakListenerAdapterBase( _rxListener
, _rxBroadcaster
)
166 //---------------------------------------------------------------------
167 template< class BROADCASTER
, class LISTENER
>
168 void SAL_CALL OWeakListenerAdapter
< BROADCASTER
, LISTENER
>::disposing( const ::com::sun::star::lang::EventObject
& _rSource
) throw (::com::sun::star::uno::RuntimeException
)
170 ::com::sun::star::uno::Reference
< LISTENER
> xListener( getListener() );
171 if ( xListener
.is() )
172 xListener
->disposing( _rSource
);
175 //.........................................................................
176 } // namespace comphelper
177 //.........................................................................
179 #endif // INCLUDED_COMPHELPER_WEAKEVENTLISTENER_HXX
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */