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 .
19 #ifndef CHART2_MODIFYLISTENERHELPER_HXX
20 #define CHART2_MODIFYLISTENERHELPER_HXX
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>
31 #include "MutexContainer.hxx"
32 #include "charttoolsdllapi.hxx"
41 namespace ModifyListenerHelper
44 OOO_DLLPUBLIC_CHARTTOOLS ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
> createModifyEventForwarder();
46 // ================================================================================
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
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
56 class ModifyEventForwarder
:
57 public MutexContainer
,
58 public ::cppu::WeakComponentImplHelper2
<
59 ::com::sun::star::util::XModifyBroadcaster
,
60 ::com::sun::star::util::XModifyListener
>
63 ModifyEventForwarder();
65 void FireEvent( const ::com::sun::star::lang::EventObject
& rEvent
);
68 const ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
>& aListener
);
70 const ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
>& aListener
);
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
);
81 // ____ XModifyListener ____
82 virtual void SAL_CALL
modified(
83 const ::com::sun::star::lang::EventObject
& aEvent
)
84 throw (::com::sun::star::uno::RuntimeException
);
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
);
91 // ____ WeakComponentImplHelperBase ____
92 virtual void SAL_CALL
disposing();
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
);
99 // ::osl::Mutex & m_rMutex;
100 ::cppu::OBroadcastHelper m_aModifyListeners
;
104 ::com::sun::star::uno::WeakReference
< ::com::sun::star::util::XModifyListener
>,
105 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
> > >
108 tListenerMap m_aListenerMap
;
111 // ================================================================================
116 template< class InterfaceRef
>
117 struct addListenerFunctor
: public ::std::unary_function
< InterfaceRef
, void >
119 explicit addListenerFunctor( const ::com::sun::star::uno::Reference
<
120 ::com::sun::star::util::XModifyListener
> & xListener
) :
121 m_xListener( xListener
)
124 void operator() ( const InterfaceRef
& xObject
)
126 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyBroadcaster
>
127 xBroadcaster( xObject
, ::com::sun::star::uno::UNO_QUERY
);
128 if( xBroadcaster
.is() && m_xListener
.is())
129 xBroadcaster
->addModifyListener( m_xListener
);
132 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
> m_xListener
;
135 template< class InterfaceRef
>
136 struct removeListenerFunctor
: public ::std::unary_function
< InterfaceRef
, void >
138 explicit removeListenerFunctor( const ::com::sun::star::uno::Reference
<
139 ::com::sun::star::util::XModifyListener
> & xListener
) :
140 m_xListener( xListener
)
143 void operator() ( const InterfaceRef
& xObject
)
145 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyBroadcaster
>
146 xBroadcaster( xObject
, ::com::sun::star::uno::UNO_QUERY
);
147 if( xBroadcaster
.is() && m_xListener
.is())
148 xBroadcaster
->removeModifyListener( m_xListener
);
151 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
> m_xListener
;
154 template< class Pair
>
155 struct addListenerToMappedElementFunctor
: public ::std::unary_function
< Pair
, void >
157 explicit addListenerToMappedElementFunctor( const ::com::sun::star::uno::Reference
<
158 ::com::sun::star::util::XModifyListener
> & xListener
) :
159 m_xListener( xListener
)
162 void operator() ( const Pair
& aPair
)
164 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyBroadcaster
>
165 xBroadcaster( aPair
.second
, ::com::sun::star::uno::UNO_QUERY
);
166 if( xBroadcaster
.is() && m_xListener
.is())
167 xBroadcaster
->addModifyListener( m_xListener
);
170 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
> m_xListener
;
173 template< class Pair
>
174 struct removeListenerFromMappedElementFunctor
: public ::std::unary_function
< Pair
, void >
176 explicit removeListenerFromMappedElementFunctor( const ::com::sun::star::uno::Reference
<
177 ::com::sun::star::util::XModifyListener
> & xListener
) :
178 m_xListener( xListener
)
181 void operator() ( const Pair
& aPair
)
183 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyBroadcaster
>
184 xBroadcaster( aPair
.second
, ::com::sun::star::uno::UNO_QUERY
);
185 if( xBroadcaster
.is() && m_xListener
.is())
186 xBroadcaster
->removeModifyListener( m_xListener
);
189 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
> m_xListener
;
194 // --------------------------------------------------------------------------------
197 template< class InterfaceRef
>
199 const InterfaceRef
& xObject
,
200 const ::com::sun::star::uno::Reference
<
201 ::com::sun::star::util::XModifyListener
> & xListener
)
205 impl::addListenerFunctor
< InterfaceRef
> aFunctor( xListener
);
210 template< class Container
>
211 void addListenerToAllElements(
212 const Container
& rContainer
,
213 const ::com::sun::star::uno::Reference
<
214 ::com::sun::star::util::XModifyListener
> & xListener
)
217 ::std::for_each( rContainer
.begin(), rContainer
.end(),
218 impl::addListenerFunctor
< typename
Container::value_type
>( xListener
));
221 template< class Container
>
222 void addListenerToAllMapElements(
223 const Container
& rContainer
,
224 const ::com::sun::star::uno::Reference
<
225 ::com::sun::star::util::XModifyListener
> & xListener
)
228 ::std::for_each( rContainer
.begin(), rContainer
.end(),
229 impl::addListenerToMappedElementFunctor
< typename
Container::value_type
>( xListener
));
232 template< typename T
>
233 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
)
239 ::std::for_each( rSequence
.getConstArray(), rSequence
.getConstArray() + rSequence
.getLength(),
240 impl::addListenerFunctor
< T
>( xListener
));
243 template< class InterfaceRef
>
245 const InterfaceRef
& xObject
,
246 const ::com::sun::star::uno::Reference
<
247 ::com::sun::star::util::XModifyListener
> & xListener
)
251 impl::removeListenerFunctor
< InterfaceRef
> aFunctor( xListener
);
256 template< class Container
>
257 void removeListenerFromAllElements(
258 const Container
& rContainer
,
259 const ::com::sun::star::uno::Reference
<
260 ::com::sun::star::util::XModifyListener
> & xListener
)
263 ::std::for_each( rContainer
.begin(), rContainer
.end(),
264 impl::removeListenerFunctor
< typename
Container::value_type
>( xListener
));
267 template< class Container
>
268 void removeListenerFromAllMapElements(
269 const Container
& rContainer
,
270 const ::com::sun::star::uno::Reference
<
271 ::com::sun::star::util::XModifyListener
> & xListener
)
274 ::std::for_each( rContainer
.begin(), rContainer
.end(),
275 impl::removeListenerFromMappedElementFunctor
< typename
Container::value_type
>( xListener
));
278 template< typename T
>
279 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
)
285 ::std::for_each( rSequence
.getConstArray(), rSequence
.getConstArray() + rSequence
.getLength(),
286 impl::removeListenerFunctor
< T
>( xListener
));
289 } // namespace ModifyListenerHelper
292 // CHART2_MODIFYLISTENERHELPER_HXX
295 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */