Bump for 3.6-28
[LibreOffice.git] / chart2 / source / inc / ModifyListenerHelper.hxx
blobd46eba5cb44ad0421bed1ebfbeaa3851694a7ed1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
28 #ifndef CHART2_MODIFYLISTENERHELPER_HXX
29 #define CHART2_MODIFYLISTENERHELPER_HXX
31 #include <cppuhelper/interfacecontainer.hxx>
32 #include <com/sun/star/util/XModifyListener.hpp>
33 #include <com/sun/star/util/XModifyBroadcaster.hpp>
34 #include <com/sun/star/util/XModifiable.hpp>
35 #include <com/sun/star/uno/XWeak.hpp>
36 #include <cppuhelper/implbase1.hxx>
37 #include <cppuhelper/compbase2.hxx>
38 #include <cppuhelper/weakref.hxx>
40 #include "MutexContainer.hxx"
41 #include "charttoolsdllapi.hxx"
43 #include <list>
44 #include <algorithm>
45 #include <functional>
46 #include <utility>
48 namespace com { namespace sun { namespace star {
49 namespace chart2
51 class XChartDocument;
52 }}}}
54 namespace chart
56 namespace ModifyListenerHelper
59 OOO_DLLPUBLIC_CHARTTOOLS ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > createModifyEventForwarder();
61 // ================================================================================
63 /** This helper class serves as forwarder of modify events. It can be used
64 whenever an object has to send modify events after it gets a modify event of
65 one of its children.
67 <p>The listeners are held as WeakReferences if they support XWeak. Thus the
68 life time of the listeners is independent of the broadcaster's lifetime in
69 this case.</p>
71 class ModifyEventForwarder :
72 public MutexContainer,
73 public ::cppu::WeakComponentImplHelper2<
74 ::com::sun::star::util::XModifyBroadcaster,
75 ::com::sun::star::util::XModifyListener >
77 public:
78 ModifyEventForwarder();
80 void FireEvent( const ::com::sun::star::lang::EventObject & rEvent );
82 void AddListener(
83 const ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener >& aListener );
84 void RemoveListener(
85 const ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener >& aListener );
87 protected:
88 // ____ XModifyBroadcaster ____
89 virtual void SAL_CALL addModifyListener(
90 const ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener >& aListener )
91 throw (::com::sun::star::uno::RuntimeException);
92 virtual void SAL_CALL removeModifyListener(
93 const ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener >& aListener )
94 throw (::com::sun::star::uno::RuntimeException);
96 // ____ XModifyListener ____
97 virtual void SAL_CALL modified(
98 const ::com::sun::star::lang::EventObject& aEvent )
99 throw (::com::sun::star::uno::RuntimeException);
101 // ____ XEventListener (base of XModifyListener) ____
102 virtual void SAL_CALL disposing(
103 const ::com::sun::star::lang::EventObject& Source )
104 throw (::com::sun::star::uno::RuntimeException);
106 // ____ WeakComponentImplHelperBase ____
107 virtual void SAL_CALL disposing();
109 private:
110 /// call disposing() at all listeners and remove all listeners
111 void DisposeAndClear( const ::com::sun::star::uno::Reference<
112 ::com::sun::star::uno::XWeak > & xSource );
114 // ::osl::Mutex & m_rMutex;
115 ::cppu::OBroadcastHelper m_aModifyListeners;
117 typedef ::std::list<
118 ::std::pair<
119 ::com::sun::star::uno::WeakReference< ::com::sun::star::util::XModifyListener >,
120 ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > > >
121 tListenerMap;
123 tListenerMap m_aListenerMap;
126 // ================================================================================
128 namespace impl
131 template< class InterfaceRef >
132 struct addListenerFunctor : public ::std::unary_function< InterfaceRef, void >
134 explicit addListenerFunctor( const ::com::sun::star::uno::Reference<
135 ::com::sun::star::util::XModifyListener > & xListener ) :
136 m_xListener( xListener )
139 void operator() ( const InterfaceRef & xObject )
141 ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyBroadcaster >
142 xBroadcaster( xObject, ::com::sun::star::uno::UNO_QUERY );
143 if( xBroadcaster.is() && m_xListener.is())
144 xBroadcaster->addModifyListener( m_xListener );
146 private:
147 ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > m_xListener;
150 template< class InterfaceRef >
151 struct removeListenerFunctor : public ::std::unary_function< InterfaceRef, void >
153 explicit removeListenerFunctor( const ::com::sun::star::uno::Reference<
154 ::com::sun::star::util::XModifyListener > & xListener ) :
155 m_xListener( xListener )
158 void operator() ( const InterfaceRef & xObject )
160 ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyBroadcaster >
161 xBroadcaster( xObject, ::com::sun::star::uno::UNO_QUERY );
162 if( xBroadcaster.is() && m_xListener.is())
163 xBroadcaster->removeModifyListener( m_xListener );
165 private:
166 ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > m_xListener;
169 template< class Pair >
170 struct addListenerToMappedElementFunctor : public ::std::unary_function< Pair, void >
172 explicit addListenerToMappedElementFunctor( const ::com::sun::star::uno::Reference<
173 ::com::sun::star::util::XModifyListener > & xListener ) :
174 m_xListener( xListener )
177 void operator() ( const Pair & aPair )
179 ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyBroadcaster >
180 xBroadcaster( aPair.second, ::com::sun::star::uno::UNO_QUERY );
181 if( xBroadcaster.is() && m_xListener.is())
182 xBroadcaster->addModifyListener( m_xListener );
184 private:
185 ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > m_xListener;
188 template< class Pair >
189 struct removeListenerFromMappedElementFunctor : public ::std::unary_function< Pair, void >
191 explicit removeListenerFromMappedElementFunctor( const ::com::sun::star::uno::Reference<
192 ::com::sun::star::util::XModifyListener > & xListener ) :
193 m_xListener( xListener )
196 void operator() ( const Pair & aPair )
198 ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyBroadcaster >
199 xBroadcaster( aPair.second, ::com::sun::star::uno::UNO_QUERY );
200 if( xBroadcaster.is() && m_xListener.is())
201 xBroadcaster->removeModifyListener( m_xListener );
203 private:
204 ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > m_xListener;
207 } // namespace impl
209 // --------------------------------------------------------------------------------
212 template< class InterfaceRef >
213 void addListener(
214 const InterfaceRef & xObject,
215 const ::com::sun::star::uno::Reference<
216 ::com::sun::star::util::XModifyListener > & xListener )
218 if( xListener.is())
220 impl::addListenerFunctor< InterfaceRef > aFunctor( xListener );
221 aFunctor( xObject );
225 template< class Container >
226 void addListenerToAllElements(
227 const Container & rContainer,
228 const ::com::sun::star::uno::Reference<
229 ::com::sun::star::util::XModifyListener > & xListener )
231 if( xListener.is())
232 ::std::for_each( rContainer.begin(), rContainer.end(),
233 impl::addListenerFunctor< typename Container::value_type >( xListener ));
236 template< class Container >
237 void addListenerToAllMapElements(
238 const Container & rContainer,
239 const ::com::sun::star::uno::Reference<
240 ::com::sun::star::util::XModifyListener > & xListener )
242 if( xListener.is())
243 ::std::for_each( rContainer.begin(), rContainer.end(),
244 impl::addListenerToMappedElementFunctor< typename Container::value_type >( xListener ));
247 template< typename T >
248 void addListenerToAllSequenceElements(
249 const ::com::sun::star::uno::Sequence< T > & rSequence,
250 const ::com::sun::star::uno::Reference<
251 ::com::sun::star::util::XModifyListener > & xListener )
253 if( xListener.is())
254 ::std::for_each( rSequence.getConstArray(), rSequence.getConstArray() + rSequence.getLength(),
255 impl::addListenerFunctor< T >( xListener ));
258 template< class InterfaceRef >
259 void removeListener(
260 const InterfaceRef & xObject,
261 const ::com::sun::star::uno::Reference<
262 ::com::sun::star::util::XModifyListener > & xListener )
264 if( xListener.is())
266 impl::removeListenerFunctor< InterfaceRef > aFunctor( xListener );
267 aFunctor( xObject );
271 template< class Container >
272 void removeListenerFromAllElements(
273 const Container & rContainer,
274 const ::com::sun::star::uno::Reference<
275 ::com::sun::star::util::XModifyListener > & xListener )
277 if( xListener.is())
278 ::std::for_each( rContainer.begin(), rContainer.end(),
279 impl::removeListenerFunctor< typename Container::value_type >( xListener ));
282 template< class Container >
283 void removeListenerFromAllMapElements(
284 const Container & rContainer,
285 const ::com::sun::star::uno::Reference<
286 ::com::sun::star::util::XModifyListener > & xListener )
288 if( xListener.is())
289 ::std::for_each( rContainer.begin(), rContainer.end(),
290 impl::removeListenerFromMappedElementFunctor< typename Container::value_type >( xListener ));
293 template< typename T >
294 void removeListenerFromAllSequenceElements(
295 const ::com::sun::star::uno::Sequence< T > & rSequence,
296 const ::com::sun::star::uno::Reference<
297 ::com::sun::star::util::XModifyListener > & xListener )
299 if( xListener.is())
300 ::std::for_each( rSequence.getConstArray(), rSequence.getConstArray() + rSequence.getLength(),
301 impl::removeListenerFunctor< T >( xListener ));
304 } // namespace ModifyListenerHelper
305 } // namespace chart
307 // CHART2_MODIFYLISTENERHELPER_HXX
308 #endif
310 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */