Avoid potential negative array index access to cached text.
[LibreOffice.git] / chart2 / source / inc / ModifyListenerHelper.hxx
blobee90d407a66f445c1c97af86fbf69661a394780f
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 <comphelper/interfacecontainer4.hxx>
24 #include <comphelper/compbase.hxx>
25 #include <rtl/ref.hxx>
27 #include <mutex>
28 #include <algorithm>
29 #include <utility>
31 namespace chart
34 /** This helper class serves as forwarder of modify events. It can be used
35 whenever an object has to send modify events after it gets a modify event of
36 one of its children.
38 class ModifyEventForwarder final :
39 public ::comphelper::WeakComponentImplHelper<
40 css::util::XModifyBroadcaster,
41 css::util::XModifyListener >
43 public:
44 ModifyEventForwarder();
46 // ____ XModifyBroadcaster ____
47 virtual void SAL_CALL addModifyListener(
48 const css::uno::Reference< css::util::XModifyListener >& aListener ) override;
49 virtual void SAL_CALL removeModifyListener(
50 const css::uno::Reference< css::util::XModifyListener >& aListener ) override;
52 // ____ XModifyListener ____
53 virtual void SAL_CALL modified(
54 const css::lang::EventObject& aEvent ) override;
56 private:
57 // ____ XEventListener (base of XModifyListener) ____
58 virtual void SAL_CALL disposing(
59 const css::lang::EventObject& Source ) override;
61 // ____ WeakComponentImplHelperBase ____
62 virtual void disposing(std::unique_lock<std::mutex>& ) override;
64 comphelper::OInterfaceContainerHelper4<css::util::XModifyListener> m_aModifyListeners;
69 namespace chart::ModifyListenerHelper
72 namespace impl
75 template< class InterfaceRef >
76 struct addListenerFunctor
78 explicit addListenerFunctor( css::uno::Reference< css::util::XModifyListener > xListener ) :
79 m_xListener(std::move( xListener ))
82 void operator() ( const InterfaceRef & xObject )
84 css::uno::Reference< css::util::XModifyBroadcaster >
85 xBroadcaster( xObject, css::uno::UNO_QUERY );
86 if( xBroadcaster.is() && m_xListener.is())
87 xBroadcaster->addModifyListener( m_xListener );
89 private:
90 css::uno::Reference< css::util::XModifyListener > m_xListener;
93 template< class InterfaceRef >
94 struct removeListenerFunctor
96 explicit removeListenerFunctor( css::uno::Reference< css::util::XModifyListener > xListener ) :
97 m_xListener(std::move( xListener ))
100 void operator() ( const InterfaceRef & xObject )
102 css::uno::Reference< css::util::XModifyBroadcaster >
103 xBroadcaster( xObject, css::uno::UNO_QUERY );
104 if( xBroadcaster.is() && m_xListener.is())
105 xBroadcaster->removeModifyListener( m_xListener );
107 private:
108 css::uno::Reference< css::util::XModifyListener > m_xListener;
111 template< class Pair >
112 struct addListenerToMappedElementFunctor
114 explicit addListenerToMappedElementFunctor( css::uno::Reference< css::util::XModifyListener > xListener ) :
115 m_xListener(std::move( xListener ))
118 void operator() ( const Pair & aPair )
120 css::uno::Reference< css::util::XModifyBroadcaster >
121 xBroadcaster( aPair.second, css::uno::UNO_QUERY );
122 if( xBroadcaster.is() && m_xListener.is())
123 xBroadcaster->addModifyListener( m_xListener );
125 private:
126 css::uno::Reference< css::util::XModifyListener > m_xListener;
129 template< class Pair >
130 struct removeListenerFromMappedElementFunctor
132 explicit removeListenerFromMappedElementFunctor( css::uno::Reference< css::util::XModifyListener > xListener ) :
133 m_xListener(std::move( xListener ))
136 void operator() ( const Pair & aPair )
138 css::uno::Reference< css::util::XModifyBroadcaster >
139 xBroadcaster( aPair.second, css::uno::UNO_QUERY );
140 if( xBroadcaster.is() && m_xListener.is())
141 xBroadcaster->removeModifyListener( m_xListener );
143 private:
144 css::uno::Reference< css::util::XModifyListener > m_xListener;
147 } // namespace impl
149 template< class InterfaceRef >
150 void addListener(
151 const InterfaceRef & xObject,
152 const css::uno::Reference< css::util::XModifyListener > & xListener )
154 if( xListener.is())
156 impl::addListenerFunctor< InterfaceRef > aFunctor( xListener );
157 aFunctor( xObject );
160 template< class T >
161 void addListener(
162 const rtl::Reference<T> & xBroadcaster,
163 const css::uno::Reference< css::util::XModifyListener > & xListener )
165 if( xBroadcaster && xListener )
166 xBroadcaster->addModifyListener( xListener );
169 template< class Container >
170 void addListenerToAllElements(
171 const Container & rContainer,
172 const css::uno::Reference< css::util::XModifyListener > & xListener )
174 if( xListener.is())
175 std::for_each( rContainer.begin(), rContainer.end(),
176 impl::addListenerFunctor< typename Container::value_type >( xListener ));
179 template< class T >
180 void addListenerToAllElements(
181 const std::vector<rtl::Reference<T>> & rContainer,
182 const css::uno::Reference< css::util::XModifyListener > & xListener )
184 if( !xListener )
185 return;
186 for (auto const & i : rContainer)
187 i->addModifyListener(xListener);
190 template< class Container >
191 void addListenerToAllMapElements(
192 const Container & rContainer,
193 const css::uno::Reference< css::util::XModifyListener > & xListener )
195 if( xListener.is())
196 std::for_each( rContainer.begin(), rContainer.end(),
197 impl::addListenerToMappedElementFunctor< typename Container::value_type >( xListener ));
200 template< class InterfaceRef >
201 void removeListener(
202 const InterfaceRef & xObject,
203 const css::uno::Reference< css::util::XModifyListener > & xListener )
205 if( xListener.is())
207 impl::removeListenerFunctor< InterfaceRef > aFunctor( xListener );
208 aFunctor( xObject );
212 template< class T >
213 void removeListener(
214 const rtl::Reference<T> & xBroadcaster,
215 const css::uno::Reference< css::util::XModifyListener > & xListener )
217 if( xBroadcaster && xListener )
218 xBroadcaster->removeModifyListener( xListener );
221 template< class Container >
222 void removeListenerFromAllElements(
223 const Container & rContainer,
224 const css::uno::Reference< css::util::XModifyListener > & xListener )
226 if( xListener.is())
227 std::for_each( rContainer.begin(), rContainer.end(),
228 impl::removeListenerFunctor< typename Container::value_type >( xListener ));
231 template< class T >
232 void removeListenerFromAllElements(
233 const std::vector<rtl::Reference<T>> & rContainer,
234 const css::uno::Reference< css::util::XModifyListener > & xListener )
236 if( !xListener )
237 return;
238 for (auto const & i : rContainer)
239 i->removeModifyListener(xListener);
242 template< class Container >
243 void removeListenerFromAllMapElements(
244 const Container & rContainer,
245 const css::uno::Reference< css::util::XModifyListener > & xListener )
247 if( xListener.is())
248 std::for_each( rContainer.begin(), rContainer.end(),
249 impl::removeListenerFromMappedElementFunctor< typename Container::value_type >( xListener ));
252 } // namespace chart::ModifyListenerHelper
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */