xref: /AOO41X/main/cppuhelper/source/implbase.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_cppuhelper.hxx"
30*cdf0e10cSrcweir #include <cppuhelper/implbase.hxx>
31*cdf0e10cSrcweir #include <cppuhelper/compbase.hxx>
32*cdf0e10cSrcweir #include <osl/diagnose.h>
33*cdf0e10cSrcweir #include <rtl/uuid.h>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
36*cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp"
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir using namespace ::osl;
39*cdf0e10cSrcweir using namespace ::rtl;
40*cdf0e10cSrcweir using namespace ::com::sun::star;
41*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir namespace cppu
44*cdf0e10cSrcweir {
45*cdf0e10cSrcweir //==================================================================================================
46*cdf0e10cSrcweir Mutex & SAL_CALL getImplHelperInitMutex(void) SAL_THROW( () )
47*cdf0e10cSrcweir {
48*cdf0e10cSrcweir 	static Mutex * s_pMutex = 0;
49*cdf0e10cSrcweir 	if (! s_pMutex)
50*cdf0e10cSrcweir 	{
51*cdf0e10cSrcweir 		MutexGuard aGuard( Mutex::getGlobalMutex() );
52*cdf0e10cSrcweir 		if (! s_pMutex)
53*cdf0e10cSrcweir 		{
54*cdf0e10cSrcweir 			static Mutex s_aMutex;
55*cdf0e10cSrcweir 			s_pMutex = & s_aMutex;
56*cdf0e10cSrcweir 		}
57*cdf0e10cSrcweir 	}
58*cdf0e10cSrcweir 	return * s_pMutex;
59*cdf0e10cSrcweir }
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir // ClassDataBase
62*cdf0e10cSrcweir //__________________________________________________________________________________________________
63*cdf0e10cSrcweir ClassDataBase::ClassDataBase() SAL_THROW( () )
64*cdf0e10cSrcweir 	: bOffsetsInit( sal_False )
65*cdf0e10cSrcweir 	, nType2Offset( 0 )
66*cdf0e10cSrcweir 	, nClassCode( 0 )
67*cdf0e10cSrcweir 	, pTypes( 0 )
68*cdf0e10cSrcweir 	, pId( 0 )
69*cdf0e10cSrcweir {
70*cdf0e10cSrcweir }
71*cdf0e10cSrcweir //__________________________________________________________________________________________________
72*cdf0e10cSrcweir ClassDataBase::ClassDataBase( sal_Int32 nClassCode_ ) SAL_THROW( () )
73*cdf0e10cSrcweir 	: bOffsetsInit( sal_False )
74*cdf0e10cSrcweir 	, nType2Offset( 0 )
75*cdf0e10cSrcweir 	, nClassCode( nClassCode_ )
76*cdf0e10cSrcweir 	, pTypes( 0 )
77*cdf0e10cSrcweir 	, pId( 0 )
78*cdf0e10cSrcweir {
79*cdf0e10cSrcweir }
80*cdf0e10cSrcweir //__________________________________________________________________________________________________
81*cdf0e10cSrcweir ClassDataBase::~ClassDataBase() SAL_THROW( () )
82*cdf0e10cSrcweir {
83*cdf0e10cSrcweir 	delete pTypes;
84*cdf0e10cSrcweir 	delete pId;
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir 	for ( sal_Int32 nPos = nType2Offset; nPos--; )
87*cdf0e10cSrcweir 	{
88*cdf0e10cSrcweir 		typelib_typedescription_release(
89*cdf0e10cSrcweir 			(typelib_TypeDescription *)((ClassData *)this)->arType2Offset[nPos].pTD );
90*cdf0e10cSrcweir 	}
91*cdf0e10cSrcweir }
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir // ClassData
94*cdf0e10cSrcweir //__________________________________________________________________________________________________
95*cdf0e10cSrcweir void ClassData::writeTypeOffset( const Type & rType, sal_Int32 nOffset ) SAL_THROW( () )
96*cdf0e10cSrcweir {
97*cdf0e10cSrcweir 	arType2Offset[nType2Offset].nOffset = nOffset;
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir 	arType2Offset[nType2Offset].pTD = 0;
100*cdf0e10cSrcweir 	typelib_typedescriptionreference_getDescription(
101*cdf0e10cSrcweir 		(typelib_TypeDescription **)&arType2Offset[nType2Offset].pTD, rType.getTypeLibType() );
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir 	if (arType2Offset[nType2Offset].pTD)
104*cdf0e10cSrcweir 		++nType2Offset;
105*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
106*cdf0e10cSrcweir 	else
107*cdf0e10cSrcweir 	{
108*cdf0e10cSrcweir 		OString msg( "### cannot get type description for " );
109*cdf0e10cSrcweir 		msg += OUStringToOString( rType.getTypeName(), RTL_TEXTENCODING_ASCII_US );
110*cdf0e10cSrcweir 		OSL_ENSURE( sal_False, msg.getStr() );
111*cdf0e10cSrcweir 	}
112*cdf0e10cSrcweir #endif
113*cdf0e10cSrcweir }
114*cdf0e10cSrcweir //__________________________________________________________________________________________________
115*cdf0e10cSrcweir void ClassData::initTypeProvider() SAL_THROW( () )
116*cdf0e10cSrcweir {
117*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
118*cdf0e10cSrcweir 	if (! pTypes)
119*cdf0e10cSrcweir 	{
120*cdf0e10cSrcweir 		// create id
121*cdf0e10cSrcweir 		pId = new Sequence< sal_Int8 >( 16 );
122*cdf0e10cSrcweir 		rtl_createUuid( (sal_uInt8 *)pId->getArray(), 0, sal_True );
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir 		// collect types
125*cdf0e10cSrcweir 		Sequence< Type > * types = new Sequence< Type >(
126*cdf0e10cSrcweir 			nType2Offset + 1 + (nClassCode == 4 ? 2 : nClassCode) );
127*cdf0e10cSrcweir 		Type * pTypeAr = types->getArray();
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir 		// given types
130*cdf0e10cSrcweir 		sal_Int32 nPos = nType2Offset;
131*cdf0e10cSrcweir 		while (nPos--)
132*cdf0e10cSrcweir 			pTypeAr[nPos] = ((typelib_TypeDescription *)arType2Offset[nPos].pTD)->pWeakRef;
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 		// XTypeProvider
135*cdf0e10cSrcweir 		pTypeAr[nType2Offset] = ::getCppuType( (const Reference< lang::XTypeProvider > *)0 );
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir 		// class code extra types: [[XComponent,] XWeak[, XAggregation]]
138*cdf0e10cSrcweir 		switch (nClassCode)
139*cdf0e10cSrcweir 		{
140*cdf0e10cSrcweir 		case 4:
141*cdf0e10cSrcweir 			pTypeAr[nType2Offset +2] = ::getCppuType( (const Reference< lang::XComponent > *)0 );
142*cdf0e10cSrcweir 			pTypeAr[nType2Offset +1] = ::getCppuType( (const Reference< XWeak > *)0 );
143*cdf0e10cSrcweir 			break;
144*cdf0e10cSrcweir 		case 3:
145*cdf0e10cSrcweir 			pTypeAr[nType2Offset +3] = ::getCppuType( (const Reference< lang::XComponent > *)0 );
146*cdf0e10cSrcweir 		case 2:
147*cdf0e10cSrcweir 			pTypeAr[nType2Offset +2] = ::getCppuType( (const Reference< XAggregation > *)0 );
148*cdf0e10cSrcweir 		case 1:
149*cdf0e10cSrcweir 			pTypeAr[nType2Offset +1] = ::getCppuType( (const Reference< XWeak > *)0 );
150*cdf0e10cSrcweir 		}
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir 		pTypes = types;
153*cdf0e10cSrcweir 	}
154*cdf0e10cSrcweir }
155*cdf0e10cSrcweir //__________________________________________________________________________________________________
156*cdf0e10cSrcweir Sequence< Type > ClassData::getTypes() SAL_THROW( () )
157*cdf0e10cSrcweir {
158*cdf0e10cSrcweir 	if (! pTypes)
159*cdf0e10cSrcweir 		initTypeProvider();
160*cdf0e10cSrcweir 	return *pTypes;
161*cdf0e10cSrcweir }
162*cdf0e10cSrcweir //__________________________________________________________________________________________________
163*cdf0e10cSrcweir Sequence< sal_Int8 > ClassData::getImplementationId() SAL_THROW( () )
164*cdf0e10cSrcweir {
165*cdf0e10cSrcweir 	if (! pTypes)
166*cdf0e10cSrcweir 		initTypeProvider();
167*cdf0e10cSrcweir 	return *pId;
168*cdf0e10cSrcweir }
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
171*cdf0e10cSrcweir static inline sal_Bool td_equals(
172*cdf0e10cSrcweir 	typelib_TypeDescription * pTD, typelib_TypeDescriptionReference * pType )
173*cdf0e10cSrcweir 	SAL_THROW( () )
174*cdf0e10cSrcweir {
175*cdf0e10cSrcweir 	return (pTD->pWeakRef == pType ||
176*cdf0e10cSrcweir 			(pTD->pTypeName->length == pType->pTypeName->length &&
177*cdf0e10cSrcweir 			 rtl_ustr_compare( pTD->pTypeName->buffer, pType->pTypeName->buffer ) == 0));
178*cdf0e10cSrcweir }
179*cdf0e10cSrcweir //__________________________________________________________________________________________________
180*cdf0e10cSrcweir Any ClassData::query( const Type & rType, lang::XTypeProvider * pBase ) SAL_THROW( () )
181*cdf0e10cSrcweir {
182*cdf0e10cSrcweir 	if (rType == ::getCppuType( (const Reference< XInterface > *)0 ))
183*cdf0e10cSrcweir 		return Any( &pBase, ::getCppuType( (const Reference< XInterface > *)0 ) );
184*cdf0e10cSrcweir 	for ( sal_Int32 nPos = 0; nPos < nType2Offset; ++nPos )
185*cdf0e10cSrcweir 	{
186*cdf0e10cSrcweir 		const Type_Offset & rTO = arType2Offset[nPos];
187*cdf0e10cSrcweir 		typelib_InterfaceTypeDescription * pTD = rTO.pTD;
188*cdf0e10cSrcweir 		while (pTD)
189*cdf0e10cSrcweir 		{
190*cdf0e10cSrcweir 			if (td_equals( (typelib_TypeDescription *)pTD,
191*cdf0e10cSrcweir 						   *(typelib_TypeDescriptionReference **)&rType ))
192*cdf0e10cSrcweir 			{
193*cdf0e10cSrcweir 				void * pInterface = (char *)pBase + rTO.nOffset;
194*cdf0e10cSrcweir 				return Any( &pInterface, (typelib_TypeDescription *)pTD );
195*cdf0e10cSrcweir 			}
196*cdf0e10cSrcweir 			pTD = pTD->pBaseTypeDescription;
197*cdf0e10cSrcweir 		}
198*cdf0e10cSrcweir 	}
199*cdf0e10cSrcweir 	if (rType == ::getCppuType( (const Reference< lang::XTypeProvider > *)0 ))
200*cdf0e10cSrcweir 		return Any( &pBase, ::getCppuType( (const Reference< lang::XTypeProvider > *)0 ) );
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir 	return Any();
203*cdf0e10cSrcweir }
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir //##################################################################################################
206*cdf0e10cSrcweir //##################################################################################################
207*cdf0e10cSrcweir //##################################################################################################
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir // WeakComponentImplHelperBase
210*cdf0e10cSrcweir //__________________________________________________________________________________________________
211*cdf0e10cSrcweir WeakComponentImplHelperBase::WeakComponentImplHelperBase( Mutex & rMutex )
212*cdf0e10cSrcweir     SAL_THROW( () )
213*cdf0e10cSrcweir     : rBHelper( rMutex )
214*cdf0e10cSrcweir {
215*cdf0e10cSrcweir }
216*cdf0e10cSrcweir //__________________________________________________________________________________________________
217*cdf0e10cSrcweir WeakComponentImplHelperBase::~WeakComponentImplHelperBase()
218*cdf0e10cSrcweir     SAL_THROW( () )
219*cdf0e10cSrcweir {
220*cdf0e10cSrcweir }
221*cdf0e10cSrcweir //__________________________________________________________________________________________________
222*cdf0e10cSrcweir void WeakComponentImplHelperBase::disposing()
223*cdf0e10cSrcweir {
224*cdf0e10cSrcweir }
225*cdf0e10cSrcweir //__________________________________________________________________________________________________
226*cdf0e10cSrcweir Any WeakComponentImplHelperBase::queryInterface( Type const & rType )
227*cdf0e10cSrcweir     throw (RuntimeException)
228*cdf0e10cSrcweir {
229*cdf0e10cSrcweir     if (rType == ::getCppuType( (Reference< lang::XComponent > const *)0 ))
230*cdf0e10cSrcweir     {
231*cdf0e10cSrcweir         void * p = static_cast< lang::XComponent * >( this );
232*cdf0e10cSrcweir         return Any( &p, rType );
233*cdf0e10cSrcweir     }
234*cdf0e10cSrcweir     return OWeakObject::queryInterface( rType );
235*cdf0e10cSrcweir }
236*cdf0e10cSrcweir //__________________________________________________________________________________________________
237*cdf0e10cSrcweir void WeakComponentImplHelperBase::acquire()
238*cdf0e10cSrcweir     throw ()
239*cdf0e10cSrcweir {
240*cdf0e10cSrcweir     OWeakObject::acquire();
241*cdf0e10cSrcweir }
242*cdf0e10cSrcweir //__________________________________________________________________________________________________
243*cdf0e10cSrcweir void WeakComponentImplHelperBase::release()
244*cdf0e10cSrcweir     throw ()
245*cdf0e10cSrcweir {
246*cdf0e10cSrcweir     if (osl_decrementInterlockedCount( &m_refCount ) == 0) {
247*cdf0e10cSrcweir         // ensure no other references are created, via the weak connection point, from now on
248*cdf0e10cSrcweir         disposeWeakConnectionPoint();
249*cdf0e10cSrcweir         // restore reference count:
250*cdf0e10cSrcweir         osl_incrementInterlockedCount( &m_refCount );
251*cdf0e10cSrcweir         if (! rBHelper.bDisposed) {
252*cdf0e10cSrcweir             try {
253*cdf0e10cSrcweir                 dispose();
254*cdf0e10cSrcweir             }
255*cdf0e10cSrcweir             catch (RuntimeException const& exc) { // don't break throw ()
256*cdf0e10cSrcweir                 OSL_ENSURE(
257*cdf0e10cSrcweir                     false, OUStringToOString(
258*cdf0e10cSrcweir                         exc.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
259*cdf0e10cSrcweir                 static_cast<void>(exc);
260*cdf0e10cSrcweir             }
261*cdf0e10cSrcweir             OSL_ASSERT( rBHelper.bDisposed );
262*cdf0e10cSrcweir         }
263*cdf0e10cSrcweir         OWeakObject::release();
264*cdf0e10cSrcweir     }
265*cdf0e10cSrcweir }
266*cdf0e10cSrcweir //__________________________________________________________________________________________________
267*cdf0e10cSrcweir void WeakComponentImplHelperBase::dispose()
268*cdf0e10cSrcweir     throw (RuntimeException)
269*cdf0e10cSrcweir {
270*cdf0e10cSrcweir     ClearableMutexGuard aGuard( rBHelper.rMutex );
271*cdf0e10cSrcweir     if (!rBHelper.bDisposed && !rBHelper.bInDispose)
272*cdf0e10cSrcweir     {
273*cdf0e10cSrcweir         rBHelper.bInDispose = sal_True;
274*cdf0e10cSrcweir         aGuard.clear();
275*cdf0e10cSrcweir         try
276*cdf0e10cSrcweir         {
277*cdf0e10cSrcweir             // side effect: keeping a reference to this
278*cdf0e10cSrcweir             lang::EventObject aEvt( static_cast< OWeakObject * >( this ) );
279*cdf0e10cSrcweir             try
280*cdf0e10cSrcweir             {
281*cdf0e10cSrcweir                 rBHelper.aLC.disposeAndClear( aEvt );
282*cdf0e10cSrcweir                 disposing();
283*cdf0e10cSrcweir             }
284*cdf0e10cSrcweir             catch (...)
285*cdf0e10cSrcweir             {
286*cdf0e10cSrcweir                 MutexGuard aGuard2( rBHelper.rMutex );
287*cdf0e10cSrcweir                 // bDisposed and bInDispose must be set in this order:
288*cdf0e10cSrcweir                 rBHelper.bDisposed = sal_True;
289*cdf0e10cSrcweir                 rBHelper.bInDispose = sal_False;
290*cdf0e10cSrcweir                 throw;
291*cdf0e10cSrcweir             }
292*cdf0e10cSrcweir             MutexGuard aGuard2( rBHelper.rMutex );
293*cdf0e10cSrcweir             // bDisposed and bInDispose must be set in this order:
294*cdf0e10cSrcweir             rBHelper.bDisposed = sal_True;
295*cdf0e10cSrcweir             rBHelper.bInDispose = sal_False;
296*cdf0e10cSrcweir         }
297*cdf0e10cSrcweir         catch (RuntimeException &)
298*cdf0e10cSrcweir         {
299*cdf0e10cSrcweir             throw;
300*cdf0e10cSrcweir         }
301*cdf0e10cSrcweir         catch (Exception & exc)
302*cdf0e10cSrcweir         {
303*cdf0e10cSrcweir             throw RuntimeException(
304*cdf0e10cSrcweir                 OUString( RTL_CONSTASCII_USTRINGPARAM(
305*cdf0e10cSrcweir                               "unexpected UNO exception caught: ") ) +
306*cdf0e10cSrcweir                 exc.Message, Reference< XInterface >() );
307*cdf0e10cSrcweir         }
308*cdf0e10cSrcweir     }
309*cdf0e10cSrcweir }
310*cdf0e10cSrcweir //__________________________________________________________________________________________________
311*cdf0e10cSrcweir void WeakComponentImplHelperBase::addEventListener(
312*cdf0e10cSrcweir     Reference< lang::XEventListener > const & xListener )
313*cdf0e10cSrcweir     throw (RuntimeException)
314*cdf0e10cSrcweir {
315*cdf0e10cSrcweir     ClearableMutexGuard aGuard( rBHelper.rMutex );
316*cdf0e10cSrcweir 	if (rBHelper.bDisposed || rBHelper.bInDispose)
317*cdf0e10cSrcweir 	{
318*cdf0e10cSrcweir         aGuard.clear();
319*cdf0e10cSrcweir         lang::EventObject aEvt( static_cast< OWeakObject * >( this ) );
320*cdf0e10cSrcweir 		xListener->disposing( aEvt );
321*cdf0e10cSrcweir 	}
322*cdf0e10cSrcweir     else
323*cdf0e10cSrcweir     {
324*cdf0e10cSrcweir         rBHelper.addListener( ::getCppuType( &xListener ), xListener );
325*cdf0e10cSrcweir     }
326*cdf0e10cSrcweir }
327*cdf0e10cSrcweir //__________________________________________________________________________________________________
328*cdf0e10cSrcweir void WeakComponentImplHelperBase::removeEventListener(
329*cdf0e10cSrcweir     Reference< lang::XEventListener > const & xListener )
330*cdf0e10cSrcweir     throw (RuntimeException)
331*cdf0e10cSrcweir {
332*cdf0e10cSrcweir     rBHelper.removeListener( ::getCppuType( &xListener ), xListener );
333*cdf0e10cSrcweir }
334*cdf0e10cSrcweir 
335*cdf0e10cSrcweir // WeakAggComponentImplHelperBase
336*cdf0e10cSrcweir //__________________________________________________________________________________________________
337*cdf0e10cSrcweir WeakAggComponentImplHelperBase::WeakAggComponentImplHelperBase( Mutex & rMutex )
338*cdf0e10cSrcweir     SAL_THROW( () )
339*cdf0e10cSrcweir     : rBHelper( rMutex )
340*cdf0e10cSrcweir {
341*cdf0e10cSrcweir }
342*cdf0e10cSrcweir //__________________________________________________________________________________________________
343*cdf0e10cSrcweir WeakAggComponentImplHelperBase::~WeakAggComponentImplHelperBase()
344*cdf0e10cSrcweir     SAL_THROW( () )
345*cdf0e10cSrcweir {
346*cdf0e10cSrcweir }
347*cdf0e10cSrcweir //__________________________________________________________________________________________________
348*cdf0e10cSrcweir void WeakAggComponentImplHelperBase::disposing()
349*cdf0e10cSrcweir {
350*cdf0e10cSrcweir }
351*cdf0e10cSrcweir //__________________________________________________________________________________________________
352*cdf0e10cSrcweir Any WeakAggComponentImplHelperBase::queryInterface( Type const & rType )
353*cdf0e10cSrcweir     throw (RuntimeException)
354*cdf0e10cSrcweir {
355*cdf0e10cSrcweir     return OWeakAggObject::queryInterface( rType );
356*cdf0e10cSrcweir }
357*cdf0e10cSrcweir //__________________________________________________________________________________________________
358*cdf0e10cSrcweir Any WeakAggComponentImplHelperBase::queryAggregation( Type const & rType )
359*cdf0e10cSrcweir     throw (RuntimeException)
360*cdf0e10cSrcweir {
361*cdf0e10cSrcweir     if (rType == ::getCppuType( (Reference< lang::XComponent > const *)0 ))
362*cdf0e10cSrcweir     {
363*cdf0e10cSrcweir         void * p = static_cast< lang::XComponent * >( this );
364*cdf0e10cSrcweir         return Any( &p, rType );
365*cdf0e10cSrcweir     }
366*cdf0e10cSrcweir     return OWeakAggObject::queryAggregation( rType );
367*cdf0e10cSrcweir }
368*cdf0e10cSrcweir //__________________________________________________________________________________________________
369*cdf0e10cSrcweir void WeakAggComponentImplHelperBase::acquire()
370*cdf0e10cSrcweir     throw ()
371*cdf0e10cSrcweir {
372*cdf0e10cSrcweir     OWeakAggObject::acquire();
373*cdf0e10cSrcweir }
374*cdf0e10cSrcweir //__________________________________________________________________________________________________
375*cdf0e10cSrcweir void WeakAggComponentImplHelperBase::release()
376*cdf0e10cSrcweir     throw ()
377*cdf0e10cSrcweir {
378*cdf0e10cSrcweir     Reference<XInterface> const xDelegator_(xDelegator);
379*cdf0e10cSrcweir     if (xDelegator_.is()) {
380*cdf0e10cSrcweir         OWeakAggObject::release();
381*cdf0e10cSrcweir     }
382*cdf0e10cSrcweir     else if (osl_decrementInterlockedCount( &m_refCount ) == 0) {
383*cdf0e10cSrcweir         // ensure no other references are created, via the weak connection point, from now on
384*cdf0e10cSrcweir         disposeWeakConnectionPoint();
385*cdf0e10cSrcweir         // restore reference count:
386*cdf0e10cSrcweir         osl_incrementInterlockedCount( &m_refCount );
387*cdf0e10cSrcweir         if (! rBHelper.bDisposed) {
388*cdf0e10cSrcweir             try {
389*cdf0e10cSrcweir                 dispose();
390*cdf0e10cSrcweir             }
391*cdf0e10cSrcweir             catch (RuntimeException const& exc) { // don't break throw ()
392*cdf0e10cSrcweir                 OSL_ENSURE(
393*cdf0e10cSrcweir                     false, OUStringToOString(
394*cdf0e10cSrcweir                         exc.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
395*cdf0e10cSrcweir                 static_cast<void>(exc);
396*cdf0e10cSrcweir             }
397*cdf0e10cSrcweir             OSL_ASSERT( rBHelper.bDisposed );
398*cdf0e10cSrcweir         }
399*cdf0e10cSrcweir         OWeakAggObject::release();
400*cdf0e10cSrcweir     }
401*cdf0e10cSrcweir }
402*cdf0e10cSrcweir //__________________________________________________________________________________________________
403*cdf0e10cSrcweir void WeakAggComponentImplHelperBase::dispose()
404*cdf0e10cSrcweir     throw (RuntimeException)
405*cdf0e10cSrcweir {
406*cdf0e10cSrcweir     ClearableMutexGuard aGuard( rBHelper.rMutex );
407*cdf0e10cSrcweir     if (!rBHelper.bDisposed && !rBHelper.bInDispose)
408*cdf0e10cSrcweir     {
409*cdf0e10cSrcweir         rBHelper.bInDispose = sal_True;
410*cdf0e10cSrcweir         aGuard.clear();
411*cdf0e10cSrcweir         try
412*cdf0e10cSrcweir         {
413*cdf0e10cSrcweir             // side effect: keeping a reference to this
414*cdf0e10cSrcweir             lang::EventObject aEvt( static_cast< OWeakObject * >( this ) );
415*cdf0e10cSrcweir             try
416*cdf0e10cSrcweir             {
417*cdf0e10cSrcweir                 rBHelper.aLC.disposeAndClear( aEvt );
418*cdf0e10cSrcweir                 disposing();
419*cdf0e10cSrcweir             }
420*cdf0e10cSrcweir             catch (...)
421*cdf0e10cSrcweir             {
422*cdf0e10cSrcweir                 MutexGuard aGuard2( rBHelper.rMutex );
423*cdf0e10cSrcweir                 // bDisposed and bInDispose must be set in this order:
424*cdf0e10cSrcweir                 rBHelper.bDisposed = sal_True;
425*cdf0e10cSrcweir                 rBHelper.bInDispose = sal_False;
426*cdf0e10cSrcweir                 throw;
427*cdf0e10cSrcweir             }
428*cdf0e10cSrcweir             MutexGuard aGuard2( rBHelper.rMutex );
429*cdf0e10cSrcweir             // bDisposed and bInDispose must be set in this order:
430*cdf0e10cSrcweir             rBHelper.bDisposed = sal_True;
431*cdf0e10cSrcweir             rBHelper.bInDispose = sal_False;
432*cdf0e10cSrcweir         }
433*cdf0e10cSrcweir         catch (RuntimeException &)
434*cdf0e10cSrcweir         {
435*cdf0e10cSrcweir             throw;
436*cdf0e10cSrcweir         }
437*cdf0e10cSrcweir         catch (Exception & exc)
438*cdf0e10cSrcweir         {
439*cdf0e10cSrcweir             throw RuntimeException(
440*cdf0e10cSrcweir                 OUString( RTL_CONSTASCII_USTRINGPARAM(
441*cdf0e10cSrcweir                               "unexpected UNO exception caught: ") ) +
442*cdf0e10cSrcweir                 exc.Message, Reference< XInterface >() );
443*cdf0e10cSrcweir         }
444*cdf0e10cSrcweir     }
445*cdf0e10cSrcweir }
446*cdf0e10cSrcweir //__________________________________________________________________________________________________
447*cdf0e10cSrcweir void WeakAggComponentImplHelperBase::addEventListener(
448*cdf0e10cSrcweir     Reference< lang::XEventListener > const & xListener )
449*cdf0e10cSrcweir     throw (RuntimeException)
450*cdf0e10cSrcweir {
451*cdf0e10cSrcweir     ClearableMutexGuard aGuard( rBHelper.rMutex );
452*cdf0e10cSrcweir 	if (rBHelper.bDisposed || rBHelper.bInDispose)
453*cdf0e10cSrcweir 	{
454*cdf0e10cSrcweir         aGuard.clear();
455*cdf0e10cSrcweir         lang::EventObject aEvt( static_cast< OWeakObject * >( this ) );
456*cdf0e10cSrcweir 		xListener->disposing( aEvt );
457*cdf0e10cSrcweir 	}
458*cdf0e10cSrcweir     else
459*cdf0e10cSrcweir     {
460*cdf0e10cSrcweir         rBHelper.addListener( ::getCppuType( &xListener ), xListener );
461*cdf0e10cSrcweir     }
462*cdf0e10cSrcweir }
463*cdf0e10cSrcweir //__________________________________________________________________________________________________
464*cdf0e10cSrcweir void WeakAggComponentImplHelperBase::removeEventListener(
465*cdf0e10cSrcweir     Reference< lang::XEventListener > const & xListener )
466*cdf0e10cSrcweir     throw (RuntimeException)
467*cdf0e10cSrcweir {
468*cdf0e10cSrcweir     rBHelper.removeListener( ::getCppuType( &xListener ), xListener );
469*cdf0e10cSrcweir }
470*cdf0e10cSrcweir 
471*cdf0e10cSrcweir }
472