LibreOffice Module comphelper (master)  1
weakeventlistener.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #ifndef INCLUDED_COMPHELPER_WEAKEVENTLISTENER_HXX
21 #define INCLUDED_COMPHELPER_WEAKEVENTLISTENER_HXX
22 
23 #include <config_options.h>
24 #include <cppuhelper/compbase.hxx>
25 #include <cppuhelper/basemutex.hxx>
26 #include <cppuhelper/weakref.hxx>
28 #include <com/sun/star/lang/XEventListener.hpp>
29 #include <com/sun/star/lang/XComponent.hpp>
30 
31 namespace com::sun::star::uno { class XWeak; }
32 
33 namespace comphelper
34 {
35 
36 
37  //= OWeakListenerAdapterBase
38 
47  {
48  private:
49  css::uno::WeakReference< css::uno::XInterface >
51  css::uno::Reference< css::uno::XInterface >
53 
54  protected:
55  css::uno::Reference< css::uno::XInterface >
56  getListener( ) const
57  {
58  return m_aListener.get();
59  }
60 
61  const css::uno::Reference< css::uno::XInterface >&
62  getBroadcaster( ) const
63  {
64  return m_xBroadcaster;
65  }
66 
67  void resetListener( )
68  {
69  m_aListener.clear();
70  }
71 
72 
73  protected:
75  const css::uno::Reference< css::uno::XWeak >& _rxListener,
76  const css::uno::Reference< css::uno::XInterface >& _rxBroadcaster
77  )
78  :m_aListener ( _rxListener )
79  ,m_xBroadcaster ( _rxBroadcaster )
80  {
81  }
82 
83  protected:
84  virtual ~OWeakListenerAdapterBase();
85  };
86 
87 
88  //= OWeakListenerAdapter
89 
90  template< class BROADCASTER, class LISTENER >
100  :public ::cppu::WeakComponentImplHelper< LISTENER >
102  {
103  protected:
109  const css::uno::Reference< css::uno::XWeak >& _rxListener,
110  const css::uno::Reference< BROADCASTER >& _rxBroadcaster
111  );
112 
113  protected:
114  css::uno::Reference< LISTENER > getListener( ) const
115  {
116  return css::uno::Reference< LISTENER >( OWeakListenerAdapterBase::getListener(), css::uno::UNO_QUERY );
117  }
118 
119  // XEventListener overridables
120  virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
121 
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( ) override = 0;
126  };
127 
128 
129  //= OWeakEventListenerAdapter
130 
131  typedef OWeakListenerAdapter < css::lang::XComponent
132  , css::lang::XEventListener
136  class UNLESS_MERGELIBS(COMPHELPER_DLLPUBLIC) OWeakEventListenerAdapter final : public OWeakEventListenerAdapter_Base
137  {
138  public:
139  OWeakEventListenerAdapter(
140  css::uno::Reference< css::uno::XWeak > const & _rxListener,
141  css::uno::Reference< css::lang::XComponent > const & _rxBroadcaster
142  );
143 
144  // nothing to do except an own ctor - the forwarding of the "disposing" is already done
145  // in the base class
146 
147  private:
149  virtual void SAL_CALL disposing( ) override;
150  };
151 
152 
153  //= OWeakListenerAdapter
154 
155 
156  template< class BROADCASTER, class LISTENER >
158  const css::uno::Reference< css::uno::XWeak >& _rxListener,
159  const css::uno::Reference< BROADCASTER >& _rxBroadcaster
160  )
161  : ::cppu::WeakComponentImplHelper< LISTENER >( m_aMutex )
162  , OWeakListenerAdapterBase( _rxListener, _rxBroadcaster )
163  {
164  }
165 
166 
167  template< class BROADCASTER, class LISTENER >
168  void SAL_CALL OWeakListenerAdapter< BROADCASTER, LISTENER >::disposing( const css::lang::EventObject& _rSource )
169  {
170  css::uno::Reference< LISTENER > xListener( getListener() );
171  if ( xListener.is() )
172  xListener->disposing( _rSource );
173  }
174 
175 
176 } // namespace comphelper
177 
178 
179 #endif // INCLUDED_COMPHELPER_WEAKEVENTLISTENER_HXX
180 
181 
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const css::uno::Reference< css::uno::XInterface > & getBroadcaster() const
::osl::Mutex m_aMutex
(the base for) an adapter which allows to add as listener to a foreign component, without being held ...
css::uno::Reference< css::uno::XInterface > m_xBroadcaster
css::uno::Reference< css::uno::XInterface > getListener() const
css::uno::Reference< LISTENER > getListener() const
OWeakListenerAdapter< css::lang::XComponent, css::lang::XEventListener > OWeakEventListenerAdapter_Base
yet another base for weak listener adapters, this time with some type safety
OWeakListenerAdapter(const css::uno::Reference< css::uno::XWeak > &_rxListener, const css::uno::Reference< BROADCASTER > &_rxBroadcaster)
ctor
OWeakListenerAdapterBase(const css::uno::Reference< css::uno::XWeak > &_rxListener, const css::uno::Reference< css::uno::XInterface > &_rxBroadcaster)
css::uno::WeakReference< css::uno::XInterface > m_aListener
virtual void SAL_CALL disposing() override=0