nss: upgrade to release 3.73
[LibreOffice.git] / chart2 / source / inc / ModifyListenerHelper.hxx
blobc8b0613086069d2f62800ab3ddf179b755a64bf1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #pragma once
21 #include <com/sun/star/util/XModifyListener.hpp>
22 #include <com/sun/star/util/XModifyBroadcaster.hpp>
23 #include <cppuhelper/compbase.hxx>
25 #include "MutexContainer.hxx"
27 #include <vector>
28 #include <algorithm>
29 #include <utility>
31 namespace com::sun::star::uno { class XWeak; }
32 namespace com::sun::star::uno { template <class interface_type> class WeakReference; }
34 namespace chart::ModifyListenerHelper
37 css::uno::Reference< css::util::XModifyListener > createModifyEventForwarder();
39 /** This helper class serves as forwarder of modify events. It can be used
40 whenever an object has to send modify events after it gets a modify event of
41 one of its children.
43 <p>The listeners are held as WeakReferences if they support XWeak. Thus the
44 life time of the listeners is independent of the broadcaster's lifetime in
45 this case.</p>
47 class ModifyEventForwarder :
48 public MutexContainer,
49 public ::cppu::WeakComponentImplHelper<
50 css::util::XModifyBroadcaster,
51 css::util::XModifyListener >
53 public:
54 ModifyEventForwarder();
56 void AddListener(
57 const css::uno::Reference< css::util::XModifyListener >& aListener );
58 void RemoveListener(
59 const css::uno::Reference< css::util::XModifyListener >& aListener );
61 protected:
62 // ____ XModifyBroadcaster ____
63 virtual void SAL_CALL addModifyListener(
64 const css::uno::Reference< css::util::XModifyListener >& aListener ) override;
65 virtual void SAL_CALL removeModifyListener(
66 const css::uno::Reference< css::util::XModifyListener >& aListener ) override;
68 // ____ XModifyListener ____
69 virtual void SAL_CALL modified(
70 const css::lang::EventObject& aEvent ) override;
72 // ____ XEventListener (base of XModifyListener) ____
73 virtual void SAL_CALL disposing(
74 const css::lang::EventObject& Source ) override;
76 // ____ WeakComponentImplHelperBase ____
77 virtual void SAL_CALL disposing() override;
79 private:
80 /// call disposing() at all listeners and remove all listeners
81 void DisposeAndClear( const css::uno::Reference<
82 css::uno::XWeak > & xSource );
84 // ::osl::Mutex & m_rMutex;
85 ::cppu::OBroadcastHelper m_aModifyListeners;
87 typedef std::vector<
88 std::pair<
89 css::uno::WeakReference< css::util::XModifyListener >,
90 css::uno::Reference< css::util::XModifyListener > > >
91 tListenerMap;
93 tListenerMap m_aListenerMap;
96 namespace impl
99 template< class InterfaceRef >
100 struct addListenerFunctor
102 explicit addListenerFunctor( const css::uno::Reference< css::util::XModifyListener > & xListener ) :
103 m_xListener( xListener )
106 void operator() ( const InterfaceRef & xObject )
108 css::uno::Reference< css::util::XModifyBroadcaster >
109 xBroadcaster( xObject, css::uno::UNO_QUERY );
110 if( xBroadcaster.is() && m_xListener.is())
111 xBroadcaster->addModifyListener( m_xListener );
113 private:
114 css::uno::Reference< css::util::XModifyListener > m_xListener;
117 template< class InterfaceRef >
118 struct removeListenerFunctor
120 explicit removeListenerFunctor( const css::uno::Reference< css::util::XModifyListener > & xListener ) :
121 m_xListener( xListener )
124 void operator() ( const InterfaceRef & xObject )
126 css::uno::Reference< css::util::XModifyBroadcaster >
127 xBroadcaster( xObject, css::uno::UNO_QUERY );
128 if( xBroadcaster.is() && m_xListener.is())
129 xBroadcaster->removeModifyListener( m_xListener );
131 private:
132 css::uno::Reference< css::util::XModifyListener > m_xListener;
135 template< class Pair >
136 struct addListenerToMappedElementFunctor
138 explicit addListenerToMappedElementFunctor( const css::uno::Reference< css::util::XModifyListener > & xListener ) :
139 m_xListener( xListener )
142 void operator() ( const Pair & aPair )
144 css::uno::Reference< css::util::XModifyBroadcaster >
145 xBroadcaster( aPair.second, css::uno::UNO_QUERY );
146 if( xBroadcaster.is() && m_xListener.is())
147 xBroadcaster->addModifyListener( m_xListener );
149 private:
150 css::uno::Reference< css::util::XModifyListener > m_xListener;
153 template< class Pair >
154 struct removeListenerFromMappedElementFunctor
156 explicit removeListenerFromMappedElementFunctor( const css::uno::Reference< css::util::XModifyListener > & xListener ) :
157 m_xListener( xListener )
160 void operator() ( const Pair & aPair )
162 css::uno::Reference< css::util::XModifyBroadcaster >
163 xBroadcaster( aPair.second, css::uno::UNO_QUERY );
164 if( xBroadcaster.is() && m_xListener.is())
165 xBroadcaster->removeModifyListener( m_xListener );
167 private:
168 css::uno::Reference< css::util::XModifyListener > m_xListener;
171 } // namespace impl
173 template< class InterfaceRef >
174 void addListener(
175 const InterfaceRef & xObject,
176 const css::uno::Reference< css::util::XModifyListener > & xListener )
178 if( xListener.is())
180 impl::addListenerFunctor< InterfaceRef > aFunctor( xListener );
181 aFunctor( xObject );
185 template< class Container >
186 void addListenerToAllElements(
187 const Container & rContainer,
188 const css::uno::Reference< css::util::XModifyListener > & xListener )
190 if( xListener.is())
191 std::for_each( rContainer.begin(), rContainer.end(),
192 impl::addListenerFunctor< typename Container::value_type >( xListener ));
195 template< class Container >
196 void addListenerToAllMapElements(
197 const Container & rContainer,
198 const css::uno::Reference< css::util::XModifyListener > & xListener )
200 if( xListener.is())
201 std::for_each( rContainer.begin(), rContainer.end(),
202 impl::addListenerToMappedElementFunctor< typename Container::value_type >( xListener ));
205 template< typename T >
206 void addListenerToAllSequenceElements(
207 const css::uno::Sequence< T > & rSequence,
208 const css::uno::Reference< css::util::XModifyListener > & xListener )
210 if( xListener.is())
211 std::for_each( rSequence.begin(), rSequence.end(),
212 impl::addListenerFunctor< T >( xListener ));
215 template< class InterfaceRef >
216 void removeListener(
217 const InterfaceRef & xObject,
218 const css::uno::Reference< css::util::XModifyListener > & xListener )
220 if( xListener.is())
222 impl::removeListenerFunctor< InterfaceRef > aFunctor( xListener );
223 aFunctor( xObject );
227 template< class Container >
228 void removeListenerFromAllElements(
229 const Container & rContainer,
230 const css::uno::Reference< css::util::XModifyListener > & xListener )
232 if( xListener.is())
233 std::for_each( rContainer.begin(), rContainer.end(),
234 impl::removeListenerFunctor< typename Container::value_type >( xListener ));
237 template< class Container >
238 void removeListenerFromAllMapElements(
239 const Container & rContainer,
240 const css::uno::Reference< css::util::XModifyListener > & xListener )
242 if( xListener.is())
243 std::for_each( rContainer.begin(), rContainer.end(),
244 impl::removeListenerFromMappedElementFunctor< typename Container::value_type >( xListener ));
247 template< typename T >
248 void removeListenerFromAllSequenceElements(
249 const css::uno::Sequence< T > & rSequence,
250 const css::uno::Reference< css::util::XModifyListener > & xListener )
252 if( xListener.is())
253 std::for_each( rSequence.begin(), rSequence.end(),
254 impl::removeListenerFunctor< T >( xListener ));
257 } // namespace chart::ModifyListenerHelper
259 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */