LibreOffice Module comphelper (master)  1
listenernotification.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_LISTENERNOTIFICATION_HXX
21 #define INCLUDED_COMPHELPER_LISTENERNOTIFICATION_HXX
22 
24 
25 #include <com/sun/star/lang/EventObject.hpp>
27 
28 #include <memory>
29 
30 namespace com::sun::star::lang { class XEventListener; }
31 
32 namespace comphelper
33 {
34 
35 
36  //= OListenerContainer
37 
58  {
59  private:
61 
62  public:
68  void disposing( const css::lang::EventObject& _rEventSource );
69 
73  void clear();
74 
77  inline bool
78  empty() const;
79 
82  ::std::unique_ptr< ::comphelper::OInterfaceIteratorHelper2 > createIterator()
83  {
84  ::std::unique_ptr< ::comphelper::OInterfaceIteratorHelper2 > pIterator( new ::comphelper::OInterfaceIteratorHelper2( m_aListeners ) );
85  return pIterator;
86  }
87 
88  protected:
89  OListenerContainer( ::osl::Mutex& _rMutex );
90 
91  virtual ~OListenerContainer();
92 
93  void impl_addListener( const css::uno::Reference< css::lang::XEventListener >& _rxListener );
94  void impl_removeListener( const css::uno::Reference< css::lang::XEventListener >& _rxListener );
95 
106  bool impl_notify( const css::lang::EventObject& _rEvent );
107 
108  protected:
131  virtual bool implNotify(
132  const css::uno::Reference< css::lang::XEventListener >& _rxListener,
133  const css::lang::EventObject& _rEvent
134  ) = 0;
135  };
136 
137 
138  inline bool OListenerContainer::empty() const
139  {
140  return ( m_aListeners.getLength() == 0 );
141  }
142 
143 
144  //= OSimpleListenerContainer
145 
155  template< class LISTENER, class EVENT >
157  {
158  public:
159  typedef LISTENER ListenerClass;
160  typedef EVENT EventClass;
161  typedef void ( SAL_CALL LISTENER::*NotificationMethod )( const EventClass& );
162 
163  private:
164  NotificationMethod m_pNotificationMethod;
165 
166  public:
167  OSimpleListenerContainer( ::osl::Mutex& _rMutex )
168  :OListenerContainer( _rMutex )
169  ,m_pNotificationMethod( nullptr )
170  {
171  }
172 
173  void addListener( const css::uno::Reference< ListenerClass >& _rxListener )
174  {
175  OListenerContainer::impl_addListener( _rxListener.get() );
176  }
177 
178  void removeListener( const css::uno::Reference< ListenerClass >& _rxListener )
179  {
180  OListenerContainer::impl_removeListener( _rxListener.get() );
181  }
182 
183  // publish some otherwise hidden base functionality
188 
190  inline void notify( const EventClass& _rEvent, NotificationMethod _pNotify );
191 
192  private:
193  virtual bool implNotify(
194  const css::uno::Reference< css::lang::XEventListener >& _rxListener,
195  const css::lang::EventObject& _rEvent
196  ) override
197  {
198  const EventClass& rTypedEvent( static_cast< const EventClass& >( _rEvent ) );
199  ListenerClass* pTypedListener( static_cast< ListenerClass* >( _rxListener.get() ) );
200  (pTypedListener->*m_pNotificationMethod)( rTypedEvent );
201  return true;
202  }
203  };
204 
205 
206  template< class LISTENER, class EVENT >
207  inline void OSimpleListenerContainer< LISTENER, EVENT >::notify( const EventClass& _rEvent, NotificationMethod _pNotify )
208  {
209  m_pNotificationMethod = _pNotify;
211  m_pNotificationMethod = nullptr;
212  }
213 
214  //= OListenerContainerBase
215 
219  template< class ListenerClass, class EventClass >
221  {
222  public:
223  OListenerContainerBase( ::osl::Mutex& _rMutex ) : OListenerContainer( _rMutex )
224  {
225  }
226 
227  void addTypedListener( const css::uno::Reference< ListenerClass >& _rxListener )
228  {
229  OListenerContainer::impl_addListener( _rxListener.get() );
230  }
231 
232  void removeTypedListener( const css::uno::Reference< ListenerClass >& _rxListener )
233  {
234  OListenerContainer::impl_removeListener( _rxListener.get() );
235  }
236 
237  bool notify( const EventClass& _rEvent )
238  {
239  return OListenerContainer::impl_notify( _rEvent );
240  }
241 
243 
244  protected:
245  virtual bool implNotify(
246  const css::uno::Reference< css::lang::XEventListener >& _rxListener,
247  const css::lang::EventObject& _rEvent
248  ) override
249  {
250  return implTypedNotify(
251  css::uno::Reference< ListenerClass >( static_cast< ListenerClass* >( _rxListener.get() ) ),
252  static_cast< const EventClass& >( _rEvent )
253  );
254  }
255 
256  virtual bool implTypedNotify(
257  const css::uno::Reference< ListenerClass >& _rxListener,
258  const EventClass& _rEvent
259  ) = 0;
260  };
261 
262 } // namespace comphelper
263 
264 
265 #endif // INCLUDED_COMPHELPER_LISTENERNOTIFICATION_HXX
266 
267 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool empty() const
determines whether the listener container is currently empty
bool notify(const EventClass &_rEvent)
is a specialization of OListenerContainer which saves you some additional type casts, by making the required listener and event types template arguments.
void removeTypedListener(const css::uno::Reference< ListenerClass > &_rxListener)
typedef void(SAL_CALL LISTENER::*NotificationMethod)(const EventClass &)
bool impl_notify(const css::lang::EventObject &_rEvent)
notifies all listeners of the given event, using THB's notification pattern
void disposing(const css::lang::EventObject &_rEventSource)
sends a XEventObject::disposing notification to all listeners, and clears the listener container ...
virtual bool implNotify(const css::uno::Reference< css::lang::XEventListener > &_rxListener, const css::lang::EventObject &_rEvent) override
call a single listener
helper class for simple notification of the form LISTENER::METHOD( EVENT )
void impl_removeListener(const css::uno::Reference< css::lang::XEventListener > &_rxListener)
virtual bool implTypedNotify(const css::uno::Reference< ListenerClass > &_rxListener, const EventClass &_rEvent)=0
void clear()
clears the container without calling XEventListener::disposing at ...
::std::unique_ptr< ::comphelper::OInterfaceIteratorHelper2 > createIterator()
creates an iterator for looping through all registered listeners
void removeListener(const css::uno::Reference< ListenerClass > &_rxListener)
virtual bool implNotify(const css::uno::Reference< css::lang::XEventListener > &_rxListener, const css::lang::EventObject &_rEvent) override
call a single listener
void notify(const EventClass &_rEvent, NotificationMethod _pNotify)
typed notification
void addListener(const css::uno::Reference< ListenerClass > &_rxListener)
void addTypedListener(const css::uno::Reference< ListenerClass > &_rxListener)
abstract base class which manages a listener container, including THB's listener notification pattern...
::comphelper::OInterfaceContainerHelper2 m_aListeners
sal_Int32 getLength() const
Return the number of Elements in the container.
#define COMPHELPER_DLLPUBLIC
void impl_addListener(const css::uno::Reference< css::lang::XEventListener > &_rxListener)