LCOV - code coverage report
Current view: top level - include/comphelper - scoped_disposing_ptr.hxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 55 0.0 %
Date: 2014-04-14 Functions: 0 42 0.0 %
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             : 
      10             : #ifndef _SCOPED_DISPOSING_PTR
      11             : #define _SCOPED_DISPOSING_PTR
      12             : 
      13             : #include <cppuhelper/implbase1.hxx>
      14             : #include <boost/utility.hpp>
      15             : #include <boost/scoped_ptr.hpp>
      16             : 
      17             : #include <com/sun/star/lang/XComponent.hpp>
      18             : #include <com/sun/star/frame/XDesktop.hpp>
      19             : 
      20             : // for locking SolarMutex: svapp + mutex
      21             : #include <vcl/svapp.hxx>
      22             : #include <osl/mutex.hxx>
      23             : 
      24             : namespace comphelper
      25             : {
      26             : //Similar to boost::scoped_ptr, except additionally releases the ptr on XComponent::disposing and/or XTerminateListener::notifyTermination if supported
      27             : template<class T> class scoped_disposing_ptr : private boost::noncopyable
      28             : {
      29             : private:
      30             :     boost::scoped_ptr<T> m_aItem;
      31             :     ::com::sun::star::uno::Reference< ::com::sun::star::frame::XTerminateListener> m_xTerminateListener;
      32             : public:
      33           0 :     scoped_disposing_ptr( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > &rComponent, T * p = 0 )
      34           0 :         : m_aItem(p)
      35             :     {
      36           0 :         m_xTerminateListener = new TerminateListener(rComponent, *this);
      37           0 :     }
      38             : 
      39           0 :     virtual void reset(T * p = 0)
      40             :     {
      41           0 :         m_aItem.reset(p);
      42           0 :     }
      43             : 
      44             :     T & operator*() const
      45             :     {
      46             :         return *m_aItem;
      47             :     }
      48             : 
      49           0 :     T * get() const
      50             :     {
      51           0 :         return m_aItem.get();
      52             :     }
      53             : 
      54           0 :     T * operator->() const
      55             :     {
      56           0 :         return m_aItem.get();
      57             :     }
      58             : 
      59           0 :     operator bool () const
      60             :     {
      61           0 :         return static_cast< bool >(m_aItem);
      62             :     }
      63             : 
      64           0 :     virtual ~scoped_disposing_ptr()
      65             :     {
      66           0 :         reset();
      67           0 :     }
      68             : private:
      69             :     class TerminateListener : public ::cppu::WeakImplHelper1< ::com::sun::star::frame::XTerminateListener >
      70             :     {
      71             :     private:
      72             :         ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > m_xComponent;
      73             :         scoped_disposing_ptr<T>& m_rItem;
      74             :     public:
      75           0 :         TerminateListener(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > &rComponent,
      76           0 :             scoped_disposing_ptr<T>& rItem) : m_xComponent(rComponent), m_rItem(rItem)
      77             :         {
      78           0 :             if (m_xComponent.is())
      79             :             {
      80           0 :                 ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDesktop> xDesktop(m_xComponent, ::com::sun::star::uno::UNO_QUERY);
      81           0 :                 if (xDesktop.is())
      82           0 :                     xDesktop->addTerminateListener(this);
      83             :                 else
      84           0 :                     m_xComponent->addEventListener(this);
      85             :             }
      86           0 :         }
      87             : 
      88           0 :         virtual ~TerminateListener()
      89             :         {
      90           0 :             if ( m_xComponent.is() )
      91             :             {
      92           0 :                 ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDesktop> xDesktop(m_xComponent, ::com::sun::star::uno::UNO_QUERY);
      93           0 :                 if (xDesktop.is())
      94           0 :                     xDesktop->removeTerminateListener(this);
      95             :                 else
      96           0 :                     m_xComponent->removeEventListener(this);
      97             :             }
      98           0 :         }
      99             : 
     100             :     private:
     101             :         // XEventListener
     102           0 :         virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& rEvt )
     103             :             throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
     104             :         {
     105           0 :             bool shutDown = (rEvt.Source == m_xComponent);
     106             : 
     107           0 :             if (shutDown && m_xComponent.is())
     108             :             {
     109           0 :                 ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDesktop> xDesktop(m_xComponent, ::com::sun::star::uno::UNO_QUERY);
     110           0 :                 if (xDesktop.is())
     111           0 :                     xDesktop->removeTerminateListener(this);
     112             :                 else
     113           0 :                     m_xComponent->removeEventListener(this);
     114           0 :                 m_xComponent.clear();
     115             :             }
     116             : 
     117           0 :             if (shutDown)
     118           0 :                m_rItem.reset();
     119           0 :         }
     120             : 
     121             :         // XTerminateListener
     122           0 :         virtual void SAL_CALL queryTermination( const ::com::sun::star::lang::EventObject& )
     123             :             throw(::com::sun::star::frame::TerminationVetoException,
     124             :                   ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
     125             :         {
     126           0 :         }
     127             : 
     128           0 :         virtual void SAL_CALL notifyTermination( const ::com::sun::star::lang::EventObject& rEvt )
     129             :             throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
     130             :         {
     131           0 :             disposing(rEvt);
     132           0 :         }
     133             :    };
     134             : };
     135             : 
     136             : //Something like an OutputDevice requires the SolarMutex to be taken before use
     137             : //for threadsafety. The user can ensure this, except in the case of its dtor
     138             : //being called from reset due to a terminate on the XComponent being called
     139             : //from an aribitrary thread
     140           0 : template<class T> class scoped_disposing_solar_mutex_reset_ptr
     141             :     : public scoped_disposing_ptr<T>
     142             : {
     143             : public:
     144           0 :     scoped_disposing_solar_mutex_reset_ptr( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > &rComponent, T * p = 0 )
     145           0 :         : scoped_disposing_ptr<T>(rComponent, p)
     146             :     {
     147           0 :     }
     148             : 
     149           0 :     virtual void reset(T * p = 0)
     150             :     {
     151           0 :         SolarMutexGuard aGuard;
     152           0 :         scoped_disposing_ptr<T>::reset(p);
     153           0 :     }
     154             : };
     155             : 
     156             : }
     157             : 
     158             : #endif
     159             : 
     160             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10