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 
83  {
84  return ::comphelper::OInterfaceIteratorHelper2( m_aListeners );
85  }
86 
87  protected:
88  OListenerContainer( ::osl::Mutex& _rMutex );
89 
90  virtual ~OListenerContainer();
91 
92  void impl_addListener( const css::uno::Reference< css::lang::XEventListener >& _rxListener );
93  void impl_removeListener( const css::uno::Reference< css::lang::XEventListener >& _rxListener );
94 
105  bool impl_notify( const css::lang::EventObject& _rEvent );
106 
107  protected:
130  virtual bool implNotify(
131  const css::uno::Reference< css::lang::XEventListener >& _rxListener,
132  const css::lang::EventObject& _rEvent
133  ) = 0;
134  };
135 
136 
137  inline bool OListenerContainer::empty() const
138  {
139  return ( m_aListeners.getLength() == 0 );
140  }
141 
142 
143  //= OSimpleListenerContainer
144 
154  template< class LISTENER, class EVENT >
156  {
157  public:
158  typedef LISTENER ListenerClass;
159  typedef EVENT EventClass;
160  typedef void ( SAL_CALL LISTENER::*NotificationMethod )( const EventClass& );
161 
162  private:
163  NotificationMethod m_pNotificationMethod;
164 
165  public:
166  OSimpleListenerContainer( ::osl::Mutex& _rMutex )
167  :OListenerContainer( _rMutex )
168  ,m_pNotificationMethod( nullptr )
169  {
170  }
171 
172  void addListener( const css::uno::Reference< ListenerClass >& _rxListener )
173  {
174  OListenerContainer::impl_addListener( _rxListener.get() );
175  }
176 
177  void removeListener( const css::uno::Reference< ListenerClass >& _rxListener )
178  {
179  OListenerContainer::impl_removeListener( _rxListener.get() );
180  }
181 
182  // publish some otherwise hidden base functionality
187 
189  inline void notify( const EventClass& _rEvent, NotificationMethod _pNotify );
190 
191  private:
192  virtual bool implNotify(
193  const css::uno::Reference< css::lang::XEventListener >& _rxListener,
194  const css::lang::EventObject& _rEvent
195  ) override
196  {
197  const EventClass& rTypedEvent( static_cast< const EventClass& >( _rEvent ) );
198  ListenerClass* pTypedListener( static_cast< ListenerClass* >( _rxListener.get() ) );
199  (pTypedListener->*m_pNotificationMethod)( rTypedEvent );
200  return true;
201  }
202  };
203 
204 
205  template< class LISTENER, class EVENT >
206  inline void OSimpleListenerContainer< LISTENER, EVENT >::notify( const EventClass& _rEvent, NotificationMethod _pNotify )
207  {
208  m_pNotificationMethod = _pNotify;
210  m_pNotificationMethod = nullptr;
211  }
212 
213  //= OListenerContainerBase
214 
218  template< class ListenerClass, class EventClass >
220  {
221  public:
222  OListenerContainerBase( ::osl::Mutex& _rMutex ) : OListenerContainer( _rMutex )
223  {
224  }
225 
226  void addTypedListener( const css::uno::Reference< ListenerClass >& _rxListener )
227  {
228  OListenerContainer::impl_addListener( _rxListener.get() );
229  }
230 
231  void removeTypedListener( const css::uno::Reference< ListenerClass >& _rxListener )
232  {
233  OListenerContainer::impl_removeListener( _rxListener.get() );
234  }
235 
236  bool notify( const EventClass& _rEvent )
237  {
238  return OListenerContainer::impl_notify( _rEvent );
239  }
240 
242 
243  protected:
244  virtual bool implNotify(
245  const css::uno::Reference< css::lang::XEventListener >& _rxListener,
246  const css::lang::EventObject& _rEvent
247  ) override
248  {
249  return implTypedNotify(
250  css::uno::Reference< ListenerClass >( static_cast< ListenerClass* >( _rxListener.get() ) ),
251  static_cast< const EventClass& >( _rEvent )
252  );
253  }
254 
255  virtual bool implTypedNotify(
256  const css::uno::Reference< ListenerClass >& _rxListener,
257  const EventClass& _rEvent
258  ) = 0;
259  };
260 
261 } // namespace comphelper
262 
263 
264 #endif // INCLUDED_COMPHELPER_LISTENERNOTIFICATION_HXX
265 
266 /* 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 ...
This is the iterator of an OInterfaceContainerHelper2.
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)
::comphelper::OInterfaceIteratorHelper2 createIterator()
creates an iterator for looping through all registered listeners