Bump version to 5.0-43
[LibreOffice.git] / cppuhelper / source / implbase.cxx
blobbe0405c8cf5f1219ee2b06c52d1b9b0c272dd1e6
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 <osl/diagnose.h>
22 #include <rtl/instance.hxx>
23 #include <rtl/string.hxx>
25 #include <com/sun/star/lang/XComponent.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;
32 using rtl::OUString;
33 using rtl::OString;
35 namespace cppu
38 // WeakComponentImplHelperBase
40 WeakComponentImplHelperBase::WeakComponentImplHelperBase( Mutex & rMutex )
41 : rBHelper( rMutex )
45 WeakComponentImplHelperBase::~WeakComponentImplHelperBase()
49 void WeakComponentImplHelperBase::disposing()
53 Any WeakComponentImplHelperBase::queryInterface( Type const & rType )
54 throw (RuntimeException, std::exception)
56 if (rType == cppu::UnoType<lang::XComponent>::get())
58 void * p = static_cast< lang::XComponent * >( this );
59 return Any( &p, rType );
61 return OWeakObject::queryInterface( rType );
64 void WeakComponentImplHelperBase::acquire()
65 throw ()
67 OWeakObject::acquire();
70 void WeakComponentImplHelperBase::release()
71 throw ()
73 if (osl_atomic_decrement( &m_refCount ) == 0) {
74 // ensure no other references are created, via the weak connection point, from now on
75 disposeWeakConnectionPoint();
76 // restore reference count:
77 osl_atomic_increment( &m_refCount );
78 if (! rBHelper.bDisposed) {
79 try {
80 dispose();
82 catch (RuntimeException const& exc) { // don't break throw ()
83 OSL_FAIL(
84 OUStringToOString(
85 exc.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
86 static_cast<void>(exc);
88 OSL_ASSERT( rBHelper.bDisposed );
90 OWeakObject::release();
94 void WeakComponentImplHelperBase::dispose()
95 throw (RuntimeException, std::exception)
97 ClearableMutexGuard aGuard( rBHelper.rMutex );
98 if (!rBHelper.bDisposed && !rBHelper.bInDispose)
100 rBHelper.bInDispose = sal_True;
101 aGuard.clear();
104 // side effect: keeping a reference to this
105 lang::EventObject aEvt( static_cast< OWeakObject * >( this ) );
108 rBHelper.aLC.disposeAndClear( aEvt );
109 disposing();
111 catch (...)
113 MutexGuard aGuard2( rBHelper.rMutex );
114 // bDisposed and bInDispose must be set in this order:
115 rBHelper.bDisposed = sal_True;
116 rBHelper.bInDispose = sal_False;
117 throw;
119 MutexGuard aGuard2( rBHelper.rMutex );
120 // bDisposed and bInDispose must be set in this order:
121 rBHelper.bDisposed = sal_True;
122 rBHelper.bInDispose = sal_False;
124 catch (RuntimeException &)
126 throw;
128 catch (Exception & exc)
130 throw RuntimeException(
131 "unexpected UNO exception caught: " + exc.Message );
136 void WeakComponentImplHelperBase::addEventListener(
137 Reference< lang::XEventListener > const & xListener )
138 throw (RuntimeException, std::exception)
140 ClearableMutexGuard aGuard( rBHelper.rMutex );
141 if (rBHelper.bDisposed || rBHelper.bInDispose)
143 aGuard.clear();
144 lang::EventObject aEvt( static_cast< OWeakObject * >( this ) );
145 xListener->disposing( aEvt );
147 else
149 rBHelper.addListener( cppu::UnoType<decltype(xListener)>::get(), xListener );
153 void WeakComponentImplHelperBase::removeEventListener(
154 Reference< lang::XEventListener > const & xListener )
155 throw (RuntimeException, std::exception)
157 rBHelper.removeListener( cppu::UnoType<decltype(xListener)>::get(), xListener );
160 // WeakAggComponentImplHelperBase
162 WeakAggComponentImplHelperBase::WeakAggComponentImplHelperBase( Mutex & rMutex )
163 : rBHelper( rMutex )
167 WeakAggComponentImplHelperBase::~WeakAggComponentImplHelperBase()
171 void WeakAggComponentImplHelperBase::disposing()
175 Any WeakAggComponentImplHelperBase::queryInterface( Type const & rType )
176 throw (RuntimeException, std::exception)
178 return OWeakAggObject::queryInterface( rType );
181 Any WeakAggComponentImplHelperBase::queryAggregation( Type const & rType )
182 throw (RuntimeException, std::exception)
184 if (rType == cppu::UnoType<lang::XComponent>::get())
186 void * p = static_cast< lang::XComponent * >( this );
187 return Any( &p, rType );
189 return OWeakAggObject::queryAggregation( rType );
192 void WeakAggComponentImplHelperBase::acquire()
193 throw ()
195 OWeakAggObject::acquire();
198 void WeakAggComponentImplHelperBase::release()
199 throw ()
201 Reference<XInterface> const xDelegator_(xDelegator);
202 if (xDelegator_.is()) {
203 OWeakAggObject::release();
205 else if (osl_atomic_decrement( &m_refCount ) == 0) {
206 // ensure no other references are created, via the weak connection point, from now on
207 disposeWeakConnectionPoint();
208 // restore reference count:
209 osl_atomic_increment( &m_refCount );
210 if (! rBHelper.bDisposed) {
211 try {
212 dispose();
214 catch (RuntimeException const& exc) { // don't break throw ()
215 OSL_FAIL(
216 OUStringToOString(
217 exc.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
218 static_cast<void>(exc);
220 OSL_ASSERT( rBHelper.bDisposed );
222 OWeakAggObject::release();
226 void WeakAggComponentImplHelperBase::dispose()
227 throw (RuntimeException, std::exception)
229 ClearableMutexGuard aGuard( rBHelper.rMutex );
230 if (!rBHelper.bDisposed && !rBHelper.bInDispose)
232 rBHelper.bInDispose = sal_True;
233 aGuard.clear();
236 // side effect: keeping a reference to this
237 lang::EventObject aEvt( static_cast< OWeakObject * >( this ) );
240 rBHelper.aLC.disposeAndClear( aEvt );
241 disposing();
243 catch (...)
245 MutexGuard aGuard2( rBHelper.rMutex );
246 // bDisposed and bInDispose must be set in this order:
247 rBHelper.bDisposed = sal_True;
248 rBHelper.bInDispose = sal_False;
249 throw;
251 MutexGuard aGuard2( rBHelper.rMutex );
252 // bDisposed and bInDispose must be set in this order:
253 rBHelper.bDisposed = sal_True;
254 rBHelper.bInDispose = sal_False;
256 catch (RuntimeException &)
258 throw;
260 catch (Exception & exc)
262 throw RuntimeException(
263 "unexpected UNO exception caught: " + exc.Message );
268 void WeakAggComponentImplHelperBase::addEventListener(
269 Reference< lang::XEventListener > const & xListener )
270 throw (RuntimeException, std::exception)
272 ClearableMutexGuard aGuard( rBHelper.rMutex );
273 if (rBHelper.bDisposed || rBHelper.bInDispose)
275 aGuard.clear();
276 lang::EventObject aEvt( static_cast< OWeakObject * >( this ) );
277 xListener->disposing( aEvt );
279 else
281 rBHelper.addListener( cppu::UnoType<decltype(xListener)>::get(), xListener );
285 void WeakAggComponentImplHelperBase::removeEventListener(
286 Reference< lang::XEventListener > const & xListener )
287 throw (RuntimeException, std::exception)
289 rBHelper.removeListener( cppu::UnoType<decltype(xListener)>::get(), xListener );
294 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */