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 .
20 #include "EventThread.hxx"
21 #include <comphelper/guarding.hxx>
22 #include <tools/debug.hxx>
23 #include <cppuhelper/queryinterface.hxx>
30 using namespace ::com::sun::star::uno
;
31 using namespace ::com::sun::star::awt
;
32 using namespace ::com::sun::star::lang
;
34 OComponentEventThread::OComponentEventThread( ::cppu::OComponentHelper
* pCompImpl
) :
38 osl_atomic_increment(&m_refCount
);
40 // and add us at the Control
42 Reference
<XEventListener
> xEvtLstnr
= static_cast<XEventListener
*>(this);
43 m_xComp
->addEventListener( xEvtLstnr
);
46 osl_atomic_decrement(&m_refCount
);
49 OComponentEventThread::~OComponentEventThread()
52 DBG_ASSERT( m_aEvents
.empty(),
53 "OComponentEventThread::~OComponentEventThread: Didn't call dispose?" );
55 impl_clearEventQueue();
58 Any SAL_CALL
OComponentEventThread::queryInterface(const Type
& _rType
)
60 Any aReturn
= OWeakObject::queryInterface(_rType
);
62 if (!aReturn
.hasValue())
63 aReturn
= ::cppu::queryInterface(_rType
,
64 static_cast<XEventListener
*>(this)
70 void OComponentEventThread::impl_clearEventQueue()
77 void OComponentEventThread::disposing( const EventObject
& evt
)
79 std::unique_lock
aGuard( m_aMutex
);
81 if( evt
.Source
!= static_cast<XWeak
*>(m_xComp
.get()) )
84 // Remove EventListener
85 Reference
<XEventListener
> xEvtLstnr
= static_cast<XEventListener
*>(this);
86 m_xComp
->removeEventListener( xEvtLstnr
);
89 impl_clearEventQueue();
91 // Free the Control and set pCompImpl to 0,
92 // so that the thread knows, that it should terminate.
95 // Wake up the thread and terminate
100 void OComponentEventThread::addEvent( std::unique_ptr
<EventObject
> _pEvt
)
102 Reference
<XControl
> xTmp
;
103 addEvent( std::move(_pEvt
), xTmp
);
106 void OComponentEventThread::addEvent( std::unique_ptr
<EventObject
> _pEvt
,
107 const Reference
<XControl
>& rControl
,
110 std::unique_lock
aGuard( m_aMutex
);
112 // Put data into the queue
113 m_aEvents
.push_back( std::move( _pEvt
) );
115 Reference
<XWeak
> xWeakControl(rControl
, UNO_QUERY
);
116 Reference
<XAdapter
> xControlAdapter
= xWeakControl
.is() ? xWeakControl
->queryAdapter() : Reference
<XAdapter
>();
117 m_aControls
.push_back( xControlAdapter
);
119 m_aFlags
.push_back( bFlag
);
125 void SAL_CALL
OComponentEventThread::onTerminated()
127 OComponentEventThread_TBASE::onTerminated();
132 void OComponentEventThread::run()
134 osl_setThreadName("frm::OComponentEventThread");
138 // Hold on to ourselves, so that we're not deleted if a dispose is called at some point in time
139 css::uno::Reference
<css::uno::XInterface
> xThis(static_cast<XWeak
*>(this));
143 std::unique_lock
aGuard(m_aMutex
);
145 while( !m_aEvents
.empty() )
147 // Get the Control and hold on to it so that it cannot be deleted during actionPerformed
148 rtl::Reference
<::cppu::OComponentHelper
> xComp
= m_xComp
;
150 ThreadEvents::iterator
firstEvent( m_aEvents
.begin() );
151 std::unique_ptr
<EventObject
> pEvt
= std::move(*firstEvent
);
152 m_aEvents
.erase( firstEvent
);
154 ThreadObjects::iterator
firstControl( m_aControls
.begin() );
155 Reference
<XAdapter
> xControlAdapter
= *firstControl
;
156 m_aControls
.erase( firstControl
);
158 auto firstFlag( m_aFlags
.begin() );
159 bool bFlag
= *firstFlag
;
160 m_aFlags
.erase( firstFlag
);
164 // Because a queryHardRef can throw an Exception, it should not be called when
165 // the mutex is locked.
166 Reference
<XControl
> xControl
;
167 if ( xControlAdapter
.is() )
169 xControlAdapter
->queryAdapted(), css::uno::UNO_QUERY
);
172 processEvent( xComp
.get(), pEvt
.get(), xControl
, bFlag
);
177 // After a Dispose, we do not know the Control anymore.
178 // Thus, we must not wait either.
182 // Reset waiting condition
186 // And wait ... if, in the meantime, an Event came in after all
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */