1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: EventListenerHelper.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #ifndef CHART2_EVENTLISTENERHELPER_HXX
31 #define CHART2_EVENTLISTENERHELPER_HXX
33 #include <com/sun/star/lang/XEventListener.hpp>
34 #include <com/sun/star/lang/XComponent.hpp>
43 namespace EventListenerHelper
49 template< class InterfaceRef
>
50 struct addListenerFunctor
: public ::std::unary_function
< InterfaceRef
, void >
52 explicit addListenerFunctor( const ::com::sun::star::uno::Reference
<
53 ::com::sun::star::lang::XEventListener
> & xListener
) :
54 m_xListener( xListener
)
57 void operator() ( const InterfaceRef
& xObject
)
59 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XComponent
>
60 xBroadcaster( xObject
, ::com::sun::star::uno::UNO_QUERY
);
61 if( xBroadcaster
.is() && m_xListener
.is())
62 xBroadcaster
->addEventListener( m_xListener
);
65 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XEventListener
> m_xListener
;
68 template< class InterfaceRef
>
69 struct removeListenerFunctor
: public ::std::unary_function
< InterfaceRef
, void >
71 explicit removeListenerFunctor( const ::com::sun::star::uno::Reference
<
72 ::com::sun::star::lang::XEventListener
> & xListener
) :
73 m_xListener( xListener
)
76 void operator() ( const InterfaceRef
& xObject
)
78 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XComponent
>
79 xBroadcaster( xObject
, ::com::sun::star::uno::UNO_QUERY
);
80 if( xBroadcaster
.is() && m_xListener
.is())
81 xBroadcaster
->removeEventListener( m_xListener
);
84 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XEventListener
> m_xListener
;
87 template< class Pair
>
88 struct addListenerToMappedElementFunctor
: public ::std::unary_function
< Pair
, void >
90 explicit addListenerToMappedElementFunctor( const ::com::sun::star::uno::Reference
<
91 ::com::sun::star::lang::XEventListener
> & xListener
) :
92 m_xListener( xListener
)
95 void operator() ( const Pair
& aPair
)
97 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XComponent
>
98 xBroadcaster( aPair
.second
, ::com::sun::star::uno::UNO_QUERY
);
99 if( xBroadcaster
.is() && m_xListener
.is())
100 xBroadcaster
->addEventListener( m_xListener
);
103 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XEventListener
> m_xListener
;
106 template< class Pair
>
107 struct removeListenerFromMappedElementFunctor
: public ::std::unary_function
< Pair
, void >
109 explicit removeListenerFromMappedElementFunctor( const ::com::sun::star::uno::Reference
<
110 ::com::sun::star::lang::XEventListener
> & xListener
) :
111 m_xListener( xListener
)
114 void operator() ( const Pair
& aPair
)
116 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XComponent
>
117 xBroadcaster( aPair
.second
, ::com::sun::star::uno::UNO_QUERY
);
118 if( xBroadcaster
.is() && m_xListener
.is())
119 xBroadcaster
->removeEventListener( m_xListener
);
122 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XEventListener
> m_xListener
;
127 // --------------------------------------------------------------------------------
129 template< class InterfaceRef
>
131 const InterfaceRef
& xObject
,
132 const ::com::sun::star::uno::Reference
<
133 ::com::sun::star::lang::XEventListener
> & xListener
)
137 impl::addListenerFunctor
< InterfaceRef
> aFunctor( xListener
);
142 template< class Container
>
143 void addListenerToAllElements(
144 const Container
& rContainer
,
145 const ::com::sun::star::uno::Reference
<
146 ::com::sun::star::lang::XEventListener
> & xListener
)
149 ::std::for_each( rContainer
.begin(), rContainer
.end(),
150 impl::addListenerFunctor
< typename
Container::value_type
>( xListener
));
153 template< class Container
>
154 void addListenerToAllMapElements(
155 const Container
& rContainer
,
156 const ::com::sun::star::uno::Reference
<
157 ::com::sun::star::lang::XEventListener
> & xListener
)
160 ::std::for_each( rContainer
.begin(), rContainer
.end(),
161 impl::addListenerToMappedElementFunctor
< typename
Container::value_type
>( xListener
));
164 template< typename T
>
165 void addListenerToAllSequenceElements(
166 const ::com::sun::star::uno::Sequence
< T
> & rSequence
,
167 const ::com::sun::star::uno::Reference
<
168 ::com::sun::star::lang::XEventListener
> & xListener
)
171 ::std::for_each( rSequence
.getConstArray(), rSequence
.getConstArray() + rSequence
.getLength(),
172 impl::addListenerFunctor
< T
>( xListener
));
175 template< class InterfaceRef
>
177 const InterfaceRef
& xObject
,
178 const ::com::sun::star::uno::Reference
<
179 ::com::sun::star::lang::XEventListener
> & xListener
)
183 impl::removeListenerFunctor
< InterfaceRef
> aFunctor( xListener
);
188 template< class Container
>
189 void removeListenerFromAllElements(
190 const Container
& rContainer
,
191 const ::com::sun::star::uno::Reference
<
192 ::com::sun::star::lang::XEventListener
> & xListener
)
195 ::std::for_each( rContainer
.begin(), rContainer
.end(),
196 impl::removeListenerFunctor
< typename
Container::value_type
>( xListener
));
199 template< class Container
>
200 void removeListenerFromAllMapElements(
201 const Container
& rContainer
,
202 const ::com::sun::star::uno::Reference
<
203 ::com::sun::star::lang::XEventListener
> & xListener
)
206 ::std::for_each( rContainer
.begin(), rContainer
.end(),
207 impl::removeListenerFromMappedElementFunctor
< typename
Container::value_type
>( xListener
));
210 template< typename T
>
211 void removeListenerFromAllSequenceElements(
212 const ::com::sun::star::uno::Sequence
< T
> & rSequence
,
213 const ::com::sun::star::uno::Reference
<
214 ::com::sun::star::lang::XEventListener
> & xListener
)
217 ::std::for_each( rSequence
.getConstArray(), rSequence
.getConstArray() + rSequence
.getLength(),
218 impl::removeListenerFunctor
< T
>( xListener
));
221 } // namespace EventListenerHelper
224 // CHART2_EVENTLISTENERHELPER_HXX