xref: /aoo42x/main/cppuhelper/source/weak.cxx (revision cdf0e10c)
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 <osl/mutex.hxx>
31*cdf0e10cSrcweir #ifndef _CPPU_WEAKAGG_HXX_
32*cdf0e10cSrcweir #include <cppuhelper/weakagg.hxx>
33*cdf0e10cSrcweir #endif
34*cdf0e10cSrcweir #ifndef _CPPU_HELPER_INTERFACECONTAINER_HXX_
35*cdf0e10cSrcweir #include <cppuhelper/interfacecontainer.hxx>
36*cdf0e10cSrcweir #endif
37*cdf0e10cSrcweir #include "cppuhelper/exc_hlp.hxx"
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir using namespace osl;
40*cdf0e10cSrcweir using namespace com::sun::star::uno;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir /** */ //for docpp
43*cdf0e10cSrcweir namespace cppu
44*cdf0e10cSrcweir {
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir // due to static Reflection destruction from usr, ther must be a mutex leak (#73272#)
47*cdf0e10cSrcweir inline static Mutex & getWeakMutex() SAL_THROW( () )
48*cdf0e10cSrcweir {
49*cdf0e10cSrcweir 	static Mutex * s_pMutex = 0;
50*cdf0e10cSrcweir 	if (! s_pMutex)
51*cdf0e10cSrcweir 		s_pMutex = new Mutex();
52*cdf0e10cSrcweir 	return *s_pMutex;
53*cdf0e10cSrcweir }
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir //------------------------------------------------------------------------
56*cdf0e10cSrcweir //-- OWeakConnectionPoint ----------------------------------------------------
57*cdf0e10cSrcweir //------------------------------------------------------------------------
58*cdf0e10cSrcweir class OWeakConnectionPoint : public XAdapter
59*cdf0e10cSrcweir {
60*cdf0e10cSrcweir public:
61*cdf0e10cSrcweir 	/**
62*cdf0e10cSrcweir 		Hold the weak object without an acquire (only the pointer).
63*cdf0e10cSrcweir 	 */
64*cdf0e10cSrcweir 	OWeakConnectionPoint( OWeakObject* pObj ) SAL_THROW( () )
65*cdf0e10cSrcweir 		: m_aRefCount( 0 )
66*cdf0e10cSrcweir 		, m_pObject(pObj)
67*cdf0e10cSrcweir 		, m_aReferences( getWeakMutex() )
68*cdf0e10cSrcweir 		{}
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir 	// XInterface
71*cdf0e10cSrcweir 	Any SAL_CALL		queryInterface( const Type & rType ) throw(::com::sun::star::uno::RuntimeException);
72*cdf0e10cSrcweir 	void SAL_CALL		acquire() throw();
73*cdf0e10cSrcweir 	void SAL_CALL		release() throw();
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir 	// XAdapter
76*cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL queryAdapted() throw(::com::sun::star::uno::RuntimeException);
77*cdf0e10cSrcweir     void SAL_CALL addReference( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XReference >& xRef ) throw(::com::sun::star::uno::RuntimeException);
78*cdf0e10cSrcweir     void SAL_CALL removeReference( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XReference >& xRef ) throw(::com::sun::star::uno::RuntimeException);
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir 	/// Called from the weak object if the reference count goes to zero.
81*cdf0e10cSrcweir 	void SAL_CALL dispose() throw(::com::sun::star::uno::RuntimeException);
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir private:
84*cdf0e10cSrcweir     OWeakConnectionPoint(OWeakConnectionPoint &); // not defined
85*cdf0e10cSrcweir     void operator =(OWeakConnectionPoint &); // not defined
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir     virtual ~OWeakConnectionPoint() {}
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir 	/// The reference counter.
90*cdf0e10cSrcweir     oslInterlockedCount			m_aRefCount;
91*cdf0e10cSrcweir 	/// The weak object
92*cdf0e10cSrcweir 	OWeakObject* 				m_pObject;
93*cdf0e10cSrcweir 	/// The container to hold the weak references
94*cdf0e10cSrcweir 	OInterfaceContainerHelper	m_aReferences;
95*cdf0e10cSrcweir };
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir // XInterface
98*cdf0e10cSrcweir Any SAL_CALL OWeakConnectionPoint::queryInterface( const Type & rType )
99*cdf0e10cSrcweir 	throw(com::sun::star::uno::RuntimeException)
100*cdf0e10cSrcweir {
101*cdf0e10cSrcweir 	return ::cppu::queryInterface(
102*cdf0e10cSrcweir 		rType, static_cast< XAdapter * >( this ), static_cast< XInterface * >( this ) );
103*cdf0e10cSrcweir }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir // XInterface
106*cdf0e10cSrcweir void SAL_CALL OWeakConnectionPoint::acquire() throw()
107*cdf0e10cSrcweir {
108*cdf0e10cSrcweir 	osl_incrementInterlockedCount( &m_aRefCount );
109*cdf0e10cSrcweir }
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir // XInterface
112*cdf0e10cSrcweir void SAL_CALL OWeakConnectionPoint::release() throw()
113*cdf0e10cSrcweir {
114*cdf0e10cSrcweir 	if (! osl_decrementInterlockedCount( &m_aRefCount ))
115*cdf0e10cSrcweir 		delete this;
116*cdf0e10cSrcweir }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir void SAL_CALL OWeakConnectionPoint::dispose() throw(::com::sun::star::uno::RuntimeException)
119*cdf0e10cSrcweir {
120*cdf0e10cSrcweir     Any ex;
121*cdf0e10cSrcweir 	OInterfaceIteratorHelper aIt( m_aReferences );
122*cdf0e10cSrcweir 	while( aIt.hasMoreElements() )
123*cdf0e10cSrcweir     {
124*cdf0e10cSrcweir         try
125*cdf0e10cSrcweir         {
126*cdf0e10cSrcweir             ((XReference *)aIt.next())->dispose();
127*cdf0e10cSrcweir         }
128*cdf0e10cSrcweir         catch (com::sun::star::lang::DisposedException &) {}
129*cdf0e10cSrcweir         catch (RuntimeException &)
130*cdf0e10cSrcweir         {
131*cdf0e10cSrcweir             ex = cppu::getCaughtException();
132*cdf0e10cSrcweir         }
133*cdf0e10cSrcweir     }
134*cdf0e10cSrcweir     if (ex.hasValue())
135*cdf0e10cSrcweir     {
136*cdf0e10cSrcweir         cppu::throwException(ex);
137*cdf0e10cSrcweir     }
138*cdf0e10cSrcweir }
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir // XInterface
141*cdf0e10cSrcweir Reference< XInterface > SAL_CALL OWeakConnectionPoint::queryAdapted() throw(::com::sun::star::uno::RuntimeException)
142*cdf0e10cSrcweir {
143*cdf0e10cSrcweir 	Reference< XInterface > ret;
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 	ClearableMutexGuard guard(getWeakMutex());
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir 	if (m_pObject)
148*cdf0e10cSrcweir 	{
149*cdf0e10cSrcweir 		oslInterlockedCount n = osl_incrementInterlockedCount( &m_pObject->m_refCount );
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir 		if (n > 1)
152*cdf0e10cSrcweir 		{
153*cdf0e10cSrcweir 			// The refence is incremented. The object cannot be destroyed.
154*cdf0e10cSrcweir 			// Release the guard at the earliest point.
155*cdf0e10cSrcweir 			guard.clear();
156*cdf0e10cSrcweir 			// WeakObject has a (XInterface *) cast operator
157*cdf0e10cSrcweir 			ret = *m_pObject;
158*cdf0e10cSrcweir 			n = osl_decrementInterlockedCount( &m_pObject->m_refCount );
159*cdf0e10cSrcweir 		}
160*cdf0e10cSrcweir 		else
161*cdf0e10cSrcweir 			// Another thread wait in the dispose method at the guard
162*cdf0e10cSrcweir 			n = osl_decrementInterlockedCount( &m_pObject->m_refCount );
163*cdf0e10cSrcweir 	}
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir 	return ret;
166*cdf0e10cSrcweir }
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir // XInterface
169*cdf0e10cSrcweir void SAL_CALL OWeakConnectionPoint::addReference(const Reference< XReference >& rRef)
170*cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
171*cdf0e10cSrcweir {
172*cdf0e10cSrcweir 	m_aReferences.addInterface( (const Reference< XInterface > &)rRef );
173*cdf0e10cSrcweir }
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir // XInterface
176*cdf0e10cSrcweir void SAL_CALL OWeakConnectionPoint::removeReference(const Reference< XReference >& rRef)
177*cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
178*cdf0e10cSrcweir {
179*cdf0e10cSrcweir 	m_aReferences.removeInterface( (const Reference< XInterface > &)rRef );
180*cdf0e10cSrcweir }
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir //------------------------------------------------------------------------
184*cdf0e10cSrcweir //-- OWeakObject -------------------------------------------------------
185*cdf0e10cSrcweir //------------------------------------------------------------------------
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir #ifdef _MSC_VER
188*cdf0e10cSrcweir // Accidentally occurs in msvc mapfile = > had to be outlined.
189*cdf0e10cSrcweir OWeakObject::OWeakObject() SAL_THROW( () )
190*cdf0e10cSrcweir     : m_refCount( 0 ),
191*cdf0e10cSrcweir       m_pWeakConnectionPoint( 0 )
192*cdf0e10cSrcweir {
193*cdf0e10cSrcweir }
194*cdf0e10cSrcweir #endif
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir // XInterface
197*cdf0e10cSrcweir Any SAL_CALL OWeakObject::queryInterface( const Type & rType ) throw(::com::sun::star::uno::RuntimeException)
198*cdf0e10cSrcweir {
199*cdf0e10cSrcweir 	return ::cppu::queryInterface(
200*cdf0e10cSrcweir 		rType,
201*cdf0e10cSrcweir 		static_cast< XWeak * >( this ), static_cast< XInterface * >( this ) );
202*cdf0e10cSrcweir }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir // XInterface
205*cdf0e10cSrcweir void SAL_CALL OWeakObject::acquire() throw()
206*cdf0e10cSrcweir {
207*cdf0e10cSrcweir 	osl_incrementInterlockedCount( &m_refCount );
208*cdf0e10cSrcweir }
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir // XInterface
211*cdf0e10cSrcweir void SAL_CALL OWeakObject::release() throw()
212*cdf0e10cSrcweir {
213*cdf0e10cSrcweir     if (osl_decrementInterlockedCount( &m_refCount ) == 0) {
214*cdf0e10cSrcweir         // notify/clear all weak-refs before object's dtor is executed
215*cdf0e10cSrcweir         // (which may check weak-refs to this object):
216*cdf0e10cSrcweir         disposeWeakConnectionPoint();
217*cdf0e10cSrcweir         // destroy object:
218*cdf0e10cSrcweir         delete this;
219*cdf0e10cSrcweir     }
220*cdf0e10cSrcweir }
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir void OWeakObject::disposeWeakConnectionPoint()
223*cdf0e10cSrcweir {
224*cdf0e10cSrcweir     OSL_PRECOND( m_refCount == 0, "OWeakObject::disposeWeakConnectionPoint: only to be called with a ref count of 0!" );
225*cdf0e10cSrcweir     if (m_pWeakConnectionPoint != 0) {
226*cdf0e10cSrcweir         OWeakConnectionPoint * const p = m_pWeakConnectionPoint;
227*cdf0e10cSrcweir         m_pWeakConnectionPoint = 0;
228*cdf0e10cSrcweir         try {
229*cdf0e10cSrcweir             p->dispose();
230*cdf0e10cSrcweir         }
231*cdf0e10cSrcweir         catch (RuntimeException const& exc) {
232*cdf0e10cSrcweir             OSL_ENSURE(
233*cdf0e10cSrcweir                 false, OUStringToOString(
234*cdf0e10cSrcweir                     exc.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
235*cdf0e10cSrcweir             static_cast<void>(exc);
236*cdf0e10cSrcweir         }
237*cdf0e10cSrcweir         p->release();
238*cdf0e10cSrcweir     }
239*cdf0e10cSrcweir }
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir OWeakObject::~OWeakObject() SAL_THROW( (RuntimeException) )
242*cdf0e10cSrcweir {
243*cdf0e10cSrcweir }
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir // XWeak
246*cdf0e10cSrcweir Reference< XAdapter > SAL_CALL OWeakObject::queryAdapter()
247*cdf0e10cSrcweir 	throw (::com::sun::star::uno::RuntimeException)
248*cdf0e10cSrcweir {
249*cdf0e10cSrcweir 	if (!m_pWeakConnectionPoint)
250*cdf0e10cSrcweir 	{
251*cdf0e10cSrcweir 		// only acquire mutex if member is not created
252*cdf0e10cSrcweir 		MutexGuard aGuard( getWeakMutex() );
253*cdf0e10cSrcweir 		if( !m_pWeakConnectionPoint )
254*cdf0e10cSrcweir 		{
255*cdf0e10cSrcweir 			OWeakConnectionPoint * p = new OWeakConnectionPoint(this);
256*cdf0e10cSrcweir 			p->acquire();
257*cdf0e10cSrcweir 			m_pWeakConnectionPoint = p;
258*cdf0e10cSrcweir 		}
259*cdf0e10cSrcweir 	}
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir 	return m_pWeakConnectionPoint;
262*cdf0e10cSrcweir }
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir //------------------------------------------------------------------------
265*cdf0e10cSrcweir //-- OWeakAggObject ----------------------------------------------------
266*cdf0e10cSrcweir //------------------------------------------------------------------------
267*cdf0e10cSrcweir OWeakAggObject::~OWeakAggObject() SAL_THROW( (RuntimeException) )
268*cdf0e10cSrcweir {
269*cdf0e10cSrcweir }
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir // XInterface
272*cdf0e10cSrcweir void OWeakAggObject::acquire() throw()
273*cdf0e10cSrcweir {
274*cdf0e10cSrcweir 	Reference<XInterface > x( xDelegator );
275*cdf0e10cSrcweir 	if (x.is())
276*cdf0e10cSrcweir 		x->acquire();
277*cdf0e10cSrcweir 	else
278*cdf0e10cSrcweir 		OWeakObject::acquire();
279*cdf0e10cSrcweir }
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir // XInterface
282*cdf0e10cSrcweir void OWeakAggObject::release() throw()
283*cdf0e10cSrcweir {
284*cdf0e10cSrcweir 	Reference<XInterface > x( xDelegator );
285*cdf0e10cSrcweir 	if (x.is())
286*cdf0e10cSrcweir 		x->release();
287*cdf0e10cSrcweir 	else
288*cdf0e10cSrcweir 		OWeakObject::release();
289*cdf0e10cSrcweir }
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir // XInterface
292*cdf0e10cSrcweir Any OWeakAggObject::queryInterface( const Type & rType ) throw(::com::sun::star::uno::RuntimeException)
293*cdf0e10cSrcweir {
294*cdf0e10cSrcweir 	Reference< XInterface > x( xDelegator ); // harden ref
295*cdf0e10cSrcweir 	return (x.is() ? x->queryInterface( rType ) : queryAggregation( rType ));
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir //  	// set rOut to zero, if failed
298*cdf0e10cSrcweir //  	if( !xDelegator.queryHardRef( aUik, rOut ) )
299*cdf0e10cSrcweir //  	{
300*cdf0e10cSrcweir //  		XInterfaceRef x;
301*cdf0e10cSrcweir //  		if( !xDelegator.queryHardRef( ((XInterface*)0)->getSmartUik(), x ) )
302*cdf0e10cSrcweir //  			// reference is not valid
303*cdf0e10cSrcweir //  			queryAggregation( aUik, rOut );
304*cdf0e10cSrcweir //  	}
305*cdf0e10cSrcweir //  	return rOut.is();
306*cdf0e10cSrcweir }
307*cdf0e10cSrcweir 
308*cdf0e10cSrcweir // XAggregation
309*cdf0e10cSrcweir Any OWeakAggObject::queryAggregation( const Type & rType ) throw(::com::sun::star::uno::RuntimeException)
310*cdf0e10cSrcweir {
311*cdf0e10cSrcweir 	return ::cppu::queryInterface(
312*cdf0e10cSrcweir 		rType,
313*cdf0e10cSrcweir 		static_cast< XInterface * >( static_cast< OWeakObject * >( this ) ),
314*cdf0e10cSrcweir 		static_cast< XAggregation * >( this ),
315*cdf0e10cSrcweir 		static_cast< XWeak * >( this ) );
316*cdf0e10cSrcweir }
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir // XAggregation
319*cdf0e10cSrcweir void OWeakAggObject::setDelegator( const Reference<XInterface > & rDelegator ) throw(::com::sun::star::uno::RuntimeException)
320*cdf0e10cSrcweir {
321*cdf0e10cSrcweir 	xDelegator = rDelegator;
322*cdf0e10cSrcweir }
323*cdf0e10cSrcweir 
324*cdf0e10cSrcweir }
325*cdf0e10cSrcweir 
326*cdf0e10cSrcweir /** */ //for docpp
327*cdf0e10cSrcweir namespace com
328*cdf0e10cSrcweir {
329*cdf0e10cSrcweir /** */ //for docpp
330*cdf0e10cSrcweir namespace sun
331*cdf0e10cSrcweir {
332*cdf0e10cSrcweir /** */ //for docpp
333*cdf0e10cSrcweir namespace star
334*cdf0e10cSrcweir {
335*cdf0e10cSrcweir /** */ //for docpp
336*cdf0e10cSrcweir namespace uno
337*cdf0e10cSrcweir {
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir 
340*cdf0e10cSrcweir //------------------------------------------------------------------------
341*cdf0e10cSrcweir //-- OWeakRefListener -----------------------------------------------------
342*cdf0e10cSrcweir //------------------------------------------------------------------------
343*cdf0e10cSrcweir class OWeakRefListener : public XReference
344*cdf0e10cSrcweir {
345*cdf0e10cSrcweir public:
346*cdf0e10cSrcweir 	OWeakRefListener(const OWeakRefListener& rRef) SAL_THROW( () );
347*cdf0e10cSrcweir 	OWeakRefListener(const Reference< XInterface >& xInt) SAL_THROW( () );
348*cdf0e10cSrcweir 	virtual ~OWeakRefListener() SAL_THROW( () );
349*cdf0e10cSrcweir 
350*cdf0e10cSrcweir 	// XInterface
351*cdf0e10cSrcweir     Any SAL_CALL queryInterface( const Type & rType ) throw(RuntimeException);
352*cdf0e10cSrcweir     void SAL_CALL acquire() throw();
353*cdf0e10cSrcweir     void SAL_CALL release() throw();
354*cdf0e10cSrcweir 
355*cdf0e10cSrcweir 	// XReference
356*cdf0e10cSrcweir 	void SAL_CALL 	dispose() throw(::com::sun::star::uno::RuntimeException);
357*cdf0e10cSrcweir 
358*cdf0e10cSrcweir 	/// The reference counter.
359*cdf0e10cSrcweir     oslInterlockedCount			m_aRefCount;
360*cdf0e10cSrcweir 	/// The connection point of the weak object
361*cdf0e10cSrcweir 	Reference< XAdapter >		m_XWeakConnectionPoint;
362*cdf0e10cSrcweir 
363*cdf0e10cSrcweir private:
364*cdf0e10cSrcweir 	OWeakRefListener& SAL_CALL operator=(const OWeakRefListener& rRef) SAL_THROW( () );
365*cdf0e10cSrcweir };
366*cdf0e10cSrcweir 
367*cdf0e10cSrcweir OWeakRefListener::OWeakRefListener(const OWeakRefListener& rRef) SAL_THROW( () )
368*cdf0e10cSrcweir 	: com::sun::star::uno::XReference()
369*cdf0e10cSrcweir     , m_aRefCount( 1 )
370*cdf0e10cSrcweir {
371*cdf0e10cSrcweir     try
372*cdf0e10cSrcweir     {
373*cdf0e10cSrcweir 	m_XWeakConnectionPoint = rRef.m_XWeakConnectionPoint;
374*cdf0e10cSrcweir 
375*cdf0e10cSrcweir 	if (m_XWeakConnectionPoint.is())
376*cdf0e10cSrcweir     {
377*cdf0e10cSrcweir         m_XWeakConnectionPoint->addReference((XReference*)this);
378*cdf0e10cSrcweir     }
379*cdf0e10cSrcweir     }
380*cdf0e10cSrcweir     catch (RuntimeException &) { OSL_ASSERT( 0 ); } // assert here, but no unexpected()
381*cdf0e10cSrcweir     osl_decrementInterlockedCount( &m_aRefCount );
382*cdf0e10cSrcweir }
383*cdf0e10cSrcweir 
384*cdf0e10cSrcweir OWeakRefListener::OWeakRefListener(const Reference< XInterface >& xInt) SAL_THROW( () )
385*cdf0e10cSrcweir 	: m_aRefCount( 1 )
386*cdf0e10cSrcweir {
387*cdf0e10cSrcweir     try
388*cdf0e10cSrcweir     {
389*cdf0e10cSrcweir 	Reference< XWeak > xWeak( Reference< XWeak >::query( xInt ) );
390*cdf0e10cSrcweir 
391*cdf0e10cSrcweir 	if (xWeak.is())
392*cdf0e10cSrcweir 	{
393*cdf0e10cSrcweir 		m_XWeakConnectionPoint = xWeak->queryAdapter();
394*cdf0e10cSrcweir 
395*cdf0e10cSrcweir 		if (m_XWeakConnectionPoint.is())
396*cdf0e10cSrcweir 		{
397*cdf0e10cSrcweir             m_XWeakConnectionPoint->addReference((XReference*)this);
398*cdf0e10cSrcweir 		}
399*cdf0e10cSrcweir 	}
400*cdf0e10cSrcweir     }
401*cdf0e10cSrcweir     catch (RuntimeException &) { OSL_ASSERT( 0 ); } // assert here, but no unexpected()
402*cdf0e10cSrcweir     osl_decrementInterlockedCount( &m_aRefCount );
403*cdf0e10cSrcweir }
404*cdf0e10cSrcweir 
405*cdf0e10cSrcweir OWeakRefListener::~OWeakRefListener() SAL_THROW( () )
406*cdf0e10cSrcweir {
407*cdf0e10cSrcweir     try
408*cdf0e10cSrcweir     {
409*cdf0e10cSrcweir 	if (m_XWeakConnectionPoint.is())
410*cdf0e10cSrcweir     {
411*cdf0e10cSrcweir         acquire(); // dont die again
412*cdf0e10cSrcweir 		m_XWeakConnectionPoint->removeReference((XReference*)this);
413*cdf0e10cSrcweir     }
414*cdf0e10cSrcweir     }
415*cdf0e10cSrcweir     catch (RuntimeException &) { OSL_ASSERT( 0 ); } // assert here, but no unexpected()
416*cdf0e10cSrcweir }
417*cdf0e10cSrcweir 
418*cdf0e10cSrcweir // XInterface
419*cdf0e10cSrcweir Any SAL_CALL OWeakRefListener::queryInterface( const Type & rType ) throw(RuntimeException)
420*cdf0e10cSrcweir {
421*cdf0e10cSrcweir 	return ::cppu::queryInterface(
422*cdf0e10cSrcweir 		rType, static_cast< XReference * >( this ), static_cast< XInterface * >( this ) );
423*cdf0e10cSrcweir }
424*cdf0e10cSrcweir 
425*cdf0e10cSrcweir // XInterface
426*cdf0e10cSrcweir void SAL_CALL OWeakRefListener::acquire() throw()
427*cdf0e10cSrcweir {
428*cdf0e10cSrcweir 	osl_incrementInterlockedCount( &m_aRefCount );
429*cdf0e10cSrcweir }
430*cdf0e10cSrcweir 
431*cdf0e10cSrcweir // XInterface
432*cdf0e10cSrcweir void SAL_CALL OWeakRefListener::release() throw()
433*cdf0e10cSrcweir {
434*cdf0e10cSrcweir 	if( ! osl_decrementInterlockedCount( &m_aRefCount ) )
435*cdf0e10cSrcweir 		delete this;
436*cdf0e10cSrcweir }
437*cdf0e10cSrcweir 
438*cdf0e10cSrcweir void SAL_CALL OWeakRefListener::dispose()
439*cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
440*cdf0e10cSrcweir {
441*cdf0e10cSrcweir 	Reference< XAdapter > xAdp;
442*cdf0e10cSrcweir 	{
443*cdf0e10cSrcweir 		MutexGuard guard(cppu::getWeakMutex());
444*cdf0e10cSrcweir 		if( m_XWeakConnectionPoint.is() )
445*cdf0e10cSrcweir 		{
446*cdf0e10cSrcweir 			xAdp = m_XWeakConnectionPoint;
447*cdf0e10cSrcweir 			m_XWeakConnectionPoint.clear();
448*cdf0e10cSrcweir 		}
449*cdf0e10cSrcweir 	}
450*cdf0e10cSrcweir 
451*cdf0e10cSrcweir 	if( xAdp.is() )
452*cdf0e10cSrcweir 		xAdp->removeReference((XReference*)this);
453*cdf0e10cSrcweir }
454*cdf0e10cSrcweir 
455*cdf0e10cSrcweir //------------------------------------------------------------------------
456*cdf0e10cSrcweir //-- WeakReferenceHelper ----------------------------------------------------------
457*cdf0e10cSrcweir //------------------------------------------------------------------------
458*cdf0e10cSrcweir WeakReferenceHelper::WeakReferenceHelper(const Reference< XInterface >& xInt) SAL_THROW( () )
459*cdf0e10cSrcweir 	: m_pImpl( 0 )
460*cdf0e10cSrcweir {
461*cdf0e10cSrcweir 	if (xInt.is())
462*cdf0e10cSrcweir 	{
463*cdf0e10cSrcweir 		m_pImpl = new OWeakRefListener(xInt);
464*cdf0e10cSrcweir 		m_pImpl->acquire();
465*cdf0e10cSrcweir 	}
466*cdf0e10cSrcweir }
467*cdf0e10cSrcweir 
468*cdf0e10cSrcweir WeakReferenceHelper::WeakReferenceHelper(const WeakReferenceHelper& rWeakRef) SAL_THROW( () )
469*cdf0e10cSrcweir 	: m_pImpl( 0 )
470*cdf0e10cSrcweir {
471*cdf0e10cSrcweir 	Reference< XInterface > xInt( rWeakRef.get() );
472*cdf0e10cSrcweir 	if (xInt.is())
473*cdf0e10cSrcweir 	{
474*cdf0e10cSrcweir 		m_pImpl = new OWeakRefListener(xInt);
475*cdf0e10cSrcweir 		m_pImpl->acquire();
476*cdf0e10cSrcweir 	}
477*cdf0e10cSrcweir }
478*cdf0e10cSrcweir 
479*cdf0e10cSrcweir void WeakReferenceHelper::clear() SAL_THROW( () )
480*cdf0e10cSrcweir {
481*cdf0e10cSrcweir     try
482*cdf0e10cSrcweir     {
483*cdf0e10cSrcweir         if (m_pImpl)
484*cdf0e10cSrcweir         {
485*cdf0e10cSrcweir             if (m_pImpl->m_XWeakConnectionPoint.is())
486*cdf0e10cSrcweir             {
487*cdf0e10cSrcweir                 m_pImpl->m_XWeakConnectionPoint->removeReference(
488*cdf0e10cSrcweir                         (XReference*)m_pImpl);
489*cdf0e10cSrcweir                 m_pImpl->m_XWeakConnectionPoint.clear();
490*cdf0e10cSrcweir             }
491*cdf0e10cSrcweir             m_pImpl->release();
492*cdf0e10cSrcweir             m_pImpl = 0;
493*cdf0e10cSrcweir         }
494*cdf0e10cSrcweir     }
495*cdf0e10cSrcweir     catch (RuntimeException &) { OSL_ASSERT( 0 ); } // assert here, but no unexpected()
496*cdf0e10cSrcweir }
497*cdf0e10cSrcweir 
498*cdf0e10cSrcweir WeakReferenceHelper& WeakReferenceHelper::operator=(const WeakReferenceHelper& rWeakRef) SAL_THROW( () )
499*cdf0e10cSrcweir {
500*cdf0e10cSrcweir     if (this == &rWeakRef)
501*cdf0e10cSrcweir     {
502*cdf0e10cSrcweir         return *this;
503*cdf0e10cSrcweir     }
504*cdf0e10cSrcweir     Reference< XInterface > xInt( rWeakRef.get() );
505*cdf0e10cSrcweir     return operator = ( xInt );
506*cdf0e10cSrcweir }
507*cdf0e10cSrcweir 
508*cdf0e10cSrcweir WeakReferenceHelper & SAL_CALL
509*cdf0e10cSrcweir WeakReferenceHelper::operator= (const Reference< XInterface > & xInt)
510*cdf0e10cSrcweir SAL_THROW( () )
511*cdf0e10cSrcweir {
512*cdf0e10cSrcweir     try
513*cdf0e10cSrcweir     {
514*cdf0e10cSrcweir         clear();
515*cdf0e10cSrcweir 		if (xInt.is())
516*cdf0e10cSrcweir 		{
517*cdf0e10cSrcweir 			m_pImpl = new OWeakRefListener(xInt);
518*cdf0e10cSrcweir 			m_pImpl->acquire();
519*cdf0e10cSrcweir 		}
520*cdf0e10cSrcweir     }
521*cdf0e10cSrcweir     catch (RuntimeException &) { OSL_ASSERT( 0 ); } // assert here, but no unexpected()
522*cdf0e10cSrcweir     return *this;
523*cdf0e10cSrcweir }
524*cdf0e10cSrcweir 
525*cdf0e10cSrcweir WeakReferenceHelper::~WeakReferenceHelper() SAL_THROW( () )
526*cdf0e10cSrcweir {
527*cdf0e10cSrcweir     clear();
528*cdf0e10cSrcweir }
529*cdf0e10cSrcweir 
530*cdf0e10cSrcweir Reference< XInterface > WeakReferenceHelper::get() const SAL_THROW( () )
531*cdf0e10cSrcweir {
532*cdf0e10cSrcweir     try
533*cdf0e10cSrcweir     {
534*cdf0e10cSrcweir 	Reference< XAdapter > xAdp;
535*cdf0e10cSrcweir 	{
536*cdf0e10cSrcweir 		MutexGuard guard(cppu::getWeakMutex());
537*cdf0e10cSrcweir 		if( m_pImpl && m_pImpl->m_XWeakConnectionPoint.is() )
538*cdf0e10cSrcweir 			xAdp = m_pImpl->m_XWeakConnectionPoint;
539*cdf0e10cSrcweir 	}
540*cdf0e10cSrcweir 
541*cdf0e10cSrcweir 	if (xAdp.is())
542*cdf0e10cSrcweir 		return xAdp->queryAdapted();
543*cdf0e10cSrcweir     }
544*cdf0e10cSrcweir     catch (RuntimeException &) { OSL_ASSERT( 0 ); } // assert here, but no unexpected()
545*cdf0e10cSrcweir 
546*cdf0e10cSrcweir 	return Reference< XInterface >();
547*cdf0e10cSrcweir }
548*cdf0e10cSrcweir 
549*cdf0e10cSrcweir }
550*cdf0e10cSrcweir }
551*cdf0e10cSrcweir }
552*cdf0e10cSrcweir }
553*cdf0e10cSrcweir 
554