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/compbase.hxx>
24 #include <cppuhelper/basemutex.hxx>
25 #include <cppuhelper/weakref.hxx>
26 #include <comphelper/comphelperdllapi.h>
29 namespace com
{ namespace sun
{ namespace star
{ namespace lang
{ class XComponent
; } } } }
30 namespace com
{ namespace sun
{ namespace star
{ namespace lang
{ class XEventListener
; } } } }
31 namespace com
{ namespace sun
{ namespace star
{ namespace uno
{ class XWeak
; } } } }
37 //= OWeakListenerAdapterBase
39 /** (the base for) an adapter which allows to add as listener to a foreign component, without
42 <p>The idea is that this adapter is added as listener to a foreign component, which usually
43 holds it's listener hard. The adapter itself knows the real listener as weak reference,
44 thus not affecting its life time.</p>
46 class OWeakListenerAdapterBase
: public cppu::BaseMutex
49 css::uno::WeakReference
< css::uno::XInterface
>
51 css::uno::Reference
< css::uno::XInterface
>
55 css::uno::Reference
< css::uno::XInterface
>
58 return m_aListener
.get();
61 const css::uno::Reference
< css::uno::XInterface
>&
62 getBroadcaster( ) const
64 return m_xBroadcaster
;
74 OWeakListenerAdapterBase(
75 const css::uno::Reference
< css::uno::XWeak
>& _rxListener
,
76 const css::uno::Reference
< css::uno::XInterface
>& _rxBroadcaster
78 :m_aListener ( _rxListener
)
79 ,m_xBroadcaster ( _rxBroadcaster
)
84 virtual ~OWeakListenerAdapterBase();
88 //= OWeakListenerAdapter
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>Additionally, 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::WeakComponentImplHelper
< 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 css::uno::Reference
< css::uno::XWeak
>& _rxListener
,
110 const css::uno::Reference
< BROADCASTER
>& _rxBroadcaster
114 css::uno::Reference
< LISTENER
> getListener( ) const
116 return css::uno::Reference
< LISTENER
>( OWeakListenerAdapterBase::getListener(), css::uno::UNO_QUERY
);
119 // XEventListener overridables
120 virtual void SAL_CALL
disposing( const css::lang::EventObject
& Source
) override
;
123 // OComponentHelper overridables
124 // to be overridden, again - the derived class should revoke the listener from the broadcaster
125 virtual void SAL_CALL
disposing( ) override
= 0;
129 //= OWeakEventListenerAdapter
131 typedef OWeakListenerAdapter
< css::lang::XComponent
132 , css::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 css::uno::Reference
< css::uno::XWeak
> const & _rxListener
,
141 css::uno::Reference
< css::lang::XComponent
> const & _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( ) override
;
153 //= OWeakListenerAdapter
156 template< class BROADCASTER
, class LISTENER
>
157 OWeakListenerAdapter
< BROADCASTER
, LISTENER
>::OWeakListenerAdapter(
158 const css::uno::Reference
< css::uno::XWeak
>& _rxListener
,
159 const css::uno::Reference
< BROADCASTER
>& _rxBroadcaster
161 : ::cppu::WeakComponentImplHelper
< LISTENER
>( m_aMutex
)
162 , OWeakListenerAdapterBase( _rxListener
, _rxBroadcaster
)
167 template< class BROADCASTER
, class LISTENER
>
168 void SAL_CALL OWeakListenerAdapter
< BROADCASTER
, LISTENER
>::disposing( const css::lang::EventObject
& _rSource
)
170 css::uno::Reference
< LISTENER
> xListener( getListener() );
171 if ( xListener
.is() )
172 xListener
->disposing( _rSource
);
176 } // namespace comphelper
179 #endif // INCLUDED_COMPHELPER_WEAKEVENTLISTENER_HXX
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */