1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include <com/sun/star/lang/XComponent.hpp>
26 namespace com::sun::star::lang
{ class XEventListener
; }
30 namespace EventListenerHelper
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
);
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
);
69 css::uno::Reference
< css::lang::XEventListener
> m_xListener
;
74 template< class InterfaceRef
>
76 const InterfaceRef
& xObject
,
77 const css::uno::Reference
< css::lang::XEventListener
> & xListener
)
81 impl::addListenerFunctor
< InterfaceRef
> aFunctor( xListener
);
86 template< class Container
>
87 void addListenerToAllElements(
88 const Container
& rContainer
,
89 const css::uno::Reference
< css::lang::XEventListener
> & xListener
)
92 std::for_each( rContainer
.begin(), rContainer
.end(),
93 impl::addListenerFunctor
< typename
Container::value_type
>( xListener
));
96 template< class InterfaceRef
>
98 const InterfaceRef
& xObject
,
99 const css::uno::Reference
< css::lang::XEventListener
> & xListener
)
103 impl::removeListenerFunctor
< InterfaceRef
> aFunctor( xListener
);
108 template< class Container
>
109 void removeListenerFromAllElements(
110 const Container
& rContainer
,
111 const css::uno::Reference
< css::lang::XEventListener
> & xListener
)
114 std::for_each( rContainer
.begin(), rContainer
.end(),
115 impl::removeListenerFunctor
< typename
Container::value_type
>( xListener
));
118 } // namespace EventListenerHelper
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */