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

Generated by: LCOV version 1.11