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 <rtl/string.hxx>
21 : #include <osl/diagnose.h>
22 : #include <cppuhelper/component.hxx>
23 : #include <cppuhelper/queryinterface.hxx>
24 : #include <cppuhelper/typeprovider.hxx>
25 : #include "com/sun/star/uno/RuntimeException.hpp"
26 :
27 : using namespace osl;
28 : using namespace com::sun::star;
29 : using namespace com::sun::star::uno;
30 : using namespace com::sun::star::lang;
31 :
32 : using ::rtl::OUString;
33 : using ::rtl::OString;
34 : using ::rtl::OUStringToOString;
35 :
36 : namespace cppu
37 : {
38 :
39 : // ----------------------------------------------------
40 : // class OComponentHelper
41 : // ----------------------------------------------------
42 :
43 17347 : OComponentHelper::OComponentHelper( Mutex & rMutex ) SAL_THROW(())
44 17347 : : rBHelper( rMutex )
45 : {
46 17347 : }
47 15647 : OComponentHelper::~OComponentHelper() SAL_THROW( (RuntimeException) )
48 : {
49 15647 : }
50 :
51 192772 : Any OComponentHelper::queryInterface( Type const & rType ) throw (RuntimeException)
52 : {
53 192772 : return OWeakAggObject::queryInterface( rType );
54 : }
55 154512 : Any OComponentHelper::queryAggregation( Type const & rType ) throw (RuntimeException)
56 : {
57 154512 : if (rType == ::getCppuType( (Reference< lang::XComponent > const *)0 ))
58 : {
59 6548 : void * p = static_cast< lang::XComponent * >( this );
60 6548 : return Any( &p, rType );
61 : }
62 147964 : else if (rType == ::getCppuType( (Reference< lang::XTypeProvider > const *)0 ))
63 : {
64 165 : void * p = static_cast< lang::XTypeProvider * >( this );
65 165 : return Any( &p, rType );
66 : }
67 147799 : return OWeakAggObject::queryAggregation( rType );
68 : }
69 1511393 : void OComponentHelper::acquire() throw ()
70 : {
71 1511393 : OWeakAggObject::acquire();
72 1511393 : }
73 :
74 1471875 : void OComponentHelper::release() throw()
75 : {
76 1471875 : Reference<XInterface > x( xDelegator );
77 1471875 : if (! x.is())
78 : {
79 1422875 : if (osl_atomic_decrement( &m_refCount ) == 0)
80 : {
81 24426 : if (! rBHelper.bDisposed)
82 : {
83 : // *before* again incrementing our ref count, ensure that our weak connection point
84 : // will not create references to us anymore (via XAdapter::queryAdapted)
85 9632 : disposeWeakConnectionPoint();
86 :
87 9632 : Reference<XInterface > xHoldAlive( *this );
88 : // First dispose
89 : try
90 : {
91 9632 : dispose();
92 : }
93 0 : catch (::com::sun::star::uno::RuntimeException & exc)
94 : {
95 : // release should not throw exceptions
96 : #if OSL_DEBUG_LEVEL > 0
97 : OString msg( OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ) );
98 : OSL_FAIL( msg.getStr() );
99 : #else
100 : (void) exc; // avoid warning about unused variable
101 : #endif
102 : }
103 :
104 : // only the alive ref holds the object
105 : OSL_ASSERT( m_refCount == 1 );
106 : // destroy the object if xHoldAlive decrement the refcount to 0
107 1481507 : return;
108 : }
109 : }
110 : // restore the reference count
111 1413243 : osl_atomic_increment( &m_refCount );
112 : }
113 1462243 : OWeakAggObject::release();
114 : }
115 :
116 160 : Sequence< Type > OComponentHelper::getTypes() throw (RuntimeException)
117 : {
118 : static OTypeCollection * s_pTypes = 0;
119 160 : if (! s_pTypes)
120 : {
121 5 : MutexGuard aGuard( Mutex::getGlobalMutex() );
122 5 : if (! s_pTypes)
123 : {
124 : static OTypeCollection s_aTypes(
125 5 : ::getCppuType( (const Reference< lang::XComponent > *)0 ),
126 5 : ::getCppuType( (const Reference< lang::XTypeProvider > *)0 ),
127 5 : ::getCppuType( (const Reference< XAggregation > *)0 ),
128 10 : ::getCppuType( (const Reference< XWeak > *)0 ) );
129 5 : s_pTypes = &s_aTypes;
130 5 : }
131 : }
132 160 : return s_pTypes->getTypes();
133 : }
134 :
135 : // XComponent
136 14232 : void OComponentHelper::disposing()
137 : {
138 14232 : }
139 :
140 : // XComponent
141 14987 : void OComponentHelper::dispose()
142 : throw(::com::sun::star::uno::RuntimeException)
143 : {
144 : // An frequently programming error is to release the last
145 : // reference to this object in the disposing message.
146 : // Make it rubust, hold a self Reference.
147 14987 : Reference<XComponent > xSelf( this );
148 :
149 : // Guard dispose against multible threading
150 : // Remark: It is an error to call dispose more than once
151 14987 : sal_Bool bDoDispose = sal_False;
152 : {
153 14987 : MutexGuard aGuard( rBHelper.rMutex );
154 14987 : if( !rBHelper.bDisposed && !rBHelper.bInDispose )
155 : {
156 : // only one call go into this section
157 14974 : rBHelper.bInDispose = sal_True;
158 14974 : bDoDispose = sal_True;
159 14987 : }
160 : }
161 :
162 : // Do not hold the mutex because we are broadcasting
163 14987 : if( bDoDispose )
164 : {
165 : // Create an event with this as sender
166 : try
167 : {
168 : try
169 : {
170 : Reference<XInterface > xSource(
171 14974 : Reference<XInterface >::query( (XComponent *)this ) );
172 29948 : EventObject aEvt;
173 14974 : aEvt.Source = xSource;
174 : // inform all listeners to release this object
175 : // The listener container are automaticly cleared
176 14974 : rBHelper.aLC.disposeAndClear( aEvt );
177 : // notify subclasses to do their dispose
178 29948 : disposing();
179 : }
180 0 : catch (...)
181 : {
182 0 : MutexGuard aGuard( rBHelper.rMutex );
183 : // bDispose and bInDisposing must be set in this order:
184 0 : rBHelper.bDisposed = sal_True;
185 0 : rBHelper.bInDispose = sal_False;
186 0 : throw;
187 : }
188 14974 : MutexGuard aGuard( rBHelper.rMutex );
189 : // bDispose and bInDisposing must be set in this order:
190 14974 : rBHelper.bDisposed = sal_True;
191 14974 : rBHelper.bInDispose = sal_False;
192 : }
193 0 : catch (RuntimeException &)
194 : {
195 0 : throw;
196 : }
197 0 : catch (Exception & exc)
198 : {
199 : throw RuntimeException(
200 : OUString(
201 0 : "unexpected UNO exception caught: ") +
202 0 : exc.Message, Reference< XInterface >() );
203 : }
204 : }
205 : else
206 : {
207 : // in a multithreaded environment, it can't be avoided
208 : // that dispose is called twice.
209 : // However this condition is traced, because it MAY indicate an error.
210 : OSL_TRACE( "OComponentHelper::dispose() - dispose called twice" );
211 14987 : }
212 14987 : }
213 :
214 : // XComponent
215 1385 : void OComponentHelper::addEventListener(
216 : const Reference<XEventListener > & rxListener )
217 : throw(::com::sun::star::uno::RuntimeException)
218 : {
219 1385 : ClearableMutexGuard aGuard( rBHelper.rMutex );
220 1385 : if (rBHelper.bDisposed || rBHelper.bInDispose)
221 : {
222 0 : aGuard.clear();
223 0 : Reference< XInterface > x( (XComponent *)this, UNO_QUERY );
224 0 : rxListener->disposing( EventObject( x ) );
225 : }
226 : else
227 : {
228 1385 : rBHelper.addListener( ::getCppuType( &rxListener ) , rxListener );
229 1385 : }
230 1385 : }
231 :
232 : // XComponent
233 933 : void OComponentHelper::removeEventListener(
234 : const Reference<XEventListener > & rxListener )
235 : throw(::com::sun::star::uno::RuntimeException)
236 : {
237 933 : rBHelper.removeListener( ::getCppuType( &rxListener ) , rxListener );
238 933 : }
239 :
240 : }
241 :
242 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|