LibreOffice Module unotools (master) 1
eventlisteneradapter.cxx
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#include <sal/config.h>
21
22#include <vector>
23
24#include <com/sun/star/lang/XComponent.hpp>
26#include <osl/diagnose.h>
28#include <rtl/ref.hxx>
29
30namespace utl
31{
32
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::lang;
35
36 //= OEventListenerImpl
37
38 class OEventListenerImpl : public ::cppu::WeakImplHelper< XEventListener >
39 {
40 protected:
42 Reference< XEventListener > m_xKeepMeAlive;
43 // imagine an implementation of XComponent which holds it's listeners with a weak reference ...
44 // would be very bad if we don't hold ourself
45 Reference< XComponent > m_xComponent;
46
47 public:
48 OEventListenerImpl( OEventListenerAdapter* _pAdapter, const Reference< XComponent >& _rxComp );
49
50 void dispose();
51 const Reference< XComponent >& getComponent() const { return m_xComponent; }
52
53 protected:
54 virtual void SAL_CALL disposing( const EventObject& _rSource ) override;
55 };
56
57 OEventListenerImpl::OEventListenerImpl( OEventListenerAdapter* _pAdapter, const Reference< XComponent >& _rxComp )
58 :m_pAdapter(_pAdapter)
59 {
60 OSL_ENSURE(m_pAdapter, "OEventListenerImpl::OEventListenerImpl: invalid adapter!");
61 // no checks of _rxComp !!
62 // (OEventListenerAdapter is responsible for this)
63
64 // just in case addEventListener throws an exception ... don't initialize m_xKeepMeAlive before this
65 // is done
66 Reference< XEventListener > xMeMyselfAndI = this;
67 _rxComp->addEventListener(xMeMyselfAndI);
68
69 m_xComponent = _rxComp;
70 m_xKeepMeAlive = xMeMyselfAndI;
71 }
72
74 {
75 if (m_xComponent.is())
76 {
77 if (m_xKeepMeAlive.is())
78 m_xComponent->removeEventListener(m_xKeepMeAlive);
79 m_xComponent.clear();
80 m_xKeepMeAlive.clear();
81 }
82 }
83
84 void SAL_CALL OEventListenerImpl::disposing( const EventObject& _rSource )
85 {
86 Reference< XEventListener > xDeleteUponLeaving = m_xKeepMeAlive;
87 m_xKeepMeAlive.clear();
88
89 m_pAdapter->_disposing(_rSource);
90 }
91
92 //= OEventListenerAdapterImpl
93
95 {
96 public:
97 std::vector< rtl::Reference<OEventListenerImpl> > aListeners;
98 };
99
100 //= OEventListenerAdapter
101
104 {
105 }
106
108 {
110 }
111
112 void OEventListenerAdapter::stopComponentListening( const css::uno::Reference< css::lang::XComponent >& _rxComp )
113 {
114 if ( m_pImpl->aListeners.empty() )
115 return;
116
117 auto it = m_pImpl->aListeners.begin();
118 do
119 {
120 rtl::Reference<OEventListenerImpl>& pListenerImpl = *it;
121 if ((pListenerImpl->getComponent().get() == _rxComp.get()) || (pListenerImpl->getComponent() == _rxComp))
122 {
123 pListenerImpl->dispose();
124 it = m_pImpl->aListeners.erase( it );
125 }
126 else
127 ++it;
128 }
129 while ( it != m_pImpl->aListeners.end() );
130 }
131
133 {
134 for ( const auto & i : m_pImpl->aListeners )
135 {
136 i->dispose();
137 }
138 m_pImpl->aListeners.clear();
139 }
140
141 void OEventListenerAdapter::startComponentListening( const Reference< XComponent >& _rxComp )
142 {
143 if (!_rxComp.is())
144 {
145 OSL_FAIL("OEventListenerAdapter::startComponentListening: invalid component!");
146 return;
147 }
148
149 rtl::Reference<OEventListenerImpl> pListenerImpl = new OEventListenerImpl(this, _rxComp);
150 m_pImpl->aListeners.emplace_back(pListenerImpl);
151 }
152
153} // namespace utl
154
155/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
::std::unique_ptr< XmlIdRegistry_Impl > m_pImpl
base class for non-UNO dispose listeners
void startComponentListening(const css::uno::Reference< css::lang::XComponent > &_rxComp)
std::unique_ptr< OEventListenerAdapterImpl > m_pImpl
virtual void _disposing(const css::lang::EventObject &_rSource)=0
void stopComponentListening(const css::uno::Reference< css::lang::XComponent > &_rxComp)
Reference< XEventListener > m_xKeepMeAlive
const Reference< XComponent > & getComponent() const
Reference< XComponent > m_xComponent
OEventListenerImpl(OEventListenerAdapter *_pAdapter, const Reference< XComponent > &_rxComp)
OEventListenerAdapter * m_pAdapter
virtual void SAL_CALL disposing(const EventObject &_rSource) override
AdapterImpl * m_pAdapter
int i
std::vector< rtl::Reference< OEventListenerImpl > > aListeners