Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / chart2 / source / tools / ModifyListenerHelper.cxx
blobb921cd167109d08a715a9e4957adc8a1787f86e1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <ModifyListenerHelper.hxx>
21 #include <WeakListenerAdapter.hxx>
23 #include <tools/diagnose_ex.h>
25 using namespace ::com::sun::star;
27 using ::com::sun::star::uno::Reference;
29 namespace
32 void lcl_fireModifyEvent(
33 ::cppu::OBroadcastHelper const & rBroadcastHelper,
34 const Reference< uno::XWeak > & xEventSource,
35 const lang::EventObject * pEvent )
37 ::cppu::OInterfaceContainerHelper * pCntHlp = rBroadcastHelper.getContainer(
38 cppu::UnoType<util::XModifyListener>::get());
39 if( pCntHlp )
41 lang::EventObject aEventToSend;
42 if( pEvent )
43 aEventToSend = *pEvent;
44 else
45 aEventToSend.Source.set( xEventSource );
46 OSL_ENSURE( aEventToSend.Source.is(), "Sending event without source" );
48 ::cppu::OInterfaceIteratorHelper aIt( *pCntHlp );
50 while( aIt.hasMoreElements())
52 Reference< util::XModifyListener > xModListener( aIt.next(), uno::UNO_QUERY );
53 if( xModListener.is())
54 xModListener->modified( aEventToSend );
59 struct lcl_weakReferenceToSame
61 explicit lcl_weakReferenceToSame( const Reference< util::XModifyListener > & xModListener ) :
62 m_xHardRef( xModListener )
65 // argument type is same as tListenerMap::value_type&
66 bool operator() ( const std::pair<css::uno::WeakReference< css::util::XModifyListener>,
67 css::uno::Reference< css::util::XModifyListener> > & xElem )
69 Reference< util::XModifyListener > xWeakAsHard( xElem.first );
70 if( xWeakAsHard.is())
71 return (xWeakAsHard == m_xHardRef);
72 return false;
75 private:
76 Reference< util::XModifyListener > m_xHardRef;
79 } // anonymous namespace
81 namespace chart
83 namespace ModifyListenerHelper
86 uno::Reference< util::XModifyListener > createModifyEventForwarder()
88 return new ModifyEventForwarder();
91 ModifyEventForwarder::ModifyEventForwarder() :
92 ::cppu::WeakComponentImplHelper<
93 css::util::XModifyBroadcaster,
94 css::util::XModifyListener >( m_aMutex ),
95 m_aModifyListeners( m_aMutex )
99 void ModifyEventForwarder::AddListener( const Reference< util::XModifyListener >& aListener )
103 Reference< util::XModifyListener > xListenerToAdd( aListener );
105 Reference< uno::XWeak > xWeak( aListener, uno::UNO_QUERY );
106 if( xWeak.is())
108 // remember the helper class for later remove
109 uno::WeakReference< util::XModifyListener > xWeakRef( aListener );
110 xListenerToAdd.set( new WeakModifyListenerAdapter( xWeakRef ));
111 m_aListenerMap.emplace_back( xWeakRef, xListenerToAdd );
114 m_aModifyListeners.addListener( cppu::UnoType<decltype(xListenerToAdd)>::get(), xListenerToAdd );
116 catch( const uno::Exception & )
118 DBG_UNHANDLED_EXCEPTION("chart2");
122 void ModifyEventForwarder::RemoveListener( const Reference< util::XModifyListener >& aListener )
126 // look up fitting helper class that has been added
127 Reference< util::XModifyListener > xListenerToRemove( aListener );
128 tListenerMap::iterator aIt(
129 std::find_if( m_aListenerMap.begin(), m_aListenerMap.end(), lcl_weakReferenceToSame( aListener )));
130 if( aIt != m_aListenerMap.end())
132 xListenerToRemove.set( (*aIt).second );
133 // map entry is no longer needed
134 m_aListenerMap.erase( aIt );
137 m_aModifyListeners.removeListener( cppu::UnoType<decltype(aListener)>::get(), xListenerToRemove );
139 catch( const uno::Exception & )
141 DBG_UNHANDLED_EXCEPTION("chart2");
145 void ModifyEventForwarder::DisposeAndClear( const Reference< uno::XWeak > & xSource )
147 ::cppu::OInterfaceContainerHelper * pCntHlp = m_aModifyListeners.getContainer(
148 cppu::UnoType<util::XModifyListener>::get());
149 if( pCntHlp )
150 pCntHlp->disposeAndClear( lang::EventObject( xSource ) );
153 // ____ XModifyBroadcaster ____
154 void SAL_CALL ModifyEventForwarder::addModifyListener( const Reference< util::XModifyListener >& aListener )
156 AddListener( aListener );
159 void SAL_CALL ModifyEventForwarder::removeModifyListener( const Reference< util::XModifyListener >& aListener )
161 RemoveListener( aListener );
164 // ____ XModifyListener ____
165 void SAL_CALL ModifyEventForwarder::modified( const lang::EventObject& aEvent )
167 lcl_fireModifyEvent( m_aModifyListeners, Reference< uno::XWeak >(), &aEvent );
170 // ____ XEventListener (base of XModifyListener) ____
171 void SAL_CALL ModifyEventForwarder::disposing( const lang::EventObject& /* Source */ )
173 // nothing
176 // ____ WeakComponentImplHelperBase ____
177 void SAL_CALL ModifyEventForwarder::disposing()
179 // dispose was called at this
180 DisposeAndClear( this );
183 } // namespace ModifyListenerHelper
184 } // namespace chart
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */