LibreOffice Module comphelper (master) 1
weakeventlistener.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
20#ifndef INCLUDED_COMPHELPER_WEAKEVENTLISTENER_HXX
21#define INCLUDED_COMPHELPER_WEAKEVENTLISTENER_HXX
22
23#include <config_options.h>
28#include <com/sun/star/lang/XEventListener.hpp>
29#include <com/sun/star/lang/XComponent.hpp>
30#include <utility>
31
32namespace com::sun::star::uno { class XWeak; }
33
34namespace comphelper
35{
36
37
38 //= OWeakListenerAdapterBase
39
48 {
49 private:
50 css::uno::WeakReference< css::uno::XInterface >
52 css::uno::Reference< css::uno::XInterface >
54
55 protected:
56 css::uno::Reference< css::uno::XInterface >
57 getListener( ) const
58 {
59 return m_aListener.get();
60 }
61
62 const css::uno::Reference< css::uno::XInterface >&
64 {
65 return m_xBroadcaster;
66 }
67
69 {
70 m_aListener.clear();
71 }
72
73
74 protected:
76 const css::uno::Reference< css::uno::XWeak >& _rxListener,
77 css::uno::Reference< css::uno::XInterface > _xBroadcaster
78 )
79 :m_aListener ( _rxListener )
80 ,m_xBroadcaster (std::move( _xBroadcaster ))
81 {
82 }
83
84 protected:
86 };
87
88
89 //= OWeakListenerAdapter
90
91 template< class BROADCASTER, class LISTENER >
101 :public ::comphelper::WeakComponentImplHelper< LISTENER >
103 {
104 protected:
110 const css::uno::Reference< css::uno::XWeak >& _rxListener,
111 const css::uno::Reference< BROADCASTER >& _rxBroadcaster
112 );
113
114 protected:
115 css::uno::Reference< LISTENER > getListener( ) const
116 {
117 return css::uno::Reference< LISTENER >( OWeakListenerAdapterBase::getListener(), css::uno::UNO_QUERY );
118 }
119
120 // XEventListener overridables
121 virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
122
123 protected:
124 // OComponentHelper overridables
125 // to be overridden, again - the derived class should revoke the listener from the broadcaster
126 virtual void disposing( std::unique_lock<std::mutex>& rGuard ) override = 0;
127 };
128
129
130 //= OWeakEventListenerAdapter
131
132 typedef OWeakListenerAdapter < css::lang::XComponent
133 , css::lang::XEventListener
137 class UNLESS_MERGELIBS(COMPHELPER_DLLPUBLIC) OWeakEventListenerAdapter final : public OWeakEventListenerAdapter_Base
138 {
139 public:
140 OWeakEventListenerAdapter(
141 css::uno::Reference< css::uno::XWeak > const & _rxListener,
142 css::uno::Reference< css::lang::XComponent > const & _rxBroadcaster
143 );
144
145 // nothing to do except an own ctor - the forwarding of the "disposing" is already done
146 // in the base class
147
148 private:
150 virtual void disposing( std::unique_lock<std::mutex>& rGuard ) override;
151 };
152
153
154 //= OWeakListenerAdapter
155
156
157 template< class BROADCASTER, class LISTENER >
159 const css::uno::Reference< css::uno::XWeak >& _rxListener,
160 const css::uno::Reference< BROADCASTER >& _rxBroadcaster
161 )
162 : OWeakListenerAdapterBase( _rxListener, _rxBroadcaster )
163 {
164 }
165
166
167 template< class BROADCASTER, class LISTENER >
168 void SAL_CALL OWeakListenerAdapter< BROADCASTER, LISTENER >::disposing( const css::lang::EventObject& _rSource )
169 {
170 css::uno::Reference< LISTENER > xListener( getListener() );
171 if ( xListener.is() )
172 xListener->disposing( _rSource );
173 }
174
175
176} // namespace comphelper
177
178
179#endif // INCLUDED_COMPHELPER_WEAKEVENTLISTENER_HXX
180
181
182/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
(the base for) an adapter which allows to add as listener to a foreign component, without being held ...
css::uno::WeakReference< css::uno::XInterface > m_aListener
const css::uno::Reference< css::uno::XInterface > & getBroadcaster() const
OWeakListenerAdapterBase(const css::uno::Reference< css::uno::XWeak > &_rxListener, css::uno::Reference< css::uno::XInterface > _xBroadcaster)
css::uno::Reference< css::uno::XInterface > getListener() const
css::uno::Reference< css::uno::XInterface > m_xBroadcaster
yet another base for weak listener adapters, this time with some type safety
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
OWeakListenerAdapter(const css::uno::Reference< css::uno::XWeak > &_rxListener, const css::uno::Reference< BROADCASTER > &_rxBroadcaster)
ctor
virtual void disposing(std::unique_lock< std::mutex > &rGuard) override=0
Called by dispose for subclasses to do dispose() work.
css::uno::Reference< LISTENER > getListener() const
OWeakListenerAdapter< css::lang::XComponent, css::lang::XEventListener > OWeakEventListenerAdapter_Base