LibreOffice Module forms (master) 1
EventThread.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 "EventThread.hxx"
22#include <tools/debug.hxx>
24
25#include <memory>
26
27namespace frm
28{
29
30using namespace ::com::sun::star::uno;
31using namespace ::com::sun::star::awt;
32using namespace ::com::sun::star::lang;
33
35 m_xComp( pCompImpl )
36{
37
38 osl_atomic_increment(&m_refCount);
39
40 // and add us at the Control
41 {
42 Reference<XEventListener> xEvtLstnr = static_cast<XEventListener*>(this);
43 m_xComp->addEventListener( xEvtLstnr );
44 }
45
46 osl_atomic_decrement(&m_refCount);
47}
48
50{
51
52 DBG_ASSERT( m_aEvents.empty(),
53 "OComponentEventThread::~OComponentEventThread: Didn't call dispose?" );
54
56}
57
59{
60 Any aReturn = OWeakObject::queryInterface(_rType);
61
62 if (!aReturn.hasValue())
63 aReturn = ::cppu::queryInterface(_rType,
64 static_cast<XEventListener*>(this)
65 );
66
67 return aReturn;
68}
69
71{
72 m_aEvents.clear();
73 m_aControls.clear();
74 m_aFlags.clear();
75}
76
77void OComponentEventThread::disposing( const EventObject& evt )
78{
79 if( evt.Source != static_cast<XWeak*>(m_xComp.get()) )
80 return;
81
82 std::unique_lock aGuard( m_aMutex );
83
84 // Remove EventListener
85 Reference<XEventListener> xEvtLstnr = static_cast<XEventListener*>(this);
86 m_xComp->removeEventListener( xEvtLstnr );
87
88 // Clear EventQueue
90
91 // Free the Control and set pCompImpl to 0,
92 // so that the thread knows, that it should terminate.
93 m_xComp.clear();
94
95 // Wake up the thread and terminate
96 m_aCond.set();
97 terminate();
98}
99
100void OComponentEventThread::addEvent( std::unique_ptr<EventObject> _pEvt )
101{
102 Reference<XControl> xTmp;
103 addEvent( std::move(_pEvt), xTmp );
104}
105
106void OComponentEventThread::addEvent( std::unique_ptr<EventObject> _pEvt,
107 const Reference<XControl>& rControl,
108 bool bFlag )
109{
110 std::unique_lock aGuard( m_aMutex );
111
112 // Put data into the queue
113 m_aEvents.push_back( std::move( _pEvt ) );
114
115 Reference<XWeak> xWeakControl(rControl, UNO_QUERY);
116 Reference<XAdapter> xControlAdapter = xWeakControl.is() ? xWeakControl->queryAdapter() : Reference<XAdapter>();
117 m_aControls.push_back( xControlAdapter );
118
119 m_aFlags.push_back( bFlag );
120
121 // Wake up thread
122 m_aCond.set();
123}
124
126{
127 OComponentEventThread_TBASE::onTerminated();
128
129 release( );
130}
131
133{
134 osl_setThreadName("frm::OComponentEventThread");
135
136 acquire( );
137
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));
140
141 do
142 {
143 std::unique_lock aGuard(m_aMutex);
144
145 while( !m_aEvents.empty() )
146 {
147 // Get the Control and hold on to it so that it cannot be deleted during actionPerformed
149
150 ThreadEvents::iterator firstEvent( m_aEvents.begin() );
151 std::unique_ptr<EventObject> pEvt = std::move(*firstEvent);
152 m_aEvents.erase( firstEvent );
153
154 ThreadObjects::iterator firstControl( m_aControls.begin() );
155 Reference<XAdapter> xControlAdapter = *firstControl;
156 m_aControls.erase( firstControl );
157
158 auto firstFlag( m_aFlags.begin() );
159 bool bFlag = *firstFlag;
160 m_aFlags.erase( firstFlag );
161
162 {
163 aGuard.unlock();
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() )
168 xControl.set(
169 xControlAdapter->queryAdapted(), css::uno::UNO_QUERY);
170
171 if( xComp.is() )
172 processEvent( xComp.get(), pEvt.get(), xControl, bFlag );
173 aGuard.lock();
174 }
175 }
176
177 // After a Dispose, we do not know the Control anymore.
178 // Thus, we must not wait either.
179 if( !m_xComp.is() )
180 return;
181
182 // Reset waiting condition
183 m_aCond.reset();
184 {
185 aGuard.unlock();
186 // And wait ... if, in the meantime, an Event came in after all
187 m_aCond.wait();
188 aGuard.lock();
189 }
190 }
191 while( true );
192}
193
194
195} // namespace frm
196
197
198/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
oslInterlockedCount m_refCount
virtual void SAL_CALL acquire() SAL_NOEXCEPT SAL_OVERRIDE
virtual void SAL_CALL release() SAL_NOEXCEPT SAL_OVERRIDE
virtual void processEvent(::cppu::OComponentHelper *_pCompImpl, const css::lang::EventObject *_pEvt, const css::uno::Reference< css::awt::XControl > &_rControl, bool _bFlag)=0
void addEvent(std::unique_ptr< css::lang::EventObject > _pEvt)
std::vector< bool > m_aFlags
Definition: EventThread.hxx:58
virtual ~OComponentEventThread() override
Definition: EventThread.cxx:49
virtual void SAL_CALL run() override
rtl::Reference<::cppu::OComponentHelper > m_xComp
Definition: EventThread.hxx:60
virtual void SAL_CALL onTerminated() override
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &_rType) override
Definition: EventThread.cxx:58
::osl::Condition m_aCond
Definition: EventThread.hxx:55
virtual void SAL_CALL disposing(const css::lang::EventObject &_rSource) override
Definition: EventThread.cxx:77
OComponentEventThread(::cppu::OComponentHelper *pCompImpl)
Definition: EventThread.cxx:34
#define DBG_ASSERT(sCon, aError)
Type
ListBox is a bit confusing / different from other form components, so here are a few notes:
Definition: BaseListBox.hxx:25