1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
27 #ifndef CHART2_EVENTLISTENERHELPER_HXX
28 #define CHART2_EVENTLISTENERHELPER_HXX
30 #include <com/sun/star/lang/XEventListener.hpp>
31 #include <com/sun/star/lang/XComponent.hpp>
40 namespace EventListenerHelper
46 template< class InterfaceRef
>
47 struct addListenerFunctor
: public ::std::unary_function
< InterfaceRef
, void >
49 explicit addListenerFunctor( const ::com::sun::star::uno::Reference
<
50 ::com::sun::star::lang::XEventListener
> & xListener
) :
51 m_xListener( xListener
)
54 void operator() ( const InterfaceRef
& xObject
)
56 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XComponent
>
57 xBroadcaster( xObject
, ::com::sun::star::uno::UNO_QUERY
);
58 if( xBroadcaster
.is() && m_xListener
.is())
59 xBroadcaster
->addEventListener( m_xListener
);
62 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XEventListener
> m_xListener
;
65 template< class InterfaceRef
>
66 struct removeListenerFunctor
: public ::std::unary_function
< InterfaceRef
, void >
68 explicit removeListenerFunctor( const ::com::sun::star::uno::Reference
<
69 ::com::sun::star::lang::XEventListener
> & xListener
) :
70 m_xListener( xListener
)
73 void operator() ( const InterfaceRef
& xObject
)
75 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XComponent
>
76 xBroadcaster( xObject
, ::com::sun::star::uno::UNO_QUERY
);
77 if( xBroadcaster
.is() && m_xListener
.is())
78 xBroadcaster
->removeEventListener( m_xListener
);
81 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XEventListener
> m_xListener
;
84 template< class Pair
>
85 struct addListenerToMappedElementFunctor
: public ::std::unary_function
< Pair
, void >
87 explicit addListenerToMappedElementFunctor( const ::com::sun::star::uno::Reference
<
88 ::com::sun::star::lang::XEventListener
> & xListener
) :
89 m_xListener( xListener
)
92 void operator() ( const Pair
& aPair
)
94 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XComponent
>
95 xBroadcaster( aPair
.second
, ::com::sun::star::uno::UNO_QUERY
);
96 if( xBroadcaster
.is() && m_xListener
.is())
97 xBroadcaster
->addEventListener( m_xListener
);
100 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XEventListener
> m_xListener
;
103 template< class Pair
>
104 struct removeListenerFromMappedElementFunctor
: public ::std::unary_function
< Pair
, void >
106 explicit removeListenerFromMappedElementFunctor( const ::com::sun::star::uno::Reference
<
107 ::com::sun::star::lang::XEventListener
> & xListener
) :
108 m_xListener( xListener
)
111 void operator() ( const Pair
& aPair
)
113 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XComponent
>
114 xBroadcaster( aPair
.second
, ::com::sun::star::uno::UNO_QUERY
);
115 if( xBroadcaster
.is() && m_xListener
.is())
116 xBroadcaster
->removeEventListener( m_xListener
);
119 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XEventListener
> m_xListener
;
124 // --------------------------------------------------------------------------------
126 template< class InterfaceRef
>
128 const InterfaceRef
& xObject
,
129 const ::com::sun::star::uno::Reference
<
130 ::com::sun::star::lang::XEventListener
> & xListener
)
134 impl::addListenerFunctor
< InterfaceRef
> aFunctor( xListener
);
139 template< class Container
>
140 void addListenerToAllElements(
141 const Container
& rContainer
,
142 const ::com::sun::star::uno::Reference
<
143 ::com::sun::star::lang::XEventListener
> & xListener
)
146 ::std::for_each( rContainer
.begin(), rContainer
.end(),
147 impl::addListenerFunctor
< typename
Container::value_type
>( xListener
));
150 template< class Container
>
151 void addListenerToAllMapElements(
152 const Container
& rContainer
,
153 const ::com::sun::star::uno::Reference
<
154 ::com::sun::star::lang::XEventListener
> & xListener
)
157 ::std::for_each( rContainer
.begin(), rContainer
.end(),
158 impl::addListenerToMappedElementFunctor
< typename
Container::value_type
>( xListener
));
161 template< typename T
>
162 void addListenerToAllSequenceElements(
163 const ::com::sun::star::uno::Sequence
< T
> & rSequence
,
164 const ::com::sun::star::uno::Reference
<
165 ::com::sun::star::lang::XEventListener
> & xListener
)
168 ::std::for_each( rSequence
.getConstArray(), rSequence
.getConstArray() + rSequence
.getLength(),
169 impl::addListenerFunctor
< T
>( xListener
));
172 template< class InterfaceRef
>
174 const InterfaceRef
& xObject
,
175 const ::com::sun::star::uno::Reference
<
176 ::com::sun::star::lang::XEventListener
> & xListener
)
180 impl::removeListenerFunctor
< InterfaceRef
> aFunctor( xListener
);
185 template< class Container
>
186 void removeListenerFromAllElements(
187 const Container
& rContainer
,
188 const ::com::sun::star::uno::Reference
<
189 ::com::sun::star::lang::XEventListener
> & xListener
)
192 ::std::for_each( rContainer
.begin(), rContainer
.end(),
193 impl::removeListenerFunctor
< typename
Container::value_type
>( xListener
));
196 template< class Container
>
197 void removeListenerFromAllMapElements(
198 const Container
& rContainer
,
199 const ::com::sun::star::uno::Reference
<
200 ::com::sun::star::lang::XEventListener
> & xListener
)
203 ::std::for_each( rContainer
.begin(), rContainer
.end(),
204 impl::removeListenerFromMappedElementFunctor
< typename
Container::value_type
>( xListener
));
207 template< typename T
>
208 void removeListenerFromAllSequenceElements(
209 const ::com::sun::star::uno::Sequence
< T
> & rSequence
,
210 const ::com::sun::star::uno::Reference
<
211 ::com::sun::star::lang::XEventListener
> & xListener
)
214 ::std::for_each( rSequence
.getConstArray(), rSequence
.getConstArray() + rSequence
.getLength(),
215 impl::removeListenerFunctor
< T
>( xListener
));
218 } // namespace EventListenerHelper
221 // CHART2_EVENTLISTENERHELPER_HXX