Bump version to 4.3-4
[LibreOffice.git] / unotools / source / misc / eventlisteneradapter.cxx
blobf9be7465f3dc3a34eff58105168c7f93bc5faabd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <sal/config.h>
22 #include <vector>
24 #include <unotools/eventlisteneradapter.hxx>
25 #include <osl/diagnose.h>
26 #include <cppuhelper/implbase1.hxx>
28 namespace utl
31 using namespace ::com::sun::star::uno;
32 using namespace ::com::sun::star::lang;
34 //= OEventListenerImpl
36 class OEventListenerImpl : public ::cppu::WeakImplHelper1< XEventListener >
38 protected:
39 OEventListenerAdapter* m_pAdapter;
40 Reference< XEventListener > m_xKeepMeAlive;
41 // imagine an implementation of XComponent which holds it's listeners with a weak reference ...
42 // would be very bad if we don't hold ourself
43 Reference< XComponent > m_xComponent;
45 public:
46 OEventListenerImpl( OEventListenerAdapter* _pAdapter, const Reference< XComponent >& _rxComp );
48 void dispose();
49 const Reference< XComponent >& getComponent() const { return m_xComponent; }
51 protected:
52 virtual void SAL_CALL disposing( const EventObject& _rSource ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
55 OEventListenerImpl::OEventListenerImpl( OEventListenerAdapter* _pAdapter, const Reference< XComponent >& _rxComp )
56 :m_pAdapter(_pAdapter)
58 OSL_ENSURE(m_pAdapter, "OEventListenerImpl::OEventListenerImpl: invalid adapter!");
59 // no checks of _rxComp !!
60 // (OEventListenerAdapter is responsible for this)
62 // just in case addEventListener throws an exception ... don't initialize m_xKeepMeAlive before this
63 // is done
64 Reference< XEventListener > xMeMyselfAndI = this;
65 _rxComp->addEventListener(xMeMyselfAndI);
67 m_xComponent = _rxComp;
68 m_xKeepMeAlive = xMeMyselfAndI;
71 void OEventListenerImpl::dispose()
73 if (m_xComponent.is())
75 m_xComponent->removeEventListener(m_xKeepMeAlive);
76 m_xComponent.clear();
77 m_xKeepMeAlive.clear();
81 void SAL_CALL OEventListenerImpl::disposing( const EventObject& _rSource ) throw (RuntimeException, std::exception)
83 Reference< XEventListener > xDeleteUponLeaving = m_xKeepMeAlive;
84 m_xKeepMeAlive.clear();
85 m_xComponent.clear();
87 m_pAdapter->_disposing(_rSource);
90 //= OEventListenerAdapterImpl
92 struct OEventListenerAdapterImpl
94 public:
95 ::std::vector< void* > aListeners;
98 //= OEventListenerAdapter
100 OEventListenerAdapter::OEventListenerAdapter()
101 :m_pImpl(new OEventListenerAdapterImpl)
105 OEventListenerAdapter::~OEventListenerAdapter()
107 stopAllComponentListening( );
108 delete m_pImpl;
109 m_pImpl = NULL;
112 void OEventListenerAdapter::stopComponentListening( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >& _rxComp )
114 if ( m_pImpl->aListeners.empty() )
115 return;
117 ::std::vector< void* >::iterator dispose = m_pImpl->aListeners.begin();
120 OEventListenerImpl* pListenerImpl = static_cast< OEventListenerImpl* >( *dispose );
121 if ( pListenerImpl->getComponent().get() == _rxComp.get() )
123 pListenerImpl->dispose();
124 pListenerImpl->release();
125 dispose = m_pImpl->aListeners.erase( dispose );
127 else
128 ++dispose;
130 while ( dispose != m_pImpl->aListeners.end() );
133 void OEventListenerAdapter::stopAllComponentListening( )
135 for ( ::std::vector< void* >::const_iterator aDisposeLoop = m_pImpl->aListeners.begin();
136 aDisposeLoop != m_pImpl->aListeners.end();
137 ++aDisposeLoop
140 OEventListenerImpl* pListenerImpl = static_cast< OEventListenerImpl* >(*aDisposeLoop);
141 pListenerImpl->dispose();
142 pListenerImpl->release();
144 m_pImpl->aListeners.clear();
147 void OEventListenerAdapter::startComponentListening( const Reference< XComponent >& _rxComp )
149 if (!_rxComp.is())
151 OSL_FAIL("OEventListenerAdapter::startComponentListening: invalid component!");
152 return;
155 OEventListenerImpl* pListenerImpl = new OEventListenerImpl(this, _rxComp);
156 pListenerImpl->acquire();
157 m_pImpl->aListeners.push_back(pListenerImpl);
160 } // namespace utl
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */