LCOV - code coverage report
Current view: top level - framework/source/inc/loadenv - actionlockguard.hxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 29 39 74.4 %
Date: 2014-04-11 Functions: 5 7 71.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             : #ifndef INCLUDED_FRAMEWORK_SOURCE_INC_LOADENV_ACTIONLOCKGUARD_HXX
      21             : #define INCLUDED_FRAMEWORK_SOURCE_INC_LOADENV_ACTIONLOCKGUARD_HXX
      22             : 
      23             : #include <com/sun/star/document/XActionLockable.hpp>
      24             : 
      25             : namespace framework{
      26             : 
      27             : /** @short  implements a guard, which can use the interface
      28             :             <type scope="com::sun::star::document">XActionLockable</type>.
      29             : 
      30             :     @descr  This guard should be used to be shure, that any lock will be
      31             :             released. Otherwise the locaked document can hinder the office on shutdown!
      32             : */
      33             : class ActionLockGuard
      34             : {
      35             : 
      36             :     // member
      37             : 
      38             :     private:
      39             :         osl::Mutex m_mutex;
      40             : 
      41             :         /** @short  points to the object, which can be locked from outside. */
      42             :         css::uno::Reference< css::document::XActionLockable > m_xActionLock;
      43             : 
      44             :         /** @short  knows if a lock exists on the internal lock object
      45             :                     forced by this guard instance. */
      46             :         bool m_bActionLocked;
      47             : 
      48             :     // interface
      49             : 
      50             :     public:
      51             : 
      52             :         /** @short  default ctor to initialize a "non working guard".
      53             : 
      54             :             @descr  That can be useful in cases, where no resource still exists,
      55             :                     but will be available next time. Then this guard can be used
      56             :                     in a mode "use guard for more then one resources".
      57             :          */
      58        2079 :         ActionLockGuard()
      59        2079 :             : m_bActionLocked(false)
      60             :         {
      61        2079 :         }
      62             : 
      63             :         /** @short  initialize new guard instance and lock the given resource immediately.
      64             : 
      65             :             @param  xLock
      66             :                     points to the outside resource, which should be locked.
      67             :          */
      68             :         ActionLockGuard(const css::uno::Reference< css::document::XActionLockable >& xLock)
      69             :             : m_bActionLocked(false)
      70             :         {
      71             :             setResource(xLock);
      72             :         }
      73             : 
      74             :         /** @short  release this guard instance and make shure, that no lock
      75             :                     will exist afterwards on the internal wrapped resource.
      76             :          */
      77        2079 :         virtual ~ActionLockGuard()
      78        4158 :         {
      79        2079 :             unlock();
      80        2079 :         }
      81             : 
      82             :         /** @short  set a new resource for locking at this guard.
      83             : 
      84             :             @descr  This call will fail, if an internal resource already exists
      85             :                     and is currently locked.
      86             : 
      87             :             @param  xLock
      88             :                     points to the outside resource, which should be locked.
      89             : 
      90             :             @return sal_True, if new resource could be set and locked.
      91             :                     sal_False otherwise.
      92             :          */
      93        2079 :         virtual bool setResource(const css::uno::Reference< css::document::XActionLockable >& xLock)
      94             :         {
      95        2079 :             osl::MutexGuard g(m_mutex);
      96             : 
      97        2079 :             if (m_bActionLocked || !xLock.is())
      98           0 :                 return false;
      99             : 
     100        2079 :             m_xActionLock = xLock;
     101        2079 :             m_xActionLock->addActionLock();
     102        2079 :             m_bActionLocked = m_xActionLock->isActionLocked();
     103             : 
     104        2079 :             return true;
     105             :         }
     106             : 
     107             :         /** @short  set a new resource for locking at this guard.
     108             : 
     109             :             @descr  This call will fail, if an internal resource already exists
     110             :                     and is currently locked.
     111             : 
     112             :             @param  xLock
     113             :                     points to the outside resource, which should be locked.
     114             : 
     115             :             @return sal_True, if new resource could be set and locked.
     116             :                     sal_False otherwise.
     117             :          */
     118        2079 :         virtual void freeResource()
     119             :         {
     120             :             // SAFE -> ..........................
     121        2079 :             osl::ClearableMutexGuard aMutexLock(m_mutex);
     122             : 
     123        4158 :             css::uno::Reference< css::document::XActionLockable > xLock   = m_xActionLock;
     124        2079 :             bool                                                  bLocked = m_bActionLocked;
     125             : 
     126        2079 :             m_xActionLock.clear();
     127        2079 :             m_bActionLocked = false;
     128             : 
     129        2079 :             aMutexLock.clear();
     130             :             // <- SAFE ..........................
     131             : 
     132        2079 :             if (bLocked && xLock.is())
     133        4158 :                 xLock->removeActionLock();
     134        2079 :         }
     135             : 
     136             :         /** @short  lock the internal wrapped resource, if its not already done. */
     137           0 :         virtual void lock()
     138             :         {
     139           0 :             osl::MutexGuard g(m_mutex);
     140           0 :             if (!m_bActionLocked && m_xActionLock.is())
     141             :             {
     142           0 :                 m_xActionLock->addActionLock();
     143           0 :                 m_bActionLocked = m_xActionLock->isActionLocked();
     144           0 :             }
     145           0 :         }
     146             : 
     147             :         /** @short  unlock the internal wrapped resource, if its not already done. */
     148        2079 :         virtual void unlock()
     149             :         {
     150        2079 :             osl::MutexGuard g(m_mutex);
     151        2079 :             if (m_bActionLocked && m_xActionLock.is())
     152             :             {
     153           0 :                 m_xActionLock->removeActionLock();
     154             :                 // dont check for any locks here ...
     155             :                 // May another guard use the same lock object :-(
     156           0 :                 m_bActionLocked = false;
     157        2079 :             }
     158        2079 :         }
     159             : };
     160             : 
     161             : } // namespace framework
     162             : 
     163             : #endif // INCLUDED_FRAMEWORK_SOURCE_INC_LOADENV_ACTIONLOCKGUARD_HXX
     164             : 
     165             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10