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 <cppuhelper/compbase.hxx>
24 #include <cppuhelper/basemutex.hxx>
25 #include <cppuhelper/weakref.hxx>
27 #include <com/sun/star/lang/XEventListener.hpp>
28 #include <com/sun/star/lang/XComponent.hpp>
29 
30 namespace com { namespace sun { namespace star { namespace uno { class XWeak; } } } }
31 
32 namespace comphelper
33 {
34 
35 
36  //= OWeakListenerAdapterBase
37 
46  {
47  private:
48  css::uno::WeakReference< css::uno::XInterface >
50  css::uno::Reference< css::uno::XInterface >
52 
53  protected:
54  css::uno::Reference< css::uno::XInterface >
55  getListener( ) const
56  {
57  return m_aListener.get();
58  }
59 
60  const css::uno::Reference< css::uno::XInterface >&
61  getBroadcaster( ) const
62  {
63  return m_xBroadcaster;
64  }
65 
66  void resetListener( )
67  {
68  m_aListener.clear();
69  }
70 
71 
72  protected:
74  const css::uno::Reference< css::uno::XWeak >& _rxListener,
75  const css::uno::Reference< css::uno::XInterface >& _rxBroadcaster
76  )
77  :m_aListener ( _rxListener )
78  ,m_xBroadcaster ( _rxBroadcaster )
79  {
80  }
81 
82  protected:
83  virtual ~OWeakListenerAdapterBase();
84  };
85 
86 
87  //= OWeakListenerAdapter
88 
89  template< class BROADCASTER, class LISTENER >
99  :public ::cppu::WeakComponentImplHelper< LISTENER >
101  {
102  protected:
108  const css::uno::Reference< css::uno::XWeak >& _rxListener,
109  const css::uno::Reference< BROADCASTER >& _rxBroadcaster
110  );
111 
112  protected:
113  css::uno::Reference< LISTENER > getListener( ) const
114  {
115  return css::uno::Reference< LISTENER >( OWeakListenerAdapterBase::getListener(), css::uno::UNO_QUERY );
116  }
117 
118  // XEventListener overridables
119  virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
120 
121  protected:
122  // OComponentHelper overridables
123  // to be overridden, again - the derived class should revoke the listener from the broadcaster
124  virtual void SAL_CALL disposing( ) override = 0;
125  };
126 
127 
128  //= OWeakEventListenerAdapter
129 
130  typedef OWeakListenerAdapter < css::lang::XComponent
131  , css::lang::XEventListener
136  {
137  public:
139  css::uno::Reference< css::uno::XWeak > const & _rxListener,
140  css::uno::Reference< css::lang::XComponent > const & _rxBroadcaster
141  );
142 
143  // nothing to do except an own ctor - the forwarding of the "disposing" is already done
144  // in the base class
145 
146  private:
148  virtual void SAL_CALL disposing( ) override;
149  };
150 
151 
152  //= OWeakListenerAdapter
153 
154 
155  template< class BROADCASTER, class LISTENER >
157  const css::uno::Reference< css::uno::XWeak >& _rxListener,
158  const css::uno::Reference< BROADCASTER >& _rxBroadcaster
159  )
160  : ::cppu::WeakComponentImplHelper< LISTENER >( m_aMutex )
161  , OWeakListenerAdapterBase( _rxListener, _rxBroadcaster )
162  {
163  }
164 
165 
166  template< class BROADCASTER, class LISTENER >
167  void SAL_CALL OWeakListenerAdapter< BROADCASTER, LISTENER >::disposing( const css::lang::EventObject& _rSource )
168  {
169  css::uno::Reference< LISTENER > xListener( getListener() );
170  if ( xListener.is() )
171  xListener->disposing( _rSource );
172  }
173 
174 
175 } // namespace comphelper
176 
177 
178 #endif // INCLUDED_COMPHELPER_WEAKEVENTLISTENER_HXX
179 
180 
181 /* 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
#define COMPHELPER_DLLPUBLIC
the most simple listener adapter: for XEventListeners at XComponents