Avoid potential negative array index access to cached text.
[LibreOffice.git] / chart2 / source / inc / EventListenerHelper.hxx
blob19c93c152f55789a52b56057285392a47dc28ba3
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/lang/XComponent.hpp>
23 #include <algorithm>
24 #include <utility>
26 namespace com::sun::star::lang { class XEventListener; }
28 namespace chart
30 namespace EventListenerHelper
33 namespace impl
36 template< class InterfaceRef >
37 struct addListenerFunctor
39 explicit addListenerFunctor( css::uno::Reference< css::lang::XEventListener > xListener ) :
40 m_xListener(std::move( xListener ))
43 void operator() ( const InterfaceRef & xObject )
45 css::uno::Reference< css::lang::XComponent >
46 xBroadcaster( xObject, css::uno::UNO_QUERY );
47 if( xBroadcaster.is() && m_xListener.is())
48 xBroadcaster->addEventListener( m_xListener );
50 private:
51 css::uno::Reference< css::lang::XEventListener > m_xListener;
54 template< class InterfaceRef >
55 struct removeListenerFunctor
57 explicit removeListenerFunctor( css::uno::Reference< css::lang::XEventListener > xListener ) :
58 m_xListener(std::move( xListener ))
61 void operator() ( const InterfaceRef & xObject )
63 css::uno::Reference< css::lang::XComponent >
64 xBroadcaster( xObject, css::uno::UNO_QUERY );
65 if( xBroadcaster.is() && m_xListener.is())
66 xBroadcaster->removeEventListener( m_xListener );
68 private:
69 css::uno::Reference< css::lang::XEventListener > m_xListener;
72 } // namespace impl
74 template< class InterfaceRef >
75 void addListener(
76 const InterfaceRef & xObject,
77 const css::uno::Reference< css::lang::XEventListener > & xListener )
79 if( xListener.is())
81 impl::addListenerFunctor< InterfaceRef > aFunctor( xListener );
82 aFunctor( xObject );
86 template< class Container >
87 void addListenerToAllElements(
88 const Container & rContainer,
89 const css::uno::Reference< css::lang::XEventListener > & xListener )
91 if( xListener.is())
92 std::for_each( rContainer.begin(), rContainer.end(),
93 impl::addListenerFunctor< typename Container::value_type >( xListener ));
96 template< class InterfaceRef >
97 void removeListener(
98 const InterfaceRef & xObject,
99 const css::uno::Reference< css::lang::XEventListener > & xListener )
101 if( xListener.is())
103 impl::removeListenerFunctor< InterfaceRef > aFunctor( xListener );
104 aFunctor( xObject );
108 template< class Container >
109 void removeListenerFromAllElements(
110 const Container & rContainer,
111 const css::uno::Reference< css::lang::XEventListener > & xListener )
113 if( xListener.is())
114 std::for_each( rContainer.begin(), rContainer.end(),
115 impl::removeListenerFunctor< typename Container::value_type >( xListener ));
118 } // namespace EventListenerHelper
119 } // namespace chart
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */