LibreOffice Module chart2 (master) 1
EventListenerHelper.hxx
Go to the documentation of this file.
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#pragma once
20
21#include <com/sun/star/lang/XComponent.hpp>
22
23#include <algorithm>
24#include <utility>
25
26namespace com::sun::star::lang { class XEventListener; }
27
28namespace chart
29{
30namespace EventListenerHelper
31{
32
33namespace impl
34{
35
36template< class InterfaceRef >
38{
39 explicit addListenerFunctor( css::uno::Reference< css::lang::XEventListener > xListener ) :
40 m_xListener(std::move( xListener ))
41 {}
42
43 void operator() ( const InterfaceRef & xObject )
44 {
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 );
49 }
50private:
51 css::uno::Reference< css::lang::XEventListener > m_xListener;
52};
53
54template< class InterfaceRef >
56{
57 explicit removeListenerFunctor( css::uno::Reference< css::lang::XEventListener > xListener ) :
58 m_xListener(std::move( xListener ))
59 {}
60
61 void operator() ( const InterfaceRef & xObject )
62 {
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 );
67 }
68private:
69 css::uno::Reference< css::lang::XEventListener > m_xListener;
70};
71
72} // namespace impl
73
74template< class InterfaceRef >
76 const InterfaceRef & xObject,
77 const css::uno::Reference< css::lang::XEventListener > & xListener )
78{
79 if( xListener.is())
80 {
81 impl::addListenerFunctor< InterfaceRef > aFunctor( xListener );
82 aFunctor( xObject );
83 }
84}
85
86template< class Container >
88 const Container & rContainer,
89 const css::uno::Reference< css::lang::XEventListener > & xListener )
90{
91 if( xListener.is())
92 std::for_each( rContainer.begin(), rContainer.end(),
94}
95
96template< class InterfaceRef >
98 const InterfaceRef & xObject,
99 const css::uno::Reference< css::lang::XEventListener > & xListener )
100{
101 if( xListener.is())
102 {
104 aFunctor( xObject );
105 }
106}
107
108template< class Container >
110 const Container & rContainer,
111 const css::uno::Reference< css::lang::XEventListener > & xListener )
112{
113 if( xListener.is())
114 std::for_each( rContainer.begin(), rContainer.end(),
116}
117
118} // namespace EventListenerHelper
119} // namespace chart
120
121/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void addListenerToAllElements(const Container &rContainer, const css::uno::Reference< css::lang::XEventListener > &xListener)
void addListener(const InterfaceRef &xObject, const css::uno::Reference< css::lang::XEventListener > &xListener)
void removeListener(const InterfaceRef &xObject, const css::uno::Reference< css::lang::XEventListener > &xListener)
void removeListenerFromAllElements(const Container &rContainer, const css::uno::Reference< css::lang::XEventListener > &xListener)
addListenerFunctor(css::uno::Reference< css::lang::XEventListener > xListener)
css::uno::Reference< css::lang::XEventListener > m_xListener
css::uno::Reference< css::lang::XEventListener > m_xListener
removeListenerFunctor(css::uno::Reference< css::lang::XEventListener > xListener)