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