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 
119 {
120 public:
128  OInterfaceContainerHelper2( ::osl::Mutex & rMutex );
138  sal_Int32 getLength() const;
139 
143  std::vector< css::uno::Reference< css::uno::XInterface > > getElements() const;
144 
161  sal_Int32 addInterface( const css::uno::Reference< css::uno::XInterface > & rxIFace );
169  sal_Int32 removeInterface( const css::uno::Reference< css::uno::XInterface > & rxIFace );
174  void disposeAndClear( const css::lang::EventObject & rEvt );
178  void clear();
179 
191  template <typename ListenerT, typename FuncT>
192  inline void forEach( FuncT const& func );
193 
215  template< typename ListenerT, typename EventT >
216  inline void notifyEach( void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& ), const EventT& Event );
217 
218 private:
225  ::osl::Mutex & rMutex;
227  bool bInUse;
229  bool bIsList;
230 
232  OInterfaceContainerHelper2 & operator = ( const OInterfaceContainerHelper2 & ) = delete;
233 
234  /*
235  Duplicate content of the container and release the old one without destroying.
236  The mutex must be locked and the memberbInUse must be true.
237  */
238  void copyAndResetInUse();
239 
240 private:
241  template< typename ListenerT, typename EventT >
243  {
244  private:
245  typedef void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& );
246  NotificationMethod const m_pMethod;
247  const EventT& m_rEvent;
248  public:
249  NotifySingleListener( NotificationMethod method, const EventT& event ) : m_pMethod( method ), m_rEvent( event ) { }
250 
251  void operator()( const css::uno::Reference<ListenerT>& listener ) const
252  {
253  (listener.get()->*m_pMethod)( m_rEvent );
254  }
255  };
256 };
257 
258 template <typename ListenerT, typename FuncT>
259 inline void OInterfaceContainerHelper2::forEach( FuncT const& func )
260 {
261  OInterfaceIteratorHelper2 iter( *this );
262  while (iter.hasMoreElements()) {
263  css::uno::Reference<ListenerT> const xListener( iter.next(), css::uno::UNO_QUERY );
264  if (xListener.is()) {
265  try {
266  func( xListener );
267  }
268  catch (css::lang::DisposedException const& exc) {
269  if (exc.Context == xListener)
270  iter.remove();
271  }
272  }
273  }
274 }
275 
276 template< typename ListenerT, typename EventT >
277 inline void OInterfaceContainerHelper2::notifyEach( void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& ), const EventT& Event )
278 {
279  forEach< ListenerT, NotifySingleListener< ListenerT, EventT > >( NotifySingleListener< ListenerT, EventT >( NotificationMethod, Event ) );
280 }
281 
282 }
283 #endif
284 
285 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
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 InterfaceContainerHelper.
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