use less dynamic_cast when broadcasting SfxHint in sc
[libreoffice.git] / chart2 / source / inc / EventListenerHelper.hxx
blobdcf3256d46b5e71b35630a7d90b842aee003e0be
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 <utility>
25 namespace com::sun::star::lang { class XEventListener; }
27 namespace chart
29 namespace EventListenerHelper
32 namespace impl
35 template< class InterfaceRef >
36 struct addListenerFunctor
38 explicit addListenerFunctor( css::uno::Reference< css::lang::XEventListener > xListener ) :
39 m_xListener(std::move( xListener ))
42 void operator() ( const InterfaceRef & xObject )
44 css::uno::Reference< css::lang::XComponent >
45 xBroadcaster( xObject, css::uno::UNO_QUERY );
46 if( xBroadcaster.is() && m_xListener.is())
47 xBroadcaster->addEventListener( m_xListener );
49 private:
50 css::uno::Reference< css::lang::XEventListener > m_xListener;
53 template< class InterfaceRef >
54 struct removeListenerFunctor
56 explicit removeListenerFunctor( css::uno::Reference< css::lang::XEventListener > xListener ) :
57 m_xListener(std::move( xListener ))
60 void operator() ( const InterfaceRef & xObject )
62 css::uno::Reference< css::lang::XComponent >
63 xBroadcaster( xObject, css::uno::UNO_QUERY );
64 if( xBroadcaster.is() && m_xListener.is())
65 xBroadcaster->removeEventListener( m_xListener );
67 private:
68 css::uno::Reference< css::lang::XEventListener > m_xListener;
71 } // namespace impl
73 template< class InterfaceRef >
74 void addListener(
75 const InterfaceRef & xObject,
76 const css::uno::Reference< css::lang::XEventListener > & xListener )
78 if( xListener.is())
80 impl::addListenerFunctor< InterfaceRef > aFunctor( xListener );
81 aFunctor( xObject );
85 template< class Container >
86 void addListenerToAllElements(
87 const Container & rContainer,
88 const css::uno::Reference< css::lang::XEventListener > & xListener )
90 if( xListener.is())
91 std::for_each( rContainer.begin(), rContainer.end(),
92 impl::addListenerFunctor< typename Container::value_type >( xListener ));
95 template< class InterfaceRef >
96 void removeListener(
97 const InterfaceRef & xObject,
98 const css::uno::Reference< css::lang::XEventListener > & xListener )
100 if( xListener.is())
102 impl::removeListenerFunctor< InterfaceRef > aFunctor( xListener );
103 aFunctor( xObject );
107 template< class Container >
108 void removeListenerFromAllElements(
109 const Container & rContainer,
110 const css::uno::Reference< css::lang::XEventListener > & xListener )
112 if( xListener.is())
113 std::for_each( rContainer.begin(), rContainer.end(),
114 impl::removeListenerFunctor< typename Container::value_type >( xListener ));
117 } // namespace EventListenerHelper
118 } // namespace chart
120 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */