Line data Source code
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_CHART2_SOURCE_INC_MODIFYLISTENERHELPER_HXX
20 : #define INCLUDED_CHART2_SOURCE_INC_MODIFYLISTENERHELPER_HXX
21 :
22 : #include <cppuhelper/interfacecontainer.hxx>
23 : #include <com/sun/star/util/XModifyListener.hpp>
24 : #include <com/sun/star/util/XModifyBroadcaster.hpp>
25 : #include <com/sun/star/util/XModifiable.hpp>
26 : #include <com/sun/star/uno/XWeak.hpp>
27 : #include <cppuhelper/implbase1.hxx>
28 : #include <cppuhelper/compbase2.hxx>
29 : #include <cppuhelper/weakref.hxx>
30 :
31 : #include "MutexContainer.hxx"
32 : #include "charttoolsdllapi.hxx"
33 :
34 : #include <list>
35 : #include <algorithm>
36 : #include <functional>
37 : #include <utility>
38 :
39 : namespace chart
40 : {
41 : namespace ModifyListenerHelper
42 : {
43 :
44 : OOO_DLLPUBLIC_CHARTTOOLS ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > createModifyEventForwarder();
45 :
46 : /** This helper class serves as forwarder of modify events. It can be used
47 : whenever an object has to send modify events after it gets a modify event of
48 : one of its children.
49 :
50 : <p>The listeners are held as WeakReferences if they support XWeak. Thus the
51 : life time of the listeners is independent of the broadcaster's lifetime in
52 : this case.</p>
53 : */
54 382642 : class ModifyEventForwarder :
55 : public MutexContainer,
56 : public ::cppu::WeakComponentImplHelper2<
57 : ::com::sun::star::util::XModifyBroadcaster,
58 : ::com::sun::star::util::XModifyListener >
59 : {
60 : public:
61 : ModifyEventForwarder();
62 :
63 : void FireEvent( const ::com::sun::star::lang::EventObject & rEvent );
64 :
65 : void AddListener(
66 : const ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener >& aListener );
67 : void RemoveListener(
68 : const ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener >& aListener );
69 :
70 : protected:
71 : // ____ XModifyBroadcaster ____
72 : virtual void SAL_CALL addModifyListener(
73 : const ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener >& aListener )
74 : throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
75 : virtual void SAL_CALL removeModifyListener(
76 : const ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener >& aListener )
77 : throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
78 :
79 : // ____ XModifyListener ____
80 : virtual void SAL_CALL modified(
81 : const ::com::sun::star::lang::EventObject& aEvent )
82 : throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
83 :
84 : // ____ XEventListener (base of XModifyListener) ____
85 : virtual void SAL_CALL disposing(
86 : const ::com::sun::star::lang::EventObject& Source )
87 : throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
88 :
89 : // ____ WeakComponentImplHelperBase ____
90 : virtual void SAL_CALL disposing() SAL_OVERRIDE;
91 :
92 : private:
93 : /// call disposing() at all listeners and remove all listeners
94 : void DisposeAndClear( const ::com::sun::star::uno::Reference<
95 : ::com::sun::star::uno::XWeak > & xSource );
96 :
97 : // ::osl::Mutex & m_rMutex;
98 : ::cppu::OBroadcastHelper m_aModifyListeners;
99 :
100 : typedef ::std::list<
101 : ::std::pair<
102 : ::com::sun::star::uno::WeakReference< ::com::sun::star::util::XModifyListener >,
103 : ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > > >
104 : tListenerMap;
105 :
106 : tListenerMap m_aListenerMap;
107 : };
108 :
109 : namespace impl
110 : {
111 :
112 : template< class InterfaceRef >
113 41547 : struct addListenerFunctor : public ::std::unary_function< InterfaceRef, void >
114 : {
115 32607 : explicit addListenerFunctor( const ::com::sun::star::uno::Reference<
116 : ::com::sun::star::util::XModifyListener > & xListener ) :
117 32607 : m_xListener( xListener )
118 32607 : {}
119 :
120 32753 : void operator() ( const InterfaceRef & xObject )
121 : {
122 : ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyBroadcaster >
123 32753 : xBroadcaster( xObject, ::com::sun::star::uno::UNO_QUERY );
124 32753 : if( xBroadcaster.is() && m_xListener.is())
125 32505 : xBroadcaster->addModifyListener( m_xListener );
126 32753 : }
127 : private:
128 : ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > m_xListener;
129 : };
130 :
131 : template< class InterfaceRef >
132 537202 : struct removeListenerFunctor : public ::std::unary_function< InterfaceRef, void >
133 : {
134 202034 : explicit removeListenerFunctor( const ::com::sun::star::uno::Reference<
135 : ::com::sun::star::util::XModifyListener > & xListener ) :
136 202034 : m_xListener( xListener )
137 202034 : {}
138 :
139 43374 : void operator() ( const InterfaceRef & xObject )
140 : {
141 : ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyBroadcaster >
142 43374 : xBroadcaster( xObject, ::com::sun::star::uno::UNO_QUERY );
143 43374 : if( xBroadcaster.is() && m_xListener.is())
144 30384 : xBroadcaster->removeModifyListener( m_xListener );
145 43374 : }
146 : private:
147 : ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > m_xListener;
148 : };
149 :
150 : template< class Pair >
151 0 : struct addListenerToMappedElementFunctor : public ::std::unary_function< Pair, void >
152 : {
153 0 : explicit addListenerToMappedElementFunctor( const ::com::sun::star::uno::Reference<
154 : ::com::sun::star::util::XModifyListener > & xListener ) :
155 0 : m_xListener( xListener )
156 0 : {}
157 :
158 0 : void operator() ( const Pair & aPair )
159 : {
160 : ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyBroadcaster >
161 0 : xBroadcaster( aPair.second, ::com::sun::star::uno::UNO_QUERY );
162 0 : if( xBroadcaster.is() && m_xListener.is())
163 0 : xBroadcaster->addModifyListener( m_xListener );
164 0 : }
165 : private:
166 : ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > m_xListener;
167 : };
168 :
169 : template< class Pair >
170 4038 : struct removeListenerFromMappedElementFunctor : public ::std::unary_function< Pair, void >
171 : {
172 1346 : explicit removeListenerFromMappedElementFunctor( const ::com::sun::star::uno::Reference<
173 : ::com::sun::star::util::XModifyListener > & xListener ) :
174 1346 : m_xListener( xListener )
175 1346 : {}
176 :
177 644 : void operator() ( const Pair & aPair )
178 : {
179 : ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyBroadcaster >
180 644 : xBroadcaster( aPair.second, ::com::sun::star::uno::UNO_QUERY );
181 644 : if( xBroadcaster.is() && m_xListener.is())
182 644 : xBroadcaster->removeModifyListener( m_xListener );
183 644 : }
184 : private:
185 : ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > m_xListener;
186 : };
187 :
188 : } // namespace impl
189 :
190 : template< class InterfaceRef >
191 28137 : void addListener(
192 : const InterfaceRef & xObject,
193 : const ::com::sun::star::uno::Reference<
194 : ::com::sun::star::util::XModifyListener > & xListener )
195 : {
196 28137 : if( xListener.is())
197 : {
198 28137 : impl::addListenerFunctor< InterfaceRef > aFunctor( xListener );
199 28137 : aFunctor( xObject );
200 : }
201 28137 : }
202 :
203 : template< class Container >
204 4470 : void addListenerToAllElements(
205 : const Container & rContainer,
206 : const ::com::sun::star::uno::Reference<
207 : ::com::sun::star::util::XModifyListener > & xListener )
208 : {
209 4470 : if( xListener.is())
210 4470 : ::std::for_each( rContainer.begin(), rContainer.end(),
211 : impl::addListenerFunctor< typename Container::value_type >( xListener ));
212 4470 : }
213 :
214 : template< class Container >
215 0 : void addListenerToAllMapElements(
216 : const Container & rContainer,
217 : const ::com::sun::star::uno::Reference<
218 : ::com::sun::star::util::XModifyListener > & xListener )
219 : {
220 0 : if( xListener.is())
221 0 : ::std::for_each( rContainer.begin(), rContainer.end(),
222 : impl::addListenerToMappedElementFunctor< typename Container::value_type >( xListener ));
223 0 : }
224 :
225 : template< typename T >
226 0 : void addListenerToAllSequenceElements(
227 : const ::com::sun::star::uno::Sequence< T > & rSequence,
228 : const ::com::sun::star::uno::Reference<
229 : ::com::sun::star::util::XModifyListener > & xListener )
230 : {
231 0 : if( xListener.is())
232 0 : ::std::for_each( rSequence.getConstArray(), rSequence.getConstArray() + rSequence.getLength(),
233 : impl::addListenerFunctor< T >( xListener ));
234 0 : }
235 :
236 : template< class InterfaceRef >
237 34450 : void removeListener(
238 : const InterfaceRef & xObject,
239 : const ::com::sun::star::uno::Reference<
240 : ::com::sun::star::util::XModifyListener > & xListener )
241 : {
242 34450 : if( xListener.is())
243 : {
244 34450 : impl::removeListenerFunctor< InterfaceRef > aFunctor( xListener );
245 34450 : aFunctor( xObject );
246 : }
247 34450 : }
248 :
249 : template< class Container >
250 165384 : void removeListenerFromAllElements(
251 : const Container & rContainer,
252 : const ::com::sun::star::uno::Reference<
253 : ::com::sun::star::util::XModifyListener > & xListener )
254 : {
255 165384 : if( xListener.is())
256 165384 : ::std::for_each( rContainer.begin(), rContainer.end(),
257 : impl::removeListenerFunctor< typename Container::value_type >( xListener ));
258 165384 : }
259 :
260 : template< class Container >
261 1346 : void removeListenerFromAllMapElements(
262 : const Container & rContainer,
263 : const ::com::sun::star::uno::Reference<
264 : ::com::sun::star::util::XModifyListener > & xListener )
265 : {
266 1346 : if( xListener.is())
267 1346 : ::std::for_each( rContainer.begin(), rContainer.end(),
268 : impl::removeListenerFromMappedElementFunctor< typename Container::value_type >( xListener ));
269 1346 : }
270 :
271 : template< typename T >
272 2200 : void removeListenerFromAllSequenceElements(
273 : const ::com::sun::star::uno::Sequence< T > & rSequence,
274 : const ::com::sun::star::uno::Reference<
275 : ::com::sun::star::util::XModifyListener > & xListener )
276 : {
277 2200 : if( xListener.is())
278 2200 : ::std::for_each( rSequence.getConstArray(), rSequence.getConstArray() + rSequence.getLength(),
279 : impl::removeListenerFunctor< T >( xListener ));
280 2200 : }
281 :
282 : } // namespace ModifyListenerHelper
283 : } // namespace chart
284 :
285 : // INCLUDED_CHART2_SOURCE_INC_MODIFYLISTENERHELPER_HXX
286 : #endif
287 :
288 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|