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 5 : OComponentHelper::OComponentHelper( Mutex & rMutex ) SAL_THROW(())
44 5 : : rBHelper( rMutex )
45 : {
46 5 : }
47 5 : OComponentHelper::~OComponentHelper() SAL_THROW( (RuntimeException) )
48 : {
49 5 : }
50 :
51 10 : Any OComponentHelper::queryInterface( Type const & rType ) throw (RuntimeException, std::exception)
52 : {
53 10 : return OWeakAggObject::queryInterface( rType );
54 : }
55 10 : Any OComponentHelper::queryAggregation( Type const & rType ) throw (RuntimeException, std::exception)
56 : {
57 10 : if (rType == ::getCppuType( (Reference< lang::XComponent > const *)0 ))
58 : {
59 1 : void * p = static_cast< lang::XComponent * >( this );
60 1 : return Any( &p, rType );
61 : }
62 9 : else if (rType == ::getCppuType( (Reference< lang::XTypeProvider > const *)0 ))
63 : {
64 0 : void * p = static_cast< lang::XTypeProvider * >( this );
65 0 : return Any( &p, rType );
66 : }
67 9 : return OWeakAggObject::queryAggregation( rType );
68 : }
69 60 : void OComponentHelper::acquire() throw ()
70 : {
71 60 : OWeakAggObject::acquire();
72 60 : }
73 :
74 60 : void OComponentHelper::release() throw()
75 : {
76 60 : Reference<XInterface > x( xDelegator );
77 60 : if (! x.is())
78 : {
79 60 : if (osl_atomic_decrement( &m_refCount ) == 0)
80 : {
81 9 : 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 4 : disposeWeakConnectionPoint();
86 :
87 4 : Reference<XInterface > xHoldAlive( *this );
88 : // First dispose
89 : try
90 : {
91 4 : 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 64 : return;
108 : }
109 : }
110 : // restore the reference count
111 56 : osl_atomic_increment( &m_refCount );
112 : }
113 56 : OWeakAggObject::release();
114 : }
115 :
116 0 : Sequence< Type > OComponentHelper::getTypes() throw (RuntimeException, std::exception)
117 : {
118 : static OTypeCollection * s_pTypes = 0;
119 0 : if (! s_pTypes)
120 : {
121 0 : MutexGuard aGuard( Mutex::getGlobalMutex() );
122 0 : if (! s_pTypes)
123 : {
124 : static OTypeCollection s_aTypes(
125 0 : ::getCppuType( (const Reference< lang::XComponent > *)0 ),
126 0 : ::getCppuType( (const Reference< lang::XTypeProvider > *)0 ),
127 0 : ::getCppuType( (const Reference< XAggregation > *)0 ),
128 0 : ::getCppuType( (const Reference< XWeak > *)0 ) );
129 0 : s_pTypes = &s_aTypes;
130 0 : }
131 : }
132 0 : return s_pTypes->getTypes();
133 : }
134 :
135 : // XComponent
136 5 : void OComponentHelper::disposing()
137 : {
138 5 : }
139 :
140 : // XComponent
141 5 : void OComponentHelper::dispose()
142 : throw(::com::sun::star::uno::RuntimeException, std::exception)
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 5 : Reference<XComponent > xSelf( this );
148 :
149 : // Guard dispose against multible threading
150 : // Remark: It is an error to call dispose more than once
151 5 : bool bDoDispose = false;
152 : {
153 5 : MutexGuard aGuard( rBHelper.rMutex );
154 5 : if( !rBHelper.bDisposed && !rBHelper.bInDispose )
155 : {
156 : // only one call go into this section
157 5 : rBHelper.bInDispose = sal_True;
158 5 : bDoDispose = true;
159 5 : }
160 : }
161 :
162 : // Do not hold the mutex because we are broadcasting
163 5 : if( bDoDispose )
164 : {
165 : // Create an event with this as sender
166 : try
167 : {
168 : try
169 : {
170 : Reference<XInterface > xSource(
171 5 : Reference<XInterface >::query( (XComponent *)this ) );
172 10 : EventObject aEvt;
173 5 : aEvt.Source = xSource;
174 : // inform all listeners to release this object
175 : // The listener container are automaticly cleared
176 5 : rBHelper.aLC.disposeAndClear( aEvt );
177 : // notify subclasses to do their dispose
178 10 : 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 5 : MutexGuard aGuard( rBHelper.rMutex );
189 : // bDispose and bInDisposing must be set in this order:
190 5 : rBHelper.bDisposed = sal_True;
191 5 : 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 5 : }
212 5 : }
213 :
214 : // XComponent
215 0 : void OComponentHelper::addEventListener(
216 : const Reference<XEventListener > & rxListener )
217 : throw(::com::sun::star::uno::RuntimeException, std::exception)
218 : {
219 0 : ClearableMutexGuard aGuard( rBHelper.rMutex );
220 0 : 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 0 : rBHelper.addListener( ::getCppuType( &rxListener ) , rxListener );
229 0 : }
230 0 : }
231 :
232 : // XComponent
233 0 : void OComponentHelper::removeEventListener(
234 : const Reference<XEventListener > & rxListener )
235 : throw(::com::sun::star::uno::RuntimeException, std::exception)
236 : {
237 0 : rBHelper.removeListener( ::getCppuType( &rxListener ) , rxListener );
238 0 : }
239 :
240 : }
241 :
242 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|