LibreOffice Module comphelper (master)  1
interfacecontainer2.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 #ifndef INCLUDED_COMPHELPER_INTERFACECONTAINER2_H
20 #define INCLUDED_COMPHELPER_INTERFACECONTAINER2_H
21 
22 #include <sal/config.h>
23 
24 #include <vector>
25 
26 #include <com/sun/star/lang/EventObject.hpp>
27 
28 #include <com/sun/star/lang/DisposedException.hpp>
30 
31 namespace com::sun::star::uno { class XInterface; }
32 namespace osl { class Mutex; }
33 
34 namespace comphelper
35 {
36 
37 namespace detail {
38 
44  {
45  std::vector< css::uno::Reference< css::uno::XInterface > > *pAsVector;
46  css::uno::XInterface * pAsInterface;
47  element_alias2() : pAsInterface(nullptr) {}
48  };
49 
50 }
51 
52 
53 class OInterfaceContainerHelper2;
62 {
63 public:
78 
83 
85  bool hasMoreElements() const
86  { return nRemain != 0; }
91  css::uno::XInterface * next();
92 
98  void remove();
99 
100 private:
103  sal_Int32 nRemain;
104  bool bIsList;
105 
107  OInterfaceIteratorHelper2 & operator = ( const OInterfaceIteratorHelper2 & ) = delete;
108 };
109 
110 
122 {
123 public:
131  OInterfaceContainerHelper2( ::osl::Mutex & rMutex );
141  sal_Int32 getLength() const;
142 
146  std::vector< css::uno::Reference< css::uno::XInterface > > getElements() const;
147 
164  sal_Int32 addInterface( const css::uno::Reference< css::uno::XInterface > & rxIFace );
172  sal_Int32 removeInterface( const css::uno::Reference< css::uno::XInterface > & rxIFace );
174  css::uno::Reference< css::uno::XInterface > getInterface(sal_Int32 nIndex) const;
179  void disposeAndClear( const css::lang::EventObject & rEvt );
183  void clear();
184 
196  template <typename ListenerT, typename FuncT>
197  inline void forEach( FuncT const& func );
198 
220  template< typename ListenerT, typename EventT >
221  inline void notifyEach( void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& ), const EventT& Event );
222 
223 private:
230  ::osl::Mutex & rMutex;
232  bool bInUse;
234  bool bIsList;
235 
237  OInterfaceContainerHelper2 & operator = ( const OInterfaceContainerHelper2 & ) = delete;
238 
239  /*
240  Duplicate content of the container and release the old one without destroying.
241  The mutex must be locked and the memberbInUse must be true.
242  */
243  void copyAndResetInUse();
244 
245 private:
246  template< typename ListenerT, typename EventT >
248  {
249  private:
250  typedef void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& );
251  NotificationMethod const m_pMethod;
252  const EventT& m_rEvent;
253  public:
254  NotifySingleListener( NotificationMethod method, const EventT& event ) : m_pMethod( method ), m_rEvent( event ) { }
255 
256  void operator()( const css::uno::Reference<ListenerT>& listener ) const
257  {
258  (listener.get()->*m_pMethod)( m_rEvent );
259  }
260  };
261 };
262 
263 template <typename ListenerT, typename FuncT>
264 inline void OInterfaceContainerHelper2::forEach( FuncT const& func )
265 {
266  OInterfaceIteratorHelper2 iter( *this );
267  while (iter.hasMoreElements()) {
268  css::uno::Reference<ListenerT> const xListener( iter.next(), css::uno::UNO_QUERY );
269  if (xListener.is()) {
270  try {
271  func( xListener );
272  }
273  catch (css::lang::DisposedException const& exc) {
274  if (exc.Context == xListener)
275  iter.remove();
276  }
277  }
278  }
279 }
280 
281 template< typename ListenerT, typename EventT >
282 inline void OInterfaceContainerHelper2::notifyEach( void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& ), const EventT& Event )
283 {
284  forEach< ListenerT, NotifySingleListener< ListenerT, EventT > >( NotifySingleListener< ListenerT, EventT >( NotificationMethod, Event ) );
285 }
286 
287 }
288 #endif
289 
290 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 nIndex
void notifyEach(void(SAL_CALL ListenerT::*NotificationMethod)(const EventT &), const EventT &Event)
Calls a UNO listener method for each contained listener.
bool hasMoreElements() const
Return true, if there are more elements in the iterator.
NotifySingleListener(NotificationMethod method, const EventT &event)
This is here to optimise space in the common case that there are zero or one listeners.
bool bInUse
TRUE -> used by an iterator.
std::vector< css::uno::Reference< css::uno::XInterface > > * pAsVector
detail::element_alias2 aData
bIsList == TRUE -> aData.pAsVector of type vector< XInterfaceSequence >, otherwise aData...
This is the iterator of an OInterfaceContainerHelper2.
void operator()(const css::uno::Reference< ListenerT > &listener) const
double getLength(const B2DPolygon &rCandidate)
bool bIsList
TRUE -> aData.pAsVector is of type Sequence< XInterfaceSequence >.
#define COMPHELPER_DLLPUBLIC
css::uno::XInterface * next()
Return the next element of the iterator.
void remove()
Removes the current element (the last one returned by next()) from the underlying container...
void forEach(FuncT const &func)
Executes a functor for each contained listener of specified type, e.g.
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo