Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / cppuhelper / source / implbase.cxx
blob554520ac8196e812006dda64d8b997d488fe8367
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 <cppuhelper/compbase_ex.hxx>
21 #include <cppuhelper/exc_hlp.hxx>
22 #include <osl/diagnose.h>
23 #include <sal/log.hxx>
25 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
26 #include <com/sun/star/uno/RuntimeException.hpp>
28 using namespace ::osl;
29 using namespace ::com::sun::star;
30 using namespace ::com::sun::star::uno;
33 namespace cppu
36 // WeakComponentImplHelperBase
38 WeakComponentImplHelperBase::WeakComponentImplHelperBase( Mutex & rMutex )
39 : rBHelper( rMutex )
43 WeakComponentImplHelperBase::~WeakComponentImplHelperBase()
47 void WeakComponentImplHelperBase::disposing()
51 Any WeakComponentImplHelperBase::queryInterface( Type const & rType )
53 if (rType == cppu::UnoType<lang::XComponent>::get())
55 void * p = static_cast< lang::XComponent * >( this );
56 return Any( &p, rType );
58 return OWeakObject::queryInterface( rType );
61 void WeakComponentImplHelperBase::acquire()
62 throw ()
64 OWeakObject::acquire();
67 void WeakComponentImplHelperBase::release()
68 throw ()
70 if (osl_atomic_decrement( &m_refCount ) == 0) {
71 // ensure no other references are created, via the weak connection point, from now on
72 disposeWeakConnectionPoint();
73 // restore reference count:
74 osl_atomic_increment( &m_refCount );
75 if (! rBHelper.bDisposed) {
76 try {
77 dispose();
79 catch (RuntimeException const& exc) { // don't break throw ()
80 SAL_WARN( "cppuhelper", exc );
82 OSL_ASSERT( rBHelper.bDisposed );
84 OWeakObject::release();
88 void WeakComponentImplHelperBase::dispose()
90 ClearableMutexGuard aGuard( rBHelper.rMutex );
91 if (!rBHelper.bDisposed && !rBHelper.bInDispose)
93 rBHelper.bInDispose = true;
94 aGuard.clear();
95 try
97 // side effect: keeping a reference to this
98 lang::EventObject aEvt( static_cast< OWeakObject * >( this ) );
99 try
101 rBHelper.aLC.disposeAndClear( aEvt );
102 disposing();
104 catch (...)
106 MutexGuard aGuard2( rBHelper.rMutex );
107 // bDisposed and bInDispose must be set in this order:
108 rBHelper.bDisposed = true;
109 rBHelper.bInDispose = false;
110 throw;
112 MutexGuard aGuard2( rBHelper.rMutex );
113 // bDisposed and bInDispose must be set in this order:
114 rBHelper.bDisposed = true;
115 rBHelper.bInDispose = false;
117 catch (RuntimeException &)
119 throw;
121 catch (Exception & exc)
123 css::uno::Any anyEx = cppu::getCaughtException();
124 throw lang::WrappedTargetRuntimeException(
125 "unexpected UNO exception caught: " + exc.Message,
126 nullptr, anyEx );
131 void WeakComponentImplHelperBase::addEventListener(
132 Reference< lang::XEventListener > const & xListener )
134 ClearableMutexGuard aGuard( rBHelper.rMutex );
135 if (rBHelper.bDisposed || rBHelper.bInDispose)
137 aGuard.clear();
138 lang::EventObject aEvt( static_cast< OWeakObject * >( this ) );
139 xListener->disposing( aEvt );
141 else
143 rBHelper.addListener( cppu::UnoType<decltype(xListener)>::get(), xListener );
147 void WeakComponentImplHelperBase::removeEventListener(
148 Reference< lang::XEventListener > const & xListener )
150 rBHelper.removeListener( cppu::UnoType<decltype(xListener)>::get(), xListener );
153 // WeakAggComponentImplHelperBase
155 WeakAggComponentImplHelperBase::WeakAggComponentImplHelperBase( Mutex & rMutex )
156 : rBHelper( rMutex )
160 WeakAggComponentImplHelperBase::~WeakAggComponentImplHelperBase()
164 void WeakAggComponentImplHelperBase::disposing()
168 Any WeakAggComponentImplHelperBase::queryInterface( Type const & rType )
170 return OWeakAggObject::queryInterface( rType );
173 Any WeakAggComponentImplHelperBase::queryAggregation( Type const & rType )
175 if (rType == cppu::UnoType<lang::XComponent>::get())
177 void * p = static_cast< lang::XComponent * >( this );
178 return Any( &p, rType );
180 return OWeakAggObject::queryAggregation( rType );
183 void WeakAggComponentImplHelperBase::acquire()
184 throw ()
186 OWeakAggObject::acquire();
189 void WeakAggComponentImplHelperBase::release()
190 throw ()
192 Reference<XInterface> const xDelegator_(xDelegator);
193 if (xDelegator_.is()) {
194 OWeakAggObject::release();
196 else if (osl_atomic_decrement( &m_refCount ) == 0) {
197 // ensure no other references are created, via the weak connection point, from now on
198 disposeWeakConnectionPoint();
199 // restore reference count:
200 osl_atomic_increment( &m_refCount );
201 if (! rBHelper.bDisposed) {
202 try {
203 dispose();
205 catch (RuntimeException const& exc) { // don't break throw ()
206 SAL_WARN( "cppuhelper", exc );
208 OSL_ASSERT( rBHelper.bDisposed );
210 OWeakAggObject::release();
214 void WeakAggComponentImplHelperBase::dispose()
216 ClearableMutexGuard aGuard( rBHelper.rMutex );
217 if (!rBHelper.bDisposed && !rBHelper.bInDispose)
219 rBHelper.bInDispose = true;
220 aGuard.clear();
223 // side effect: keeping a reference to this
224 lang::EventObject aEvt( static_cast< OWeakObject * >( this ) );
227 rBHelper.aLC.disposeAndClear( aEvt );
228 disposing();
230 catch (...)
232 MutexGuard aGuard2( rBHelper.rMutex );
233 // bDisposed and bInDispose must be set in this order:
234 rBHelper.bDisposed = true;
235 rBHelper.bInDispose = false;
236 throw;
238 MutexGuard aGuard2( rBHelper.rMutex );
239 // bDisposed and bInDispose must be set in this order:
240 rBHelper.bDisposed = true;
241 rBHelper.bInDispose = false;
243 catch (RuntimeException &)
245 throw;
247 catch (Exception & exc)
249 css::uno::Any anyEx = cppu::getCaughtException();
250 throw lang::WrappedTargetRuntimeException(
251 "unexpected UNO exception caught: " + exc.Message,
252 nullptr, anyEx );
257 void WeakAggComponentImplHelperBase::addEventListener(
258 Reference< lang::XEventListener > const & xListener )
260 ClearableMutexGuard aGuard( rBHelper.rMutex );
261 if (rBHelper.bDisposed || rBHelper.bInDispose)
263 aGuard.clear();
264 lang::EventObject aEvt( static_cast< OWeakObject * >( this ) );
265 xListener->disposing( aEvt );
267 else
269 rBHelper.addListener( cppu::UnoType<decltype(xListener)>::get(), xListener );
273 void WeakAggComponentImplHelperBase::removeEventListener(
274 Reference< lang::XEventListener > const & xListener )
276 // if we have disposed, then we have cleared the list already
277 MutexGuard aGuard( rBHelper.rMutex );
278 if (!rBHelper.bDisposed)
279 rBHelper.removeListener( cppu::UnoType<decltype(xListener)>::get(), xListener );
284 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */