LibreOffice Module cppuhelper (master) 1
implbase.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
22#include <osl/diagnose.h>
23#include <sal/log.hxx>
24
25#include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
26#include <com/sun/star/uno/RuntimeException.hpp>
27
28using namespace ::osl;
29using namespace ::com::sun::star;
30using namespace ::com::sun::star::uno;
31
32
33namespace cppu
34{
35
36// WeakComponentImplHelperBase
37
38WeakComponentImplHelperBase::WeakComponentImplHelperBase( Mutex & rMutex )
39 : rBHelper( rMutex )
40{
41}
42
43WeakComponentImplHelperBase::~WeakComponentImplHelperBase()
44{
45}
46
47void WeakComponentImplHelperBase::disposing()
48{
49}
50
52{
54 {
55 void * p = static_cast< lang::XComponent * >( this );
56 return Any( &p, rType );
57 }
58 return OWeakObject::queryInterface( rType );
59}
60
61void WeakComponentImplHelperBase::acquire()
62 noexcept
63{
64 OWeakObject::acquire();
65}
66
67void WeakComponentImplHelperBase::release()
68 noexcept
69{
70 if (osl_atomic_decrement( &m_refCount ) != 0)
71 return;
72
73 // ensure no other references are created, via the weak connection point, from now on
74 disposeWeakConnectionPoint();
75 // restore reference count:
76 osl_atomic_increment( &m_refCount );
77 if (! rBHelper.bDisposed) {
78 try {
79 dispose();
80 }
81 catch (RuntimeException const& exc) { // don't break throw ()
82 SAL_WARN( "cppuhelper", exc );
83 }
84 OSL_ASSERT( rBHelper.bDisposed );
85 }
86 OWeakObject::release();
87}
88
89void WeakComponentImplHelperBase::dispose()
90{
91 ClearableMutexGuard aGuard( rBHelper.rMutex );
92 if (rBHelper.bDisposed || rBHelper.bInDispose)
93 return;
94
95 rBHelper.bInDispose = true;
96 aGuard.clear();
97 try
98 {
99 // side effect: keeping a reference to this
100 lang::EventObject aEvt( static_cast< OWeakObject * >( this ) );
101 try
102 {
103 rBHelper.aLC.disposeAndClear( aEvt );
104 disposing();
105 }
106 catch (...)
107 {
108 MutexGuard aGuard2( rBHelper.rMutex );
109 // bDisposed and bInDispose must be set in this order:
110 rBHelper.bDisposed = true;
111 rBHelper.bInDispose = false;
112 throw;
113 }
114 MutexGuard aGuard2( rBHelper.rMutex );
115 // bDisposed and bInDispose must be set in this order:
116 rBHelper.bDisposed = true;
117 rBHelper.bInDispose = false;
118 }
119 catch (RuntimeException &)
120 {
121 throw;
122 }
123 catch (Exception & exc)
124 {
125 css::uno::Any anyEx = cppu::getCaughtException();
126 throw lang::WrappedTargetRuntimeException(
127 "unexpected UNO exception caught: " + exc.Message,
128 nullptr, anyEx );
129 }
130}
131
132void WeakComponentImplHelperBase::addEventListener(
133 Reference< lang::XEventListener > const & xListener )
134{
135 ClearableMutexGuard aGuard( rBHelper.rMutex );
136 if (rBHelper.bDisposed || rBHelper.bInDispose)
137 {
138 aGuard.clear();
139 lang::EventObject aEvt( static_cast< OWeakObject * >( this ) );
140 xListener->disposing( aEvt );
141 }
142 else
143 {
144 rBHelper.addListener( cppu::UnoType<decltype(xListener)>::get(), xListener );
145 }
146}
147
148void WeakComponentImplHelperBase::removeEventListener(
149 Reference< lang::XEventListener > const & xListener )
150{
151 rBHelper.removeListener( cppu::UnoType<decltype(xListener)>::get(), xListener );
152}
153
154// WeakAggComponentImplHelperBase
155
156WeakAggComponentImplHelperBase::WeakAggComponentImplHelperBase( Mutex & rMutex )
157 : rBHelper( rMutex )
158{
159}
160
161WeakAggComponentImplHelperBase::~WeakAggComponentImplHelperBase()
162{
163}
164
165void WeakAggComponentImplHelperBase::disposing()
166{
167}
168
170{
171 return OWeakAggObject::queryInterface( rType );
172}
173
174Any WeakAggComponentImplHelperBase::queryAggregation( Type const & rType )
175{
177 {
178 void * p = static_cast< lang::XComponent * >( this );
179 return Any( &p, rType );
180 }
181 return OWeakAggObject::queryAggregation( rType );
182}
183
184void WeakAggComponentImplHelperBase::acquire()
185 noexcept
186{
187 OWeakAggObject::acquire();
188}
189
190void WeakAggComponentImplHelperBase::release()
191 noexcept
192{
193 Reference<XInterface> const xDelegator_(xDelegator);
194 if (xDelegator_.is()) {
195 OWeakAggObject::release();
196 }
197 else if (osl_atomic_decrement( &m_refCount ) == 0) {
198 // ensure no other references are created, via the weak connection point, from now on
199 disposeWeakConnectionPoint();
200 // restore reference count:
201 osl_atomic_increment( &m_refCount );
202 if (! rBHelper.bDisposed) {
203 try {
204 dispose();
205 }
206 catch (RuntimeException const& exc) { // don't break throw ()
207 SAL_WARN( "cppuhelper", exc );
208 }
209 OSL_ASSERT( rBHelper.bDisposed );
210 }
211 OWeakAggObject::release();
212 }
213}
214
215void WeakAggComponentImplHelperBase::dispose()
216{
217 ClearableMutexGuard aGuard( rBHelper.rMutex );
218 if (rBHelper.bDisposed || rBHelper.bInDispose)
219 return;
220
221 rBHelper.bInDispose = true;
222 aGuard.clear();
223 try
224 {
225 // side effect: keeping a reference to this
226 lang::EventObject aEvt( static_cast< OWeakObject * >( this ) );
227 try
228 {
229 rBHelper.aLC.disposeAndClear( aEvt );
230 disposing();
231 }
232 catch (...)
233 {
234 MutexGuard aGuard2( rBHelper.rMutex );
235 // bDisposed and bInDispose must be set in this order:
236 rBHelper.bDisposed = true;
237 rBHelper.bInDispose = false;
238 throw;
239 }
240 MutexGuard aGuard2( rBHelper.rMutex );
241 // bDisposed and bInDispose must be set in this order:
242 rBHelper.bDisposed = true;
243 rBHelper.bInDispose = false;
244 }
245 catch (RuntimeException &)
246 {
247 throw;
248 }
249 catch (Exception & exc)
250 {
251 css::uno::Any anyEx = cppu::getCaughtException();
252 throw lang::WrappedTargetRuntimeException(
253 "unexpected UNO exception caught: " + exc.Message,
254 nullptr, anyEx );
255 }
256}
257
258void WeakAggComponentImplHelperBase::addEventListener(
259 Reference< lang::XEventListener > const & xListener )
260{
261 ClearableMutexGuard aGuard( rBHelper.rMutex );
262 if (rBHelper.bDisposed || rBHelper.bInDispose)
263 {
264 aGuard.clear();
265 lang::EventObject aEvt( static_cast< OWeakObject * >( this ) );
266 xListener->disposing( aEvt );
267 }
268 else
269 {
270 rBHelper.addListener( cppu::UnoType<decltype(xListener)>::get(), xListener );
271 }
272}
273
274void WeakAggComponentImplHelperBase::removeEventListener(
275 Reference< lang::XEventListener > const & xListener )
276{
277 // if we have disposed, then we have cleared the list already
278 MutexGuard aGuard( rBHelper.rMutex );
279 if (!rBHelper.bDisposed)
280 rBHelper.removeListener( cppu::UnoType<decltype(xListener)>::get(), xListener );
281}
282
283}
284
285/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void * p
#define SAL_WARN(area, stream)
Type
css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType, Interface1 *p1)
Compares demanded type to given template argument types.
Any SAL_CALL getCaughtException()
Use this function to get the dynamic type of a caught C++-UNO exception; completes the above function...
void dispose()