xref: /aoo42x/main/cppuhelper/source/component.cxx (revision 07a3d7f1)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_cppuhelper.hxx"
26 #include <rtl/string.hxx>
27 #include <osl/diagnose.h>
28 #include <cppuhelper/component.hxx>
29 #include <cppuhelper/queryinterface.hxx>
30 #include <cppuhelper/typeprovider.hxx>
31 #include "com/sun/star/uno/RuntimeException.hpp"
32 
33 using namespace osl;
34 using namespace rtl;
35 using namespace com::sun::star;
36 using namespace com::sun::star::uno;
37 using namespace com::sun::star::lang;
38 
39 namespace cppu
40 {
41 
42 //	----------------------------------------------------
43 //	class OComponentHelper
44 //	----------------------------------------------------
45 
OComponentHelper(Mutex & rMutex)46 OComponentHelper::OComponentHelper( Mutex & rMutex ) SAL_THROW( () )
47 	: rBHelper( rMutex )
48 {
49 }
~OComponentHelper()50 OComponentHelper::~OComponentHelper() SAL_THROW( (RuntimeException) )
51 {
52 }
53 
queryInterface(Type const & rType)54 Any OComponentHelper::queryInterface( Type const & rType ) throw (RuntimeException)
55 {
56     return OWeakAggObject::queryInterface( rType );
57 }
queryAggregation(Type const & rType)58 Any OComponentHelper::queryAggregation( Type const & rType ) throw (RuntimeException)
59 {
60     if (rType == ::getCppuType( (Reference< lang::XComponent > const *)0 ))
61     {
62         void * p = static_cast< lang::XComponent * >( this );
63         return Any( &p, rType );
64     }
65     else if (rType == ::getCppuType( (Reference< lang::XTypeProvider > const *)0 ))
66     {
67         void * p = static_cast< lang::XTypeProvider * >( this );
68         return Any( &p, rType );
69     }
70     return OWeakAggObject::queryAggregation( rType );
71 }
acquire()72 void OComponentHelper::acquire() throw ()
73 {
74 	OWeakAggObject::acquire();
75 }
76 
release()77 void OComponentHelper::release() throw()
78 {
79 	Reference<XInterface > x( xDelegator );
80 	if (! x.is())
81 	{
82 		if (osl_decrementInterlockedCount( &m_refCount ) == 0)
83 		{
84 			if (! rBHelper.bDisposed)
85 			{
86                 // *before* again incrementing our ref count, ensure that our weak connection point
87                 // will not create references to us anymore (via XAdapter::queryAdapted)
88                 disposeWeakConnectionPoint();
89 
90 				Reference<XInterface > xHoldAlive( *this );
91 				// First dispose
92 				try
93 				{
94 					dispose();
95 				}
96 				catch (::com::sun::star::uno::RuntimeException & exc)
97 				{
98 					// release should not throw exceptions
99 #if OSL_DEBUG_LEVEL > 0
100                     OString msg( OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ) );
101                     OSL_ENSURE( 0, msg.getStr() );
102 #else
103                     (void) exc; // avoid warning about unused variable
104 #endif
105 				}
106 
107 				// only the alive ref holds the object
108 				OSL_ASSERT( m_refCount == 1 );
109 				// destroy the object if xHoldAlive decrement the refcount to 0
110 				return;
111 			}
112 		}
113 		// restore the reference count
114 		osl_incrementInterlockedCount( &m_refCount );
115 	}
116 	OWeakAggObject::release();
117 }
118 
getTypes()119 Sequence< Type > OComponentHelper::getTypes() throw (RuntimeException)
120 {
121 	static OTypeCollection * s_pTypes = 0;
122 	if (! s_pTypes)
123 	{
124 		MutexGuard aGuard( Mutex::getGlobalMutex() );
125 		if (! s_pTypes)
126 		{
127 			static OTypeCollection s_aTypes(
128 				::getCppuType( (const Reference< lang::XComponent > *)0 ),
129 				::getCppuType( (const Reference< lang::XTypeProvider > *)0 ),
130 				::getCppuType( (const Reference< XAggregation > *)0 ),
131 				::getCppuType( (const Reference< XWeak > *)0 ) );
132 			s_pTypes = &s_aTypes;
133 		}
134 	}
135 	return s_pTypes->getTypes();
136 }
137 
138 // XComponent
disposing()139 void OComponentHelper::disposing()
140 {
141 }
142 
143 // XComponent
dispose()144 void OComponentHelper::dispose()
145 	throw(::com::sun::star::uno::RuntimeException)
146 {
147 	// An frequently programming error is to release the last
148 	// reference to this object in the disposing message.
149 	// Make it rubust, hold a self Reference.
150 	Reference<XComponent > xSelf( this );
151 
152 	// Guard dispose against multible threading
153 	// Remark: It is an error to call dispose more than once
154 	sal_Bool bDoDispose = sal_False;
155 	{
156 	MutexGuard aGuard( rBHelper.rMutex );
157 	if( !rBHelper.bDisposed && !rBHelper.bInDispose )
158 	{
159 		// only one call go into this section
160 		rBHelper.bInDispose = sal_True;
161 		bDoDispose = sal_True;
162 	}
163 	}
164 
165 	// Do not hold the mutex because we are broadcasting
166 	if( bDoDispose )
167 	{
168 		// Create an event with this as sender
169 		try
170 		{
171             try
172             {
173                 Reference<XInterface > xSource(
174                     Reference<XInterface >::query( (XComponent *)this ) );
175                 EventObject aEvt;
176                 aEvt.Source = xSource;
177                 // inform all listeners to release this object
178                 // The listener container are automatically cleared
179                 rBHelper.aLC.disposeAndClear( aEvt );
180                 // notify subclasses to do their dispose
181                 disposing();
182             }
183             catch (...)
184             {
185                 MutexGuard aGuard( rBHelper.rMutex );
186                 // bDispose and bInDisposing must be set in this order:
187                 rBHelper.bDisposed = sal_True;
188                 rBHelper.bInDispose = sal_False;
189                 throw;
190             }
191             MutexGuard aGuard( rBHelper.rMutex );
192             // bDispose and bInDisposing must be set in this order:
193             rBHelper.bDisposed = sal_True;
194             rBHelper.bInDispose = sal_False;
195 		}
196 		catch (RuntimeException &)
197 		{
198 			throw;
199 		}
200 		catch (Exception & exc)
201 		{
202             throw RuntimeException(
203                 OUString( RTL_CONSTASCII_USTRINGPARAM(
204                               "unexpected UNO exception caught: ") ) +
205                 exc.Message, Reference< XInterface >() );
206 		}
207 	}
208 	else
209 	{
210 		// in a multithreaded environment, it can't be avoided,
211 		// that dispose is called twice.
212 		// However this condition is traced, because it MAY indicate an error.
213 		OSL_TRACE( "OComponentHelper::dispose() - dispose called twice" );
214 	}
215 }
216 
217 // XComponent
addEventListener(const Reference<XEventListener> & rxListener)218 void OComponentHelper::addEventListener(
219 	const Reference<XEventListener > & rxListener )
220 	throw(::com::sun::star::uno::RuntimeException)
221 {
222 	ClearableMutexGuard aGuard( rBHelper.rMutex );
223 	if (rBHelper.bDisposed || rBHelper.bInDispose)
224 	{
225         aGuard.clear();
226         Reference< XInterface > x( (XComponent *)this, UNO_QUERY );
227 		rxListener->disposing( EventObject( x ) );
228 	}
229 	else
230 	{
231 		rBHelper.addListener( ::getCppuType( &rxListener ) , rxListener );
232 	}
233 }
234 
235 // XComponent
removeEventListener(const Reference<XEventListener> & rxListener)236 void OComponentHelper::removeEventListener(
237 	const Reference<XEventListener > & rxListener )
238 	throw(::com::sun::star::uno::RuntimeException)
239 {
240 	rBHelper.removeListener( ::getCppuType( &rxListener ) , rxListener );
241 }
242 
243 }
244 
245