1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <comphelper/listenernotification.hxx>
22 #include <com/sun/star/lang/DisposedException.hpp>
29 using namespace ::com::sun::star::uno
;
30 using namespace ::com::sun::star::lang
;
32 OListenerContainer::OListenerContainer( ::osl::Mutex
& _rMutex
)
33 :m_aListeners( _rMutex
)
37 OListenerContainer::~OListenerContainer() {}
40 void OListenerContainer::impl_addListener( const Reference
< XEventListener
>& _rxListener
)
42 OSL_PRECOND( _rxListener
.is(), "OListenerContainer::impl_addListener: a NULL listener?!" );
43 if ( _rxListener
.is() )
44 m_aListeners
.addInterface( _rxListener
);
48 void OListenerContainer::impl_removeListener( const Reference
< XEventListener
>& _rxListener
)
50 #if OSL_DEBUG_LEVEL > 0
51 ::cppu::OInterfaceIteratorHelper
aIter( m_aListeners
);
53 while ( aIter
.hasMoreElements() && !bFound
)
55 bFound
= ( Reference
< XInterface
>( aIter
.next() ) == _rxListener
);
57 OSL_ENSURE( bFound
, "OListenerContainer::impl_removeListener: sure your listener handling is correct? The given listener is not registered!" );
59 m_aListeners
.removeInterface( _rxListener
);
63 void OListenerContainer::disposing( const EventObject
& _rEventSource
)
65 m_aListeners
.disposeAndClear( _rEventSource
);
69 void OListenerContainer::clear()
75 bool OListenerContainer::impl_notify( const EventObject
& _rEvent
)
77 ::cppu::OInterfaceIteratorHelper
aIter( m_aListeners
);
78 bool bCancelled
= false;
79 while ( aIter
.hasMoreElements() && !bCancelled
)
81 Reference
< XEventListener
> xListener( static_cast< XEventListener
* >( aIter
.next() ) );
82 if ( !xListener
.is() )
87 bCancelled
= !implNotify( xListener
, _rEvent
);
89 catch( const DisposedException
& e
)
91 // DisposedExceptions from the listener might indicate a
92 // broken connection to a different environment.
94 OSL_ENSURE( e
.Context
.is(), "OListenerContainer::impl_notify: caught dispose exception with empty Context field" );
96 // If the exception stems from the listener then remove it
97 // from the list of listeners. If the Context field of the
98 // exception is empty this is interpreted to indicate the
100 if ( e
.Context
== xListener
|| !e
.Context
.is() )
109 } // namespace comphelper
112 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */