LCOV - code coverage report
Current view: top level - chart2/source/inc - LifeTime.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 17 18 94.4 %
Date: 2015-06-13 12:38:46 Functions: 6 7 85.7 %
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             : #ifndef INCLUDED_CHART2_SOURCE_INC_LIFETIME_HXX
      20             : #define INCLUDED_CHART2_SOURCE_INC_LIFETIME_HXX
      21             : 
      22             : #include <osl/mutex.hxx>
      23             : #include <osl/conditn.hxx>
      24             : #include <com/sun/star/uno/Exception.hpp>
      25             : #include <cppuhelper/interfacecontainer.hxx>
      26             : #include <com/sun/star/util/XCloseListener.hpp>
      27             : #include <com/sun/star/util/XCloseable.hpp>
      28             : #include <com/sun/star/lang/XComponent.hpp>
      29             : #include <cppuhelper/weakref.hxx>
      30             : #include "charttoolsdllapi.hxx"
      31             : 
      32             : namespace apphelper
      33             : {
      34             : 
      35             : class LifeTimeGuard;
      36             : class OOO_DLLPUBLIC_CHARTTOOLS LifeTimeManager
      37             : {
      38             : friend class LifeTimeGuard;
      39             : protected:
      40             :     mutable ::osl::Mutex                    m_aAccessMutex;
      41             : public:
      42             :     LifeTimeManager( ::com::sun::star::lang::XComponent* pComponent, bool bLongLastingCallsCancelable = false );
      43             :     virtual ~LifeTimeManager();
      44             : 
      45             :     bool        impl_isDisposed( bool bAssert=true );
      46             :     bool    dispose() throw(::com::sun::star::uno::RuntimeException);
      47             : 
      48             : public:
      49             :     ::cppu::OMultiTypeInterfaceContainerHelper      m_aListenerContainer;
      50             : 
      51             : protected:
      52             :     SAL_DLLPRIVATE virtual bool    impl_canStartApiCall();
      53           0 :     SAL_DLLPRIVATE virtual void        impl_apiCallCountReachedNull(){}
      54             : 
      55             :     SAL_DLLPRIVATE void        impl_registerApiCall(bool bLongLastingCall);
      56             :     SAL_DLLPRIVATE void        impl_unregisterApiCall(bool bLongLastingCall);
      57             : 
      58             :     SAL_DLLPRIVATE void        impl_init();
      59             : 
      60             : protected:
      61             :     ::com::sun::star::lang::XComponent*     m_pComponent;
      62             : 
      63             :     ::osl::Condition        m_aNoAccessCountCondition;
      64             :     sal_Int32 volatile      m_nAccessCount;
      65             : 
      66             :     bool volatile       m_bDisposed;
      67             :     bool volatile       m_bInDispose;
      68             : 
      69             :     bool                m_bLongLastingCallsCancelable;
      70             :     ::osl::Condition        m_aNoLongLastingCallCountCondition;
      71             :     sal_Int32 volatile      m_nLongLastingCallCount;
      72             : };
      73             : 
      74             : class CloseableLifeTimeManager : public LifeTimeManager
      75             : {
      76             : protected:
      77             :     ::com::sun::star::util::XCloseable*         m_pCloseable;
      78             : 
      79             :     ::osl::Condition        m_aEndTryClosingCondition;
      80             :     bool volatile       m_bClosed;
      81             :     bool volatile       m_bInTryClose;
      82             :     //the ownership between model and controller is not clear at first
      83             :     //each controller might consider him as owner of the model first
      84             :     //at start the model is not considered as owner of itself
      85             :     bool volatile       m_bOwnership;
      86             : 
      87             : public:
      88             : OOO_DLLPUBLIC_CHARTTOOLS    CloseableLifeTimeManager( ::com::sun::star::util::XCloseable* pCloseable
      89             :         , ::com::sun::star::lang::XComponent* pComponent
      90             :         , bool bLongLastingCallsCancelable = false );
      91             : OOO_DLLPUBLIC_CHARTTOOLS    virtual ~CloseableLifeTimeManager();
      92             : 
      93             : OOO_DLLPUBLIC_CHARTTOOLS    bool        impl_isDisposedOrClosed( bool bAssert=true );
      94             : OOO_DLLPUBLIC_CHARTTOOLS    bool    g_close_startTryClose(bool bDeliverOwnership)
      95             :                     throw ( ::com::sun::star::uno::Exception );
      96             : OOO_DLLPUBLIC_CHARTTOOLS    bool    g_close_isNeedToCancelLongLastingCalls( bool bDeliverOwnership, ::com::sun::star::util::CloseVetoException& ex )
      97             :                     throw ( ::com::sun::star::util::CloseVetoException );
      98             : OOO_DLLPUBLIC_CHARTTOOLS    void        g_close_endTryClose(bool bDeliverOwnership, bool bMyVeto );
      99             : OOO_DLLPUBLIC_CHARTTOOLS    void        g_close_endTryClose_doClose();
     100             : OOO_DLLPUBLIC_CHARTTOOLS    bool    g_addCloseListener( const ::com::sun::star::uno::Reference<
     101             :                     ::com::sun::star::util::XCloseListener > & xListener )
     102             :                     throw(::com::sun::star::uno::RuntimeException);
     103             : 
     104             : protected:
     105             :     virtual bool    impl_canStartApiCall() SAL_OVERRIDE;
     106             :     virtual void        impl_apiCallCountReachedNull() SAL_OVERRIDE;
     107             : 
     108             :     void        impl_setOwnership( bool bDeliverOwnership, bool bMyVeto );
     109       28616 :     bool    impl_shouldCloseAtNextChance() { return m_bOwnership;}
     110             :     void        impl_doClose();
     111             : 
     112         321 :     void        impl_init()
     113             :     {
     114         321 :         m_bClosed = false;
     115         321 :         m_bInTryClose = false;
     116         321 :         m_bOwnership = false;
     117         321 :         m_aEndTryClosingCondition.set();
     118         321 :     }
     119             : };
     120             : 
     121             : /*
     122             : Use this Guard in your apicalls to protect access on resources
     123             : which will be released in dispose.
     124             : It's guarantied, that the release of resources only starts if your
     125             : guarded call has finished.
     126             : ! It's only partly guaranteed that this resources will not change during the call.
     127             : See the example for details.
     128             : 
     129             : This class is to be used as described in the example.
     130             : 
     131             : If this guard is used in all api calls of an XCloseable object
     132             : it's guarantied, that the closeable will close itself after finishing the last call
     133             : if it should do so.
     134             : 
     135             :   ::ApiCall
     136             : {
     137             :     //hold no mutex!!!
     138             :     LifeTimeGuard aLifeTimeGuard(m_aLifeTimeManager);
     139             : 
     140             :     //mutex is acquired; call is not registered
     141             : 
     142             :     if(!aLifeTimeGuard.startApiCall())
     143             :         return ; //behave as passive as possible, if disposed or closed
     144             : 
     145             :     //mutex is acquired, call is registered
     146             :     {
     147             :         //you might access some private members here
     148             :         //but than you need to protect access to these members always like this
     149             :         //never call to the outside here
     150             :     }
     151             : 
     152             :     aLifeTimeGuard.clear(); //!!!
     153             : 
     154             :       //Mutex is released, the running call is still registered
     155             :     //this call will finish before the 'release-section' in dispose is allowed to start
     156             : 
     157             :     {
     158             :         //you might access some private members here guarded with your own mutex
     159             :         //but release your mutex at the end of this block
     160             :     }
     161             : 
     162             :     //you can call to the outside (without holding the mutex) without becoming disposed
     163             : 
     164             :     //End of method -> ~LifeTimeGuard
     165             :     //-> call is unregistered
     166             :     //-> this object might be disposed now
     167             : }
     168             : 
     169             : your XComponent::dispose method has to be implemented in the following way:
     170             : 
     171             :     ::dispose()
     172             : {
     173             :     //hold no mutex!!!
     174             :     if( !m_aLifeTimeManager.dispose() )
     175             :         return;
     176             : 
     177             :     //--release all resources and references
     178             : 
     179             : }
     180             : 
     181             : */
     182             : 
     183             : class OOO_DLLPUBLIC_CHARTTOOLS LifeTimeGuard
     184             : {
     185             : 
     186             : public:
     187       29078 :     LifeTimeGuard( LifeTimeManager& rManager )
     188             :         : m_guard( rManager.m_aAccessMutex )
     189             :         , m_rManager(rManager)
     190             :         , m_bCallRegistered(false)
     191       29078 :         , m_bLongLastingCallRegistered(false)
     192             :     {
     193             : 
     194       29078 :     }
     195             :     bool startApiCall(bool bLongLastingCall=false);
     196             :     ~LifeTimeGuard();
     197        2751 :     void clear() { m_guard.clear(); }
     198             : 
     199             : private:
     200             :     osl::ClearableMutexGuard m_guard;
     201             :     LifeTimeManager&    m_rManager;
     202             :     bool            m_bCallRegistered;
     203             :     bool            m_bLongLastingCallRegistered;
     204             : 
     205             : private:
     206             :     LifeTimeGuard( const LifeTimeGuard& ) SAL_DELETED_FUNCTION;
     207             :     LifeTimeGuard& operator= ( const LifeTimeGuard& ) SAL_DELETED_FUNCTION;
     208             : };
     209             : 
     210             : template<class T>
     211             : class NegativeGuard
     212             : {
     213             : protected:
     214             :     T * m_pT;
     215             : public:
     216             : 
     217             :     NegativeGuard(T * pT) : m_pT(pT)
     218             :     {
     219             :         m_pT->release();
     220             :     }
     221             : 
     222         319 :     NegativeGuard(T & t) : m_pT(&t)
     223             :     {
     224         319 :         m_pT->release();
     225         319 :     }
     226             : 
     227         319 :     ~NegativeGuard()
     228             :     {
     229         319 :         m_pT->acquire();
     230         319 :     }
     231             : };
     232             : 
     233             : }//end namespace apphelper
     234             : #endif
     235             : 
     236             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11