LCOV - code coverage report
Current view: top level - cppuhelper/source - weak.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 168 181 92.8 %
Date: 2014-04-11 Functions: 38 39 97.4 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10