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  //for docpp
35 namespace comphelper
36 {
37 
38 namespace detail {
39 
45  {
46  std::vector< css::uno::Reference< css::uno::XInterface > > *pAsVector;
47  css::uno::XInterface * pAsInterface;
48  element_alias2() : pAsInterface(nullptr) {}
49  };
50 
51 }
52 
53 
54 class OInterfaceContainerHelper2;
63 {
64 public:
79 
84 
86  bool hasMoreElements() const
87  { return nRemain != 0; }
92  css::uno::XInterface * next();
93 
99  void remove();
100 
101 private:
104  sal_Int32 nRemain;
105  bool bIsList;
106 
108  OInterfaceIteratorHelper2 & operator = ( const OInterfaceIteratorHelper2 & ) = delete;
109 };
110 
111 
123 {
124 public:
132  OInterfaceContainerHelper2( ::osl::Mutex & rMutex );
142  sal_Int32 getLength() const;
143 
147  std::vector< css::uno::Reference< css::uno::XInterface > > getElements() const;
148 
165  sal_Int32 addInterface( const css::uno::Reference< css::uno::XInterface > & rxIFace );
173  sal_Int32 removeInterface( const css::uno::Reference< css::uno::XInterface > & rxIFace );
175  css::uno::Reference< css::uno::XInterface > getInterface(sal_Int32 nIndex) const;
180  void disposeAndClear( const css::lang::EventObject & rEvt );
184  void clear();
185 
197  template <typename ListenerT, typename FuncT>
198  inline void forEach( FuncT const& func );
199 
221  template< typename ListenerT, typename EventT >
222  inline void notifyEach( void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& ), const EventT& Event );
223 
224 private:
231  ::osl::Mutex & rMutex;
233  bool bInUse;
235  bool bIsList;
236 
238  OInterfaceContainerHelper2 & operator = ( const OInterfaceContainerHelper2 & ) = delete;
239 
240  /*
241  Duplicate content of the container and release the old one without destroying.
242  The mutex must be locked and the memberbInUse must be true.
243  */
244  void copyAndResetInUse();
245 
246 private:
247  template< typename ListenerT, typename EventT >
249  {
250  private:
251  typedef void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& );
252  NotificationMethod const m_pMethod;
253  const EventT& m_rEvent;
254  public:
255  NotifySingleListener( NotificationMethod method, const EventT& event ) : m_pMethod( method ), m_rEvent( event ) { }
256 
257  void operator()( const css::uno::Reference<ListenerT>& listener ) const
258  {
259  (listener.get()->*m_pMethod)( m_rEvent );
260  }
261  };
262 };
263 
264 template <typename ListenerT, typename FuncT>
265 inline void OInterfaceContainerHelper2::forEach( FuncT const& func )
266 {
267  OInterfaceIteratorHelper2 iter( *this );
268  while (iter.hasMoreElements()) {
269  css::uno::Reference<ListenerT> const xListener( iter.next(), css::uno::UNO_QUERY );
270  if (xListener.is()) {
271  try {
272  func( xListener );
273  }
274  catch (css::lang::DisposedException const& exc) {
275  if (exc.Context == xListener)
276  iter.remove();
277  }
278  }
279  }
280 }
281 
282 template< typename ListenerT, typename EventT >
283 inline void OInterfaceContainerHelper2::notifyEach( void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& ), const EventT& Event )
284 {
285  forEach< ListenerT, NotifySingleListener< ListenerT, EventT > >( NotifySingleListener< ListenerT, EventT >( NotificationMethod, Event ) );
286 }
287 
288 }
289 #endif
290 
291 /* 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