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>
25 namespace com::sun::star::lang
{ class XEventListener
; }
29 namespace EventListenerHelper
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
);
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
);
68 css::uno::Reference
< css::lang::XEventListener
> m_xListener
;
73 template< class InterfaceRef
>
75 const InterfaceRef
& xObject
,
76 const css::uno::Reference
< css::lang::XEventListener
> & xListener
)
80 impl::addListenerFunctor
< InterfaceRef
> aFunctor( xListener
);
85 template< class Container
>
86 void addListenerToAllElements(
87 const Container
& rContainer
,
88 const css::uno::Reference
< css::lang::XEventListener
> & xListener
)
91 std::for_each( rContainer
.begin(), rContainer
.end(),
92 impl::addListenerFunctor
< typename
Container::value_type
>( xListener
));
95 template< class InterfaceRef
>
97 const InterfaceRef
& xObject
,
98 const css::uno::Reference
< css::lang::XEventListener
> & xListener
)
102 impl::removeListenerFunctor
< InterfaceRef
> aFunctor( xListener
);
107 template< class Container
>
108 void removeListenerFromAllElements(
109 const Container
& rContainer
,
110 const css::uno::Reference
< css::lang::XEventListener
> & xListener
)
113 std::for_each( rContainer
.begin(), rContainer
.end(),
114 impl::removeListenerFunctor
< typename
Container::value_type
>( xListener
));
117 } // namespace EventListenerHelper
120 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */