xref: /trunk/main/chart2/source/inc/EventListenerHelper.hxx (revision e32ec1a966a7222d04c4ed2904dd376185cdeba0)
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
44 {
45     typedef InterfaceRef argument_type;
46     typedef void result_type;
addListenerFunctorchart::EventListenerHelper::impl::addListenerFunctor47     explicit addListenerFunctor( const ::com::sun::star::uno::Reference<
48                                      ::com::sun::star::lang::XEventListener > & xListener ) :
49             m_xListener( xListener )
50     {}
51 
operator ()chart::EventListenerHelper::impl::addListenerFunctor52     void operator() ( const InterfaceRef & xObject )
53     {
54         ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >
55               xBroadcaster( xObject, ::com::sun::star::uno::UNO_QUERY );
56         if( xBroadcaster.is() && m_xListener.is())
57             xBroadcaster->addEventListener( m_xListener );
58     }
59 private:
60     ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener > m_xListener;
61 };
62 
63 template< class InterfaceRef >
64 struct removeListenerFunctor
65 {
66     typedef InterfaceRef argument_type;
67     typedef void result_type;
removeListenerFunctorchart::EventListenerHelper::impl::removeListenerFunctor68     explicit removeListenerFunctor( const ::com::sun::star::uno::Reference<
69                                         ::com::sun::star::lang::XEventListener > & xListener ) :
70             m_xListener( xListener )
71     {}
72 
operator ()chart::EventListenerHelper::impl::removeListenerFunctor73     void operator() ( const InterfaceRef & xObject )
74     {
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 );
79     }
80 private:
81     ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener > m_xListener;
82 };
83 
84 template< class Pair >
85 struct addListenerToMappedElementFunctor
86 {
87     typedef Pair argument_type;
88     typedef void result_type;
addListenerToMappedElementFunctorchart::EventListenerHelper::impl::addListenerToMappedElementFunctor89     explicit addListenerToMappedElementFunctor( const ::com::sun::star::uno::Reference<
90                                                     ::com::sun::star::lang::XEventListener > & xListener ) :
91             m_xListener( xListener )
92     {}
93 
operator ()chart::EventListenerHelper::impl::addListenerToMappedElementFunctor94     void operator() ( const Pair & aPair )
95     {
96         ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >
97               xBroadcaster( aPair.second, ::com::sun::star::uno::UNO_QUERY );
98         if( xBroadcaster.is() && m_xListener.is())
99             xBroadcaster->addEventListener( m_xListener );
100     }
101 private:
102     ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener > m_xListener;
103 };
104 
105 template< class Pair >
106 struct removeListenerFromMappedElementFunctor
107 {
108     typedef Pair argument_type;
109     typedef void result_type;
removeListenerFromMappedElementFunctorchart::EventListenerHelper::impl::removeListenerFromMappedElementFunctor110     explicit removeListenerFromMappedElementFunctor( const ::com::sun::star::uno::Reference<
111                                                          ::com::sun::star::lang::XEventListener > & xListener ) :
112             m_xListener( xListener )
113     {}
114 
operator ()chart::EventListenerHelper::impl::removeListenerFromMappedElementFunctor115     void operator() ( const Pair & aPair )
116     {
117         ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >
118               xBroadcaster( aPair.second, ::com::sun::star::uno::UNO_QUERY );
119         if( xBroadcaster.is() && m_xListener.is())
120             xBroadcaster->removeEventListener( m_xListener );
121     }
122 private:
123     ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener > m_xListener;
124 };
125 
126 } //  namespace impl
127 
128 // --------------------------------------------------------------------------------
129 
130 template< class InterfaceRef >
addListener(const InterfaceRef & xObject,const::com::sun::star::uno::Reference<::com::sun::star::lang::XEventListener> & xListener)131 void addListener(
132     const InterfaceRef & xObject,
133     const ::com::sun::star::uno::Reference<
134         ::com::sun::star::lang::XEventListener > & xListener )
135 {
136     if( xListener.is())
137     {
138         impl::addListenerFunctor< InterfaceRef > aFunctor( xListener );
139         aFunctor( xObject );
140     }
141 }
142 
143 template< class Container >
addListenerToAllElements(const Container & rContainer,const::com::sun::star::uno::Reference<::com::sun::star::lang::XEventListener> & xListener)144 void addListenerToAllElements(
145     const Container & rContainer,
146     const ::com::sun::star::uno::Reference<
147         ::com::sun::star::lang::XEventListener > & xListener )
148 {
149     if( xListener.is())
150         ::std::for_each( rContainer.begin(), rContainer.end(),
151                          impl::addListenerFunctor< typename Container::value_type >( xListener ));
152 }
153 
154 template< class Container >
addListenerToAllMapElements(const Container & rContainer,const::com::sun::star::uno::Reference<::com::sun::star::lang::XEventListener> & xListener)155 void addListenerToAllMapElements(
156     const Container & rContainer,
157     const ::com::sun::star::uno::Reference<
158         ::com::sun::star::lang::XEventListener > & xListener )
159 {
160     if( xListener.is())
161         ::std::for_each( rContainer.begin(), rContainer.end(),
162                          impl::addListenerToMappedElementFunctor< typename Container::value_type >( xListener ));
163 }
164 
165 template< typename T >
addListenerToAllSequenceElements(const::com::sun::star::uno::Sequence<T> & rSequence,const::com::sun::star::uno::Reference<::com::sun::star::lang::XEventListener> & xListener)166 void addListenerToAllSequenceElements(
167     const ::com::sun::star::uno::Sequence< T > & rSequence,
168     const ::com::sun::star::uno::Reference<
169         ::com::sun::star::lang::XEventListener > & xListener )
170 {
171     if( xListener.is())
172         ::std::for_each( rSequence.getConstArray(), rSequence.getConstArray() + rSequence.getLength(),
173                          impl::addListenerFunctor< T >( xListener ));
174 }
175 
176 template< class InterfaceRef >
removeListener(const InterfaceRef & xObject,const::com::sun::star::uno::Reference<::com::sun::star::lang::XEventListener> & xListener)177 void removeListener(
178     const InterfaceRef & xObject,
179     const ::com::sun::star::uno::Reference<
180         ::com::sun::star::lang::XEventListener > & xListener )
181 {
182     if( xListener.is())
183     {
184         impl::removeListenerFunctor< InterfaceRef > aFunctor( xListener );
185         aFunctor( xObject );
186     }
187 }
188 
189 template< class Container >
removeListenerFromAllElements(const Container & rContainer,const::com::sun::star::uno::Reference<::com::sun::star::lang::XEventListener> & xListener)190 void removeListenerFromAllElements(
191     const Container & rContainer,
192     const ::com::sun::star::uno::Reference<
193         ::com::sun::star::lang::XEventListener > & xListener )
194 {
195     if( xListener.is())
196         ::std::for_each( rContainer.begin(), rContainer.end(),
197                          impl::removeListenerFunctor< typename Container::value_type >( xListener ));
198 }
199 
200 template< class Container >
removeListenerFromAllMapElements(const Container & rContainer,const::com::sun::star::uno::Reference<::com::sun::star::lang::XEventListener> & xListener)201 void removeListenerFromAllMapElements(
202     const Container & rContainer,
203     const ::com::sun::star::uno::Reference<
204         ::com::sun::star::lang::XEventListener > & xListener )
205 {
206     if( xListener.is())
207         ::std::for_each( rContainer.begin(), rContainer.end(),
208                          impl::removeListenerFromMappedElementFunctor< typename Container::value_type >( xListener ));
209 }
210 
211 template< typename T >
removeListenerFromAllSequenceElements(const::com::sun::star::uno::Sequence<T> & rSequence,const::com::sun::star::uno::Reference<::com::sun::star::lang::XEventListener> & xListener)212 void removeListenerFromAllSequenceElements(
213     const ::com::sun::star::uno::Sequence< T > & rSequence,
214     const ::com::sun::star::uno::Reference<
215         ::com::sun::star::lang::XEventListener > & xListener )
216 {
217     if( xListener.is())
218         ::std::for_each( rSequence.getConstArray(), rSequence.getConstArray() + rSequence.getLength(),
219                          impl::removeListenerFunctor< T >( xListener ));
220 }
221 
222 } //  namespace EventListenerHelper
223 } //  namespace chart
224 
225 // CHART2_EVENTLISTENERHELPER_HXX
226 #endif
227