1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23 #ifndef CHART2_EVENTLISTENERHELPER_HXX
24 #define CHART2_EVENTLISTENERHELPER_HXX
25
26 #include <com/sun/star/lang/XEventListener.hpp>
27 #include <com/sun/star/lang/XComponent.hpp>
28
29 #include <list>
30 #include <algorithm>
31 #include <functional>
32 #include <utility>
33
34 namespace chart
35 {
36 namespace EventListenerHelper
37 {
38
39 namespace impl
40 {
41
42 template< class InterfaceRef >
43 struct addListenerFunctor : public ::std::unary_function< InterfaceRef, void >
44 {
addListenerFunctorchart::EventListenerHelper::impl::addListenerFunctor45 explicit addListenerFunctor( const ::com::sun::star::uno::Reference<
46 ::com::sun::star::lang::XEventListener > & xListener ) :
47 m_xListener( xListener )
48 {}
49
operator ()chart::EventListenerHelper::impl::addListenerFunctor50 void operator() ( const InterfaceRef & xObject )
51 {
52 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >
53 xBroadcaster( xObject, ::com::sun::star::uno::UNO_QUERY );
54 if( xBroadcaster.is() && m_xListener.is())
55 xBroadcaster->addEventListener( m_xListener );
56 }
57 private:
58 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener > m_xListener;
59 };
60
61 template< class InterfaceRef >
62 struct removeListenerFunctor : public ::std::unary_function< InterfaceRef, void >
63 {
removeListenerFunctorchart::EventListenerHelper::impl::removeListenerFunctor64 explicit removeListenerFunctor( const ::com::sun::star::uno::Reference<
65 ::com::sun::star::lang::XEventListener > & xListener ) :
66 m_xListener( xListener )
67 {}
68
operator ()chart::EventListenerHelper::impl::removeListenerFunctor69 void operator() ( const InterfaceRef & xObject )
70 {
71 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >
72 xBroadcaster( xObject, ::com::sun::star::uno::UNO_QUERY );
73 if( xBroadcaster.is() && m_xListener.is())
74 xBroadcaster->removeEventListener( m_xListener );
75 }
76 private:
77 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener > m_xListener;
78 };
79
80 template< class Pair >
81 struct addListenerToMappedElementFunctor : public ::std::unary_function< Pair, void >
82 {
addListenerToMappedElementFunctorchart::EventListenerHelper::impl::addListenerToMappedElementFunctor83 explicit addListenerToMappedElementFunctor( const ::com::sun::star::uno::Reference<
84 ::com::sun::star::lang::XEventListener > & xListener ) :
85 m_xListener( xListener )
86 {}
87
operator ()chart::EventListenerHelper::impl::addListenerToMappedElementFunctor88 void operator() ( const Pair & aPair )
89 {
90 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >
91 xBroadcaster( aPair.second, ::com::sun::star::uno::UNO_QUERY );
92 if( xBroadcaster.is() && m_xListener.is())
93 xBroadcaster->addEventListener( m_xListener );
94 }
95 private:
96 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener > m_xListener;
97 };
98
99 template< class Pair >
100 struct removeListenerFromMappedElementFunctor : public ::std::unary_function< Pair, void >
101 {
removeListenerFromMappedElementFunctorchart::EventListenerHelper::impl::removeListenerFromMappedElementFunctor102 explicit removeListenerFromMappedElementFunctor( const ::com::sun::star::uno::Reference<
103 ::com::sun::star::lang::XEventListener > & xListener ) :
104 m_xListener( xListener )
105 {}
106
operator ()chart::EventListenerHelper::impl::removeListenerFromMappedElementFunctor107 void operator() ( const Pair & aPair )
108 {
109 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >
110 xBroadcaster( aPair.second, ::com::sun::star::uno::UNO_QUERY );
111 if( xBroadcaster.is() && m_xListener.is())
112 xBroadcaster->removeEventListener( m_xListener );
113 }
114 private:
115 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener > m_xListener;
116 };
117
118 } // namespace impl
119
120 // --------------------------------------------------------------------------------
121
122 template< class InterfaceRef >
addListener(const InterfaceRef & xObject,const::com::sun::star::uno::Reference<::com::sun::star::lang::XEventListener> & xListener)123 void addListener(
124 const InterfaceRef & xObject,
125 const ::com::sun::star::uno::Reference<
126 ::com::sun::star::lang::XEventListener > & xListener )
127 {
128 if( xListener.is())
129 {
130 impl::addListenerFunctor< InterfaceRef > aFunctor( xListener );
131 aFunctor( xObject );
132 }
133 }
134
135 template< class Container >
addListenerToAllElements(const Container & rContainer,const::com::sun::star::uno::Reference<::com::sun::star::lang::XEventListener> & xListener)136 void addListenerToAllElements(
137 const Container & rContainer,
138 const ::com::sun::star::uno::Reference<
139 ::com::sun::star::lang::XEventListener > & xListener )
140 {
141 if( xListener.is())
142 ::std::for_each( rContainer.begin(), rContainer.end(),
143 impl::addListenerFunctor< typename Container::value_type >( xListener ));
144 }
145
146 template< class Container >
addListenerToAllMapElements(const Container & rContainer,const::com::sun::star::uno::Reference<::com::sun::star::lang::XEventListener> & xListener)147 void addListenerToAllMapElements(
148 const Container & rContainer,
149 const ::com::sun::star::uno::Reference<
150 ::com::sun::star::lang::XEventListener > & xListener )
151 {
152 if( xListener.is())
153 ::std::for_each( rContainer.begin(), rContainer.end(),
154 impl::addListenerToMappedElementFunctor< typename Container::value_type >( xListener ));
155 }
156
157 template< typename T >
addListenerToAllSequenceElements(const::com::sun::star::uno::Sequence<T> & rSequence,const::com::sun::star::uno::Reference<::com::sun::star::lang::XEventListener> & xListener)158 void addListenerToAllSequenceElements(
159 const ::com::sun::star::uno::Sequence< T > & rSequence,
160 const ::com::sun::star::uno::Reference<
161 ::com::sun::star::lang::XEventListener > & xListener )
162 {
163 if( xListener.is())
164 ::std::for_each( rSequence.getConstArray(), rSequence.getConstArray() + rSequence.getLength(),
165 impl::addListenerFunctor< T >( xListener ));
166 }
167
168 template< class InterfaceRef >
removeListener(const InterfaceRef & xObject,const::com::sun::star::uno::Reference<::com::sun::star::lang::XEventListener> & xListener)169 void removeListener(
170 const InterfaceRef & xObject,
171 const ::com::sun::star::uno::Reference<
172 ::com::sun::star::lang::XEventListener > & xListener )
173 {
174 if( xListener.is())
175 {
176 impl::removeListenerFunctor< InterfaceRef > aFunctor( xListener );
177 aFunctor( xObject );
178 }
179 }
180
181 template< class Container >
removeListenerFromAllElements(const Container & rContainer,const::com::sun::star::uno::Reference<::com::sun::star::lang::XEventListener> & xListener)182 void removeListenerFromAllElements(
183 const Container & rContainer,
184 const ::com::sun::star::uno::Reference<
185 ::com::sun::star::lang::XEventListener > & xListener )
186 {
187 if( xListener.is())
188 ::std::for_each( rContainer.begin(), rContainer.end(),
189 impl::removeListenerFunctor< typename Container::value_type >( xListener ));
190 }
191
192 template< class Container >
removeListenerFromAllMapElements(const Container & rContainer,const::com::sun::star::uno::Reference<::com::sun::star::lang::XEventListener> & xListener)193 void removeListenerFromAllMapElements(
194 const Container & rContainer,
195 const ::com::sun::star::uno::Reference<
196 ::com::sun::star::lang::XEventListener > & xListener )
197 {
198 if( xListener.is())
199 ::std::for_each( rContainer.begin(), rContainer.end(),
200 impl::removeListenerFromMappedElementFunctor< typename Container::value_type >( xListener ));
201 }
202
203 template< typename T >
removeListenerFromAllSequenceElements(const::com::sun::star::uno::Sequence<T> & rSequence,const::com::sun::star::uno::Reference<::com::sun::star::lang::XEventListener> & xListener)204 void removeListenerFromAllSequenceElements(
205 const ::com::sun::star::uno::Sequence< T > & rSequence,
206 const ::com::sun::star::uno::Reference<
207 ::com::sun::star::lang::XEventListener > & xListener )
208 {
209 if( xListener.is())
210 ::std::for_each( rSequence.getConstArray(), rSequence.getConstArray() + rSequence.getLength(),
211 impl::removeListenerFunctor< T >( xListener ));
212 }
213
214 } // namespace EventListenerHelper
215 } // namespace chart
216
217 // CHART2_EVENTLISTENERHELPER_HXX
218 #endif
219