Line data Source code
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 <cppuhelper/compbase_ex.hxx>
21 : #include <osl/diagnose.h>
22 : #include <rtl/instance.hxx>
23 : #include <rtl/string.hxx>
24 :
25 : #include <com/sun/star/lang/XComponent.hpp>
26 : #include <com/sun/star/uno/RuntimeException.hpp>
27 :
28 : using namespace ::osl;
29 : using namespace ::com::sun::star;
30 : using namespace ::com::sun::star::uno;
31 :
32 : using rtl::OUString;
33 : using rtl::OString;
34 :
35 : namespace cppu
36 : {
37 :
38 : // WeakComponentImplHelperBase
39 :
40 1177535 : WeakComponentImplHelperBase::WeakComponentImplHelperBase( Mutex & rMutex )
41 1177535 : : rBHelper( rMutex )
42 : {
43 1177535 : }
44 :
45 1161084 : WeakComponentImplHelperBase::~WeakComponentImplHelperBase()
46 : {
47 1161084 : }
48 :
49 798409 : void WeakComponentImplHelperBase::disposing()
50 : {
51 798409 : }
52 :
53 789586 : Any WeakComponentImplHelperBase::queryInterface( Type const & rType )
54 : throw (RuntimeException, std::exception)
55 : {
56 789586 : if (rType == ::getCppuType( (Reference< lang::XComponent > const *)0 ))
57 : {
58 83617 : void * p = static_cast< lang::XComponent * >( this );
59 83617 : return Any( &p, rType );
60 : }
61 705969 : return OWeakObject::queryInterface( rType );
62 : }
63 :
64 45078484 : void WeakComponentImplHelperBase::acquire()
65 : throw ()
66 : {
67 45078484 : OWeakObject::acquire();
68 45078493 : }
69 :
70 45049141 : void WeakComponentImplHelperBase::release()
71 : throw ()
72 : {
73 45049141 : if (osl_atomic_decrement( &m_refCount ) == 0) {
74 : // ensure no other references are created, via the weak connection point, from now on
75 1159928 : disposeWeakConnectionPoint();
76 : // restore reference count:
77 1159928 : osl_atomic_increment( &m_refCount );
78 1159928 : if (! rBHelper.bDisposed) {
79 : try {
80 1056234 : dispose();
81 : }
82 0 : 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);
87 : }
88 : OSL_ASSERT( rBHelper.bDisposed );
89 : }
90 1159928 : OWeakObject::release();
91 : }
92 45049141 : }
93 :
94 1162241 : void WeakComponentImplHelperBase::dispose()
95 : throw (RuntimeException, std::exception)
96 : {
97 1162241 : ClearableMutexGuard aGuard( rBHelper.rMutex );
98 1162241 : if (!rBHelper.bDisposed && !rBHelper.bInDispose)
99 : {
100 1161315 : rBHelper.bInDispose = sal_True;
101 1161315 : aGuard.clear();
102 : try
103 : {
104 : // side effect: keeping a reference to this
105 1161315 : lang::EventObject aEvt( static_cast< OWeakObject * >( this ) );
106 : try
107 : {
108 1161315 : rBHelper.aLC.disposeAndClear( aEvt );
109 1161315 : disposing();
110 : }
111 4 : catch (...)
112 : {
113 4 : MutexGuard aGuard2( rBHelper.rMutex );
114 : // bDisposed and bInDispose must be set in this order:
115 4 : rBHelper.bDisposed = sal_True;
116 4 : rBHelper.bInDispose = sal_False;
117 8 : throw;
118 : }
119 2322622 : MutexGuard aGuard2( rBHelper.rMutex );
120 : // bDisposed and bInDispose must be set in this order:
121 1161311 : rBHelper.bDisposed = sal_True;
122 2322626 : rBHelper.bInDispose = sal_False;
123 : }
124 8 : catch (RuntimeException &)
125 : {
126 4 : throw;
127 : }
128 0 : catch (Exception & exc)
129 : {
130 : throw RuntimeException(
131 0 : OUString("unexpected UNO exception caught: ") +
132 0 : exc.Message );
133 : }
134 1162241 : }
135 1162237 : }
136 :
137 4764 : void WeakComponentImplHelperBase::addEventListener(
138 : Reference< lang::XEventListener > const & xListener )
139 : throw (RuntimeException, std::exception)
140 : {
141 4764 : ClearableMutexGuard aGuard( rBHelper.rMutex );
142 4764 : if (rBHelper.bDisposed || rBHelper.bInDispose)
143 : {
144 0 : aGuard.clear();
145 0 : lang::EventObject aEvt( static_cast< OWeakObject * >( this ) );
146 0 : xListener->disposing( aEvt );
147 : }
148 : else
149 : {
150 4764 : rBHelper.addListener( ::getCppuType( &xListener ), xListener );
151 4764 : }
152 4764 : }
153 :
154 812 : void WeakComponentImplHelperBase::removeEventListener(
155 : Reference< lang::XEventListener > const & xListener )
156 : throw (RuntimeException, std::exception)
157 : {
158 812 : rBHelper.removeListener( ::getCppuType( &xListener ), xListener );
159 812 : }
160 :
161 : // WeakAggComponentImplHelperBase
162 :
163 6849 : WeakAggComponentImplHelperBase::WeakAggComponentImplHelperBase( Mutex & rMutex )
164 6849 : : rBHelper( rMutex )
165 : {
166 6849 : }
167 :
168 6311 : WeakAggComponentImplHelperBase::~WeakAggComponentImplHelperBase()
169 : {
170 6311 : }
171 :
172 449 : void WeakAggComponentImplHelperBase::disposing()
173 : {
174 449 : }
175 :
176 53557 : Any WeakAggComponentImplHelperBase::queryInterface( Type const & rType )
177 : throw (RuntimeException, std::exception)
178 : {
179 53557 : return OWeakAggObject::queryInterface( rType );
180 : }
181 :
182 49355 : Any WeakAggComponentImplHelperBase::queryAggregation( Type const & rType )
183 : throw (RuntimeException, std::exception)
184 : {
185 49355 : if (rType == ::getCppuType( (Reference< lang::XComponent > const *)0 ))
186 : {
187 7986 : void * p = static_cast< lang::XComponent * >( this );
188 7986 : return Any( &p, rType );
189 : }
190 41369 : return OWeakAggObject::queryAggregation( rType );
191 : }
192 :
193 687954 : void WeakAggComponentImplHelperBase::acquire()
194 : throw ()
195 : {
196 687954 : OWeakAggObject::acquire();
197 687954 : }
198 :
199 683872 : void WeakAggComponentImplHelperBase::release()
200 : throw ()
201 : {
202 683872 : Reference<XInterface> const xDelegator_(xDelegator);
203 683872 : if (xDelegator_.is()) {
204 5556 : OWeakAggObject::release();
205 : }
206 678316 : else if (osl_atomic_decrement( &m_refCount ) == 0) {
207 : // ensure no other references are created, via the weak connection point, from now on
208 6311 : disposeWeakConnectionPoint();
209 : // restore reference count:
210 6311 : osl_atomic_increment( &m_refCount );
211 6311 : if (! rBHelper.bDisposed) {
212 : try {
213 223 : dispose();
214 : }
215 0 : catch (RuntimeException const& exc) { // don't break throw ()
216 : OSL_FAIL(
217 : OUStringToOString(
218 : exc.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
219 : static_cast<void>(exc);
220 : }
221 : OSL_ASSERT( rBHelper.bDisposed );
222 : }
223 6311 : OWeakAggObject::release();
224 683872 : }
225 683872 : }
226 :
227 7019 : void WeakAggComponentImplHelperBase::dispose()
228 : throw (RuntimeException, std::exception)
229 : {
230 7019 : ClearableMutexGuard aGuard( rBHelper.rMutex );
231 7019 : if (!rBHelper.bDisposed && !rBHelper.bInDispose)
232 : {
233 6401 : rBHelper.bInDispose = sal_True;
234 6401 : aGuard.clear();
235 : try
236 : {
237 : // side effect: keeping a reference to this
238 6401 : lang::EventObject aEvt( static_cast< OWeakObject * >( this ) );
239 : try
240 : {
241 6401 : rBHelper.aLC.disposeAndClear( aEvt );
242 6401 : disposing();
243 : }
244 0 : catch (...)
245 : {
246 0 : MutexGuard aGuard2( rBHelper.rMutex );
247 : // bDisposed and bInDispose must be set in this order:
248 0 : rBHelper.bDisposed = sal_True;
249 0 : rBHelper.bInDispose = sal_False;
250 0 : throw;
251 : }
252 12802 : MutexGuard aGuard2( rBHelper.rMutex );
253 : // bDisposed and bInDispose must be set in this order:
254 6401 : rBHelper.bDisposed = sal_True;
255 12802 : rBHelper.bInDispose = sal_False;
256 : }
257 0 : catch (RuntimeException &)
258 : {
259 0 : throw;
260 : }
261 0 : catch (Exception & exc)
262 : {
263 : throw RuntimeException(
264 0 : OUString("unexpected UNO exception caught: ") +
265 0 : exc.Message );
266 : }
267 7019 : }
268 7019 : }
269 :
270 1432 : void WeakAggComponentImplHelperBase::addEventListener(
271 : Reference< lang::XEventListener > const & xListener )
272 : throw (RuntimeException, std::exception)
273 : {
274 1432 : ClearableMutexGuard aGuard( rBHelper.rMutex );
275 1432 : if (rBHelper.bDisposed || rBHelper.bInDispose)
276 : {
277 0 : aGuard.clear();
278 0 : lang::EventObject aEvt( static_cast< OWeakObject * >( this ) );
279 0 : xListener->disposing( aEvt );
280 : }
281 : else
282 : {
283 1432 : rBHelper.addListener( ::getCppuType( &xListener ), xListener );
284 1432 : }
285 1432 : }
286 :
287 128 : void WeakAggComponentImplHelperBase::removeEventListener(
288 : Reference< lang::XEventListener > const & xListener )
289 : throw (RuntimeException, std::exception)
290 : {
291 128 : rBHelper.removeListener( ::getCppuType( &xListener ), xListener );
292 128 : }
293 :
294 : }
295 :
296 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|