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
31namespace com::sun::star::uno { class XInterface; }
32namespace osl { class Mutex; }
33
34namespace comphelper
35{
36
37namespace detail {
38
44 {
45 std::vector< css::uno::Reference< css::uno::XInterface > > *pAsVector;
46 css::uno::XInterface * pAsInterface;
48 };
49
50}
51
52
53class OInterfaceContainerHelper2;
62{
63public:
78
83
85 bool hasMoreElements() const
86 { return nRemain != 0; }
91 css::uno::XInterface * next();
92
98 void remove();
99
100private:
103 sal_Int32 nRemain;
105
107 OInterfaceIteratorHelper2 & operator = ( const OInterfaceIteratorHelper2 & ) = delete;
108};
109
110
122{
123public:
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
223private:
230 ::osl::Mutex & rMutex;
232 bool bInUse;
235
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
245private:
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
263template <typename ListenerT, typename FuncT>
264inline 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
281template< typename ListenerT, typename EventT >
282inline 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: */
NotifySingleListener(NotificationMethod method, const EventT &event)
void operator()(const css::uno::Reference< ListenerT > &listener) const
typedef void(SAL_CALL ListenerT::*NotificationMethod)(const EventT &)
void forEach(FuncT const &func)
Executes a functor for each contained listener of specified type, e.g.
void notifyEach(void(SAL_CALL ListenerT::*NotificationMethod)(const EventT &), const EventT &Event)
Calls a UNO listener method for each contained listener.
bool bIsList
TRUE -> aData.pAsVector is of type Sequence< XInterfaceSequence >.
OInterfaceContainerHelper2(const OInterfaceContainerHelper2 &)=delete
bool bInUse
TRUE -> used by an iterator.
OInterfaceContainerHelper2(::osl::Mutex &rMutex)
Create an interface container.
detail::element_alias2 aData
bIsList == TRUE -> aData.pAsVector of type vector< XInterfaceSequence >, otherwise aData....
This is the iterator of an OInterfaceContainerHelper2.
css::uno::XInterface * next()
Return the next element of the iterator.
bool hasMoreElements() const
Return true, if there are more elements in the iterator.
void remove()
Removes the current element (the last one returned by next()) from the underlying container.
OInterfaceIteratorHelper2(const OInterfaceIteratorHelper2 &)=delete
#define COMPHELPER_DLLPUBLIC
const ContainerEvent & m_rEvent
ContainerApprovalMethod m_pMethod
double getLength(const B2DPolygon &rCandidate)
OUStringBuffer & remove(OUStringBuffer &rIn, sal_Unicode c)
Removes all occurrences of a character from within the source string.
Definition: string.hxx:49
This is here to optimise space in the common case that there are zero or one listeners.
std::vector< css::uno::Reference< css::uno::XInterface > > * pAsVector