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 INCLUDED_CHART2_SOURCE_INC_MODIFYLISTENERHELPER_HXX
20 #define INCLUDED_CHART2_SOURCE_INC_MODIFYLISTENERHELPER_HXX
22 #include <com/sun/star/util/XModifyListener.hpp>
23 #include <com/sun/star/util/XModifyBroadcaster.hpp>
24 #include <cppuhelper/compbase.hxx>
26 #include "MutexContainer.hxx"
27 #include "charttoolsdllapi.hxx"
33 namespace com
{ namespace sun
{ namespace star
{ namespace uno
{ class XWeak
; } } } }
34 namespace com
{ namespace sun
{ namespace star
{ namespace uno
{ template <class interface_type
> class WeakReference
; } } } }
38 namespace ModifyListenerHelper
41 OOO_DLLPUBLIC_CHARTTOOLS
css::uno::Reference
< css::util::XModifyListener
> createModifyEventForwarder();
43 /** This helper class serves as forwarder of modify events. It can be used
44 whenever an object has to send modify events after it gets a modify event of
47 <p>The listeners are held as WeakReferences if they support XWeak. Thus the
48 life time of the listeners is independent of the broadcaster's lifetime in
51 class ModifyEventForwarder
:
52 public MutexContainer
,
53 public ::cppu::WeakComponentImplHelper
<
54 css::util::XModifyBroadcaster
,
55 css::util::XModifyListener
>
58 ModifyEventForwarder();
61 const css::uno::Reference
< css::util::XModifyListener
>& aListener
);
63 const css::uno::Reference
< css::util::XModifyListener
>& aListener
);
66 // ____ XModifyBroadcaster ____
67 virtual void SAL_CALL
addModifyListener(
68 const css::uno::Reference
< css::util::XModifyListener
>& aListener
) override
;
69 virtual void SAL_CALL
removeModifyListener(
70 const css::uno::Reference
< css::util::XModifyListener
>& aListener
) override
;
72 // ____ XModifyListener ____
73 virtual void SAL_CALL
modified(
74 const css::lang::EventObject
& aEvent
) override
;
76 // ____ XEventListener (base of XModifyListener) ____
77 virtual void SAL_CALL
disposing(
78 const css::lang::EventObject
& Source
) override
;
80 // ____ WeakComponentImplHelperBase ____
81 virtual void SAL_CALL
disposing() override
;
84 /// call disposing() at all listeners and remove all listeners
85 void DisposeAndClear( const css::uno::Reference
<
86 css::uno::XWeak
> & xSource
);
88 // ::osl::Mutex & m_rMutex;
89 ::cppu::OBroadcastHelper m_aModifyListeners
;
93 css::uno::WeakReference
< css::util::XModifyListener
>,
94 css::uno::Reference
< css::util::XModifyListener
> > >
97 tListenerMap m_aListenerMap
;
103 template< class InterfaceRef
>
104 struct addListenerFunctor
106 explicit addListenerFunctor( const css::uno::Reference
< css::util::XModifyListener
> & xListener
) :
107 m_xListener( xListener
)
110 void operator() ( const InterfaceRef
& xObject
)
112 css::uno::Reference
< css::util::XModifyBroadcaster
>
113 xBroadcaster( xObject
, css::uno::UNO_QUERY
);
114 if( xBroadcaster
.is() && m_xListener
.is())
115 xBroadcaster
->addModifyListener( m_xListener
);
118 css::uno::Reference
< css::util::XModifyListener
> m_xListener
;
121 template< class InterfaceRef
>
122 struct removeListenerFunctor
124 explicit removeListenerFunctor( const css::uno::Reference
< css::util::XModifyListener
> & xListener
) :
125 m_xListener( xListener
)
128 void operator() ( const InterfaceRef
& xObject
)
130 css::uno::Reference
< css::util::XModifyBroadcaster
>
131 xBroadcaster( xObject
, css::uno::UNO_QUERY
);
132 if( xBroadcaster
.is() && m_xListener
.is())
133 xBroadcaster
->removeModifyListener( m_xListener
);
136 css::uno::Reference
< css::util::XModifyListener
> m_xListener
;
139 template< class Pair
>
140 struct addListenerToMappedElementFunctor
142 explicit addListenerToMappedElementFunctor( const css::uno::Reference
< css::util::XModifyListener
> & xListener
) :
143 m_xListener( xListener
)
146 void operator() ( const Pair
& aPair
)
148 css::uno::Reference
< css::util::XModifyBroadcaster
>
149 xBroadcaster( aPair
.second
, css::uno::UNO_QUERY
);
150 if( xBroadcaster
.is() && m_xListener
.is())
151 xBroadcaster
->addModifyListener( m_xListener
);
154 css::uno::Reference
< css::util::XModifyListener
> m_xListener
;
157 template< class Pair
>
158 struct removeListenerFromMappedElementFunctor
160 explicit removeListenerFromMappedElementFunctor( const css::uno::Reference
< css::util::XModifyListener
> & xListener
) :
161 m_xListener( xListener
)
164 void operator() ( const Pair
& aPair
)
166 css::uno::Reference
< css::util::XModifyBroadcaster
>
167 xBroadcaster( aPair
.second
, css::uno::UNO_QUERY
);
168 if( xBroadcaster
.is() && m_xListener
.is())
169 xBroadcaster
->removeModifyListener( m_xListener
);
172 css::uno::Reference
< css::util::XModifyListener
> m_xListener
;
177 template< class InterfaceRef
>
179 const InterfaceRef
& xObject
,
180 const css::uno::Reference
< css::util::XModifyListener
> & xListener
)
184 impl::addListenerFunctor
< InterfaceRef
> aFunctor( xListener
);
189 template< class Container
>
190 void addListenerToAllElements(
191 const Container
& rContainer
,
192 const css::uno::Reference
< css::util::XModifyListener
> & xListener
)
195 std::for_each( rContainer
.begin(), rContainer
.end(),
196 impl::addListenerFunctor
< typename
Container::value_type
>( xListener
));
199 template< class Container
>
200 void addListenerToAllMapElements(
201 const Container
& rContainer
,
202 const css::uno::Reference
< css::util::XModifyListener
> & xListener
)
205 std::for_each( rContainer
.begin(), rContainer
.end(),
206 impl::addListenerToMappedElementFunctor
< typename
Container::value_type
>( xListener
));
209 template< typename T
>
210 void addListenerToAllSequenceElements(
211 const css::uno::Sequence
< T
> & rSequence
,
212 const css::uno::Reference
< css::util::XModifyListener
> & xListener
)
215 std::for_each( rSequence
.begin(), rSequence
.end(),
216 impl::addListenerFunctor
< T
>( xListener
));
219 template< class InterfaceRef
>
221 const InterfaceRef
& xObject
,
222 const css::uno::Reference
< css::util::XModifyListener
> & xListener
)
226 impl::removeListenerFunctor
< InterfaceRef
> aFunctor( xListener
);
231 template< class Container
>
232 void removeListenerFromAllElements(
233 const Container
& rContainer
,
234 const css::uno::Reference
< css::util::XModifyListener
> & xListener
)
237 std::for_each( rContainer
.begin(), rContainer
.end(),
238 impl::removeListenerFunctor
< typename
Container::value_type
>( xListener
));
241 template< class Container
>
242 void removeListenerFromAllMapElements(
243 const Container
& rContainer
,
244 const css::uno::Reference
< css::util::XModifyListener
> & xListener
)
247 std::for_each( rContainer
.begin(), rContainer
.end(),
248 impl::removeListenerFromMappedElementFunctor
< typename
Container::value_type
>( xListener
));
251 template< typename T
>
252 void removeListenerFromAllSequenceElements(
253 const css::uno::Sequence
< T
> & rSequence
,
254 const css::uno::Reference
< css::util::XModifyListener
> & xListener
)
257 std::for_each( rSequence
.begin(), rSequence
.end(),
258 impl::removeListenerFunctor
< T
>( xListener
));
261 } // namespace ModifyListenerHelper
264 // INCLUDED_CHART2_SOURCE_INC_MODIFYLISTENERHELPER_HXX
267 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */