bump product version to 4.2.0.1
[LibreOffice.git] / include / comphelper / weakeventlistener.hxx
blob734f91511d53de843a56db7898360dfdf4b68d49
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/.
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 //.........................................................................
31 namespace comphelper
33 //.........................................................................
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.
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
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;
53 protected:
54 inline ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
55 getListener( ) const
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( )
68 m_aListener.clear();
72 protected:
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 )
82 protected:
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
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>
108 OWeakListenerAdapter(
109 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XWeak >& _rxListener,
110 const ::com::sun::star::uno::Reference< BROADCASTER >& _rxBroadcaster
113 protected:
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);
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( ) = 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
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
144 // nothing to do except an own ctor - the forwarding of the "disposing" is already done
145 // in the base class
147 protected:
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: */