Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : : #ifndef CHART2_EVENTLISTENERHELPER_HXX
20 : : #define CHART2_EVENTLISTENERHELPER_HXX
21 : :
22 : : #include <com/sun/star/lang/XEventListener.hpp>
23 : : #include <com/sun/star/lang/XComponent.hpp>
24 : :
25 : : #include <list>
26 : : #include <algorithm>
27 : : #include <functional>
28 : : #include <utility>
29 : :
30 : : namespace chart
31 : : {
32 : : namespace EventListenerHelper
33 : : {
34 : :
35 : : namespace impl
36 : : {
37 : :
38 : : template< class InterfaceRef >
39 : 1873 : struct addListenerFunctor : public ::std::unary_function< InterfaceRef, void >
40 : : {
41 : 697 : explicit addListenerFunctor( const ::com::sun::star::uno::Reference<
42 : : ::com::sun::star::lang::XEventListener > & xListener ) :
43 : 697 : m_xListener( xListener )
44 : 697 : {}
45 : :
46 : 729 : void operator() ( const InterfaceRef & xObject )
47 : : {
48 : : ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >
49 [ + - ]: 729 : xBroadcaster( xObject, ::com::sun::star::uno::UNO_QUERY );
50 [ - + ][ # # ]: 729 : if( xBroadcaster.is() && m_xListener.is())
[ - + ]
51 [ # # ][ # # ]: 729 : xBroadcaster->addEventListener( m_xListener );
52 : 729 : }
53 : : private:
54 : : ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener > m_xListener;
55 : : };
56 : :
57 : : template< class InterfaceRef >
58 : 1692 : struct removeListenerFunctor : public ::std::unary_function< InterfaceRef, void >
59 : : {
60 : 586 : explicit removeListenerFunctor( const ::com::sun::star::uno::Reference<
61 : : ::com::sun::star::lang::XEventListener > & xListener ) :
62 : 586 : m_xListener( xListener )
63 : 586 : {}
64 : :
65 : 331 : void operator() ( const InterfaceRef & xObject )
66 : : {
67 : : ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >
68 [ + - ]: 331 : xBroadcaster( xObject, ::com::sun::star::uno::UNO_QUERY );
69 [ - + ][ # # ]: 331 : if( xBroadcaster.is() && m_xListener.is())
[ - + ]
70 [ # # ][ # # ]: 331 : xBroadcaster->removeEventListener( m_xListener );
71 : 331 : }
72 : : private:
73 : : ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener > m_xListener;
74 : : };
75 : :
76 : : template< class Pair >
77 : : struct addListenerToMappedElementFunctor : public ::std::unary_function< Pair, void >
78 : : {
79 : : explicit addListenerToMappedElementFunctor( const ::com::sun::star::uno::Reference<
80 : : ::com::sun::star::lang::XEventListener > & xListener ) :
81 : : m_xListener( xListener )
82 : : {}
83 : :
84 : : void operator() ( const Pair & aPair )
85 : : {
86 : : ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >
87 : : xBroadcaster( aPair.second, ::com::sun::star::uno::UNO_QUERY );
88 : : if( xBroadcaster.is() && m_xListener.is())
89 : : xBroadcaster->addEventListener( m_xListener );
90 : : }
91 : : private:
92 : : ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener > m_xListener;
93 : : };
94 : :
95 : : template< class Pair >
96 : : struct removeListenerFromMappedElementFunctor : public ::std::unary_function< Pair, void >
97 : : {
98 : : explicit removeListenerFromMappedElementFunctor( const ::com::sun::star::uno::Reference<
99 : : ::com::sun::star::lang::XEventListener > & xListener ) :
100 : : m_xListener( xListener )
101 : : {}
102 : :
103 : : void operator() ( const Pair & aPair )
104 : : {
105 : : ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >
106 : : xBroadcaster( aPair.second, ::com::sun::star::uno::UNO_QUERY );
107 : : if( xBroadcaster.is() && m_xListener.is())
108 : : xBroadcaster->removeEventListener( m_xListener );
109 : : }
110 : : private:
111 : : ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener > m_xListener;
112 : : };
113 : :
114 : : } // namespace impl
115 : :
116 : : // --------------------------------------------------------------------------------
117 : :
118 : : template< class InterfaceRef >
119 : 109 : void addListener(
120 : : const InterfaceRef & xObject,
121 : : const ::com::sun::star::uno::Reference<
122 : : ::com::sun::star::lang::XEventListener > & xListener )
123 : : {
124 [ + - ]: 109 : if( xListener.is())
125 : : {
126 [ + - ]: 109 : impl::addListenerFunctor< InterfaceRef > aFunctor( xListener );
127 [ + - ]: 109 : aFunctor( xObject );
128 : : }
129 : 109 : }
130 : :
131 : : template< class Container >
132 : 588 : void addListenerToAllElements(
133 : : const Container & rContainer,
134 : : const ::com::sun::star::uno::Reference<
135 : : ::com::sun::star::lang::XEventListener > & xListener )
136 : : {
137 [ + - ]: 588 : if( xListener.is())
138 [ + - ]: 588 : ::std::for_each( rContainer.begin(), rContainer.end(),
139 : : impl::addListenerFunctor< typename Container::value_type >( xListener ));
140 : 588 : }
141 : :
142 : : template< class Container >
143 : : void addListenerToAllMapElements(
144 : : const Container & rContainer,
145 : : const ::com::sun::star::uno::Reference<
146 : : ::com::sun::star::lang::XEventListener > & xListener )
147 : : {
148 : : if( xListener.is())
149 : : ::std::for_each( rContainer.begin(), rContainer.end(),
150 : : impl::addListenerToMappedElementFunctor< typename Container::value_type >( xListener ));
151 : : }
152 : :
153 : : template< typename T >
154 : : void addListenerToAllSequenceElements(
155 : : const ::com::sun::star::uno::Sequence< T > & rSequence,
156 : : const ::com::sun::star::uno::Reference<
157 : : ::com::sun::star::lang::XEventListener > & xListener )
158 : : {
159 : : if( xListener.is())
160 : : ::std::for_each( rSequence.getConstArray(), rSequence.getConstArray() + rSequence.getLength(),
161 : : impl::addListenerFunctor< T >( xListener ));
162 : : }
163 : :
164 : : template< class InterfaceRef >
165 : 33 : void removeListener(
166 : : const InterfaceRef & xObject,
167 : : const ::com::sun::star::uno::Reference<
168 : : ::com::sun::star::lang::XEventListener > & xListener )
169 : : {
170 [ + - ]: 33 : if( xListener.is())
171 : : {
172 [ + - ]: 33 : impl::removeListenerFunctor< InterfaceRef > aFunctor( xListener );
173 [ + - ]: 33 : aFunctor( xObject );
174 : : }
175 : 33 : }
176 : :
177 : : template< class Container >
178 : 553 : void removeListenerFromAllElements(
179 : : const Container & rContainer,
180 : : const ::com::sun::star::uno::Reference<
181 : : ::com::sun::star::lang::XEventListener > & xListener )
182 : : {
183 [ + - ]: 553 : if( xListener.is())
184 [ + - ]: 553 : ::std::for_each( rContainer.begin(), rContainer.end(),
185 : : impl::removeListenerFunctor< typename Container::value_type >( xListener ));
186 : 553 : }
187 : :
188 : : template< class Container >
189 : : void removeListenerFromAllMapElements(
190 : : const Container & rContainer,
191 : : const ::com::sun::star::uno::Reference<
192 : : ::com::sun::star::lang::XEventListener > & xListener )
193 : : {
194 : : if( xListener.is())
195 : : ::std::for_each( rContainer.begin(), rContainer.end(),
196 : : impl::removeListenerFromMappedElementFunctor< typename Container::value_type >( xListener ));
197 : : }
198 : :
199 : : template< typename T >
200 : : void removeListenerFromAllSequenceElements(
201 : : const ::com::sun::star::uno::Sequence< T > & rSequence,
202 : : const ::com::sun::star::uno::Reference<
203 : : ::com::sun::star::lang::XEventListener > & xListener )
204 : : {
205 : : if( xListener.is())
206 : : ::std::for_each( rSequence.getConstArray(), rSequence.getConstArray() + rSequence.getLength(),
207 : : impl::removeListenerFunctor< T >( xListener ));
208 : : }
209 : :
210 : : } // namespace EventListenerHelper
211 : : } // namespace chart
212 : :
213 : : // CHART2_EVENTLISTENERHELPER_HXX
214 : : #endif
215 : :
216 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|