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 19578 : OComponentHelper::OComponentHelper( Mutex & rMutex )
44 19578 : : rBHelper( rMutex )
45 : {
46 19578 : }
47 18071 : OComponentHelper::~OComponentHelper()
48 : {
49 18071 : }
50 :
51 232324 : Any OComponentHelper::queryInterface( Type const & rType ) throw (RuntimeException, std::exception)
52 : {
53 232324 : return OWeakAggObject::queryInterface( rType );
54 : }
55 183821 : Any OComponentHelper::queryAggregation( Type const & rType ) throw (RuntimeException, std::exception)
56 : {
57 183821 : if (rType == cppu::UnoType<lang::XComponent>::get())
58 : {
59 3698 : void * p = static_cast< lang::XComponent * >( this );
60 3698 : return Any( &p, rType );
61 : }
62 180123 : else if (rType == cppu::UnoType<lang::XTypeProvider>::get())
63 : {
64 217 : void * p = static_cast< lang::XTypeProvider * >( this );
65 217 : return Any( &p, rType );
66 : }
67 179906 : return OWeakAggObject::queryAggregation( rType );
68 : }
69 1980336 : void OComponentHelper::acquire() throw ()
70 : {
71 1980336 : OWeakAggObject::acquire();
72 1980337 : }
73 :
74 1845394 : void OComponentHelper::release() throw()
75 : {
76 1845394 : Reference<XInterface > x( xDelegator );
77 1845397 : if (! x.is())
78 : {
79 1794184 : if (osl_atomic_decrement( &m_refCount ) == 0)
80 : {
81 29745 : 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 13905 : disposeWeakConnectionPoint();
86 :
87 13905 : Reference<XInterface > xHoldAlive( *this );
88 : // First dispose
89 : try
90 : {
91 13905 : 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 1859301 : return;
108 : }
109 : }
110 : // restore the reference count
111 1780279 : osl_atomic_increment( &m_refCount );
112 : }
113 1831492 : OWeakAggObject::release();
114 : }
115 :
116 160 : Sequence< Type > OComponentHelper::getTypes() throw (RuntimeException, std::exception)
117 : {
118 : static OTypeCollection * s_pTypes = 0;
119 160 : if (! s_pTypes)
120 : {
121 4 : MutexGuard aGuard( Mutex::getGlobalMutex() );
122 4 : if (! s_pTypes)
123 : {
124 : static OTypeCollection s_aTypes(
125 4 : cppu::UnoType<lang::XComponent>::get(),
126 4 : cppu::UnoType<lang::XTypeProvider>::get(),
127 4 : cppu::UnoType<XAggregation>::get(),
128 8 : cppu::UnoType<XWeak>::get() );
129 4 : s_pTypes = &s_aTypes;
130 4 : }
131 : }
132 160 : return s_pTypes->getTypes();
133 : }
134 :
135 : // XComponent
136 14724 : void OComponentHelper::disposing()
137 : {
138 14724 : }
139 :
140 : // XComponent
141 16612 : 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 16612 : Reference<XComponent > xSelf( this );
148 :
149 : // Guard dispose against multible threading
150 : // Remark: It is an error to call dispose more than once
151 16612 : bool bDoDispose = false;
152 : {
153 16612 : MutexGuard aGuard( rBHelper.rMutex );
154 16612 : if( !rBHelper.bDisposed && !rBHelper.bInDispose )
155 : {
156 : // only one call go into this section
157 16598 : rBHelper.bInDispose = sal_True;
158 16598 : bDoDispose = true;
159 16612 : }
160 : }
161 :
162 : // Do not hold the mutex because we are broadcasting
163 16612 : if( bDoDispose )
164 : {
165 : // Create an event with this as sender
166 : try
167 : {
168 : try
169 : {
170 : Reference<XInterface > xSource(
171 16598 : Reference<XInterface >::query( static_cast<XComponent *>(this) ) );
172 33196 : EventObject aEvt;
173 16598 : aEvt.Source = xSource;
174 : // inform all listeners to release this object
175 : // The listener container are automatically cleared
176 16598 : rBHelper.aLC.disposeAndClear( aEvt );
177 : // notify subclasses to do their dispose
178 33196 : 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 16598 : MutexGuard aGuard( rBHelper.rMutex );
189 : // bDispose and bInDisposing must be set in this order:
190 16598 : rBHelper.bDisposed = sal_True;
191 16598 : rBHelper.bInDispose = sal_False;
192 : }
193 0 : catch (RuntimeException &)
194 : {
195 0 : throw;
196 : }
197 0 : catch (Exception & exc)
198 : {
199 : throw RuntimeException(
200 0 : "unexpected UNO exception caught: " + exc.Message );
201 : }
202 : }
203 : else
204 : {
205 : // in a multithreaded environment, it can't be avoided
206 : // that dispose is called twice.
207 : // However this condition is traced, because it MAY indicate an error.
208 : OSL_TRACE( "OComponentHelper::dispose() - dispose called twice" );
209 16612 : }
210 16612 : }
211 :
212 : // XComponent
213 1755 : void OComponentHelper::addEventListener(
214 : const Reference<XEventListener > & rxListener )
215 : throw(::com::sun::star::uno::RuntimeException, std::exception)
216 : {
217 1755 : ClearableMutexGuard aGuard( rBHelper.rMutex );
218 1755 : if (rBHelper.bDisposed || rBHelper.bInDispose)
219 : {
220 0 : aGuard.clear();
221 0 : Reference< XInterface > x( static_cast<XComponent *>(this), UNO_QUERY );
222 0 : rxListener->disposing( EventObject( x ) );
223 : }
224 : else
225 : {
226 1755 : rBHelper.addListener( cppu::UnoType<decltype(rxListener)>::get(), rxListener );
227 1755 : }
228 1755 : }
229 :
230 : // XComponent
231 1264 : void OComponentHelper::removeEventListener(
232 : const Reference<XEventListener > & rxListener )
233 : throw(::com::sun::star::uno::RuntimeException, std::exception)
234 : {
235 1264 : rBHelper.removeListener( cppu::UnoType<decltype(rxListener)>::get(), rxListener );
236 1264 : }
237 :
238 : }
239 :
240 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|