bump product version to 7.6.3.2-android
[LibreOffice.git] / include / comphelper / weakeventlistener.hxx
blobc4b4e79704c4d612650b20d43d69595b863da70e
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 <config_options.h>
24 #include <comphelper/compbase.hxx>
25 #include <cppuhelper/basemutex.hxx>
26 #include <cppuhelper/weakref.hxx>
27 #include <comphelper/comphelperdllapi.h>
28 #include <com/sun/star/lang/XEventListener.hpp>
29 #include <com/sun/star/lang/XComponent.hpp>
30 #include <utility>
32 namespace com::sun::star::uno { class XWeak; }
34 namespace comphelper
38 //= OWeakListenerAdapterBase
40 /** (the base for) an adapter which allows to add as listener to a foreign component, without
41 being held hard.
43 <p>The idea is that this adapter is added as listener to a foreign component, which usually
44 holds it's listener hard. The adapter itself knows the real listener as weak reference,
45 thus not affecting its life time.</p>
47 class OWeakListenerAdapterBase
49 private:
50 css::uno::WeakReference< css::uno::XInterface >
51 m_aListener;
52 css::uno::Reference< css::uno::XInterface >
53 m_xBroadcaster;
55 protected:
56 css::uno::Reference< css::uno::XInterface >
57 getListener( ) const
59 return m_aListener.get();
62 const css::uno::Reference< css::uno::XInterface >&
63 getBroadcaster( ) const
65 return m_xBroadcaster;
68 void resetListener( )
70 m_aListener.clear();
74 protected:
75 OWeakListenerAdapterBase(
76 const css::uno::Reference< css::uno::XWeak >& _rxListener,
77 css::uno::Reference< css::uno::XInterface > _xBroadcaster
79 :m_aListener ( _rxListener )
80 ,m_xBroadcaster (std::move( _xBroadcaster ))
84 protected:
85 virtual ~OWeakListenerAdapterBase();
89 //= OWeakListenerAdapter
91 template< class BROADCASTER, class LISTENER >
92 /** yet another base for weak listener adapters, this time with some type safety
94 <p>Note that derived classes need to overwrite all virtual methods of their interface
95 except XEventListener::disposing, and forward it to their master listener.</p>
97 <p>Additionally, derived classes need to add themself as listener to the broadcaster,
98 as this can't be done in a generic way</p>
100 class OWeakListenerAdapter
101 :public ::comphelper::WeakComponentImplHelper< LISTENER >
102 ,public OWeakListenerAdapterBase
104 protected:
105 /** ctor
106 <p>Note that derived classes still need to add themself as listener to the broadcaster,
107 as this can't be done in a generic way</p>
109 OWeakListenerAdapter(
110 const css::uno::Reference< css::uno::XWeak >& _rxListener,
111 const css::uno::Reference< BROADCASTER >& _rxBroadcaster
114 protected:
115 css::uno::Reference< LISTENER > getListener( ) const
117 return css::uno::Reference< LISTENER >( OWeakListenerAdapterBase::getListener(), css::uno::UNO_QUERY );
120 // XEventListener overridables
121 virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
123 protected:
124 // OComponentHelper overridables
125 // to be overridden, again - the derived class should revoke the listener from the broadcaster
126 virtual void disposing( std::unique_lock<std::mutex>& rGuard ) override = 0;
130 //= OWeakEventListenerAdapter
132 typedef OWeakListenerAdapter < css::lang::XComponent
133 , css::lang::XEventListener
134 > OWeakEventListenerAdapter_Base;
135 /** the most simple listener adapter: for XEventListeners at XComponents
137 class UNLESS_MERGELIBS(COMPHELPER_DLLPUBLIC) OWeakEventListenerAdapter final : public OWeakEventListenerAdapter_Base
139 public:
140 OWeakEventListenerAdapter(
141 css::uno::Reference< css::uno::XWeak > const & _rxListener,
142 css::uno::Reference< css::lang::XComponent > const & _rxBroadcaster
145 // nothing to do except an own ctor - the forwarding of the "disposing" is already done
146 // in the base class
148 private:
149 using OWeakEventListenerAdapter_Base::disposing;
150 virtual void disposing( std::unique_lock<std::mutex>& rGuard ) override;
154 //= OWeakListenerAdapter
157 template< class BROADCASTER, class LISTENER >
158 OWeakListenerAdapter< BROADCASTER, LISTENER >::OWeakListenerAdapter(
159 const css::uno::Reference< css::uno::XWeak >& _rxListener,
160 const css::uno::Reference< BROADCASTER >& _rxBroadcaster
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: */