1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
22 #include <com/sun/star/document/XActionLockable.hpp>
27 /** @short implements a guard, which can use the interface
28 <type scope="css::document">XActionLockable</type>.
30 @descr This guard should be used to be sure, that any lock will be
31 released. Otherwise the locked document can hinder the office on shutdown!
33 class ActionLockGuard final
41 /** @short points to the object, which can be locked from outside. */
42 css::uno::Reference
< css::document::XActionLockable
> m_xActionLock
;
44 /** @short knows if a lock exists on the internal lock object
45 forced by this guard instance. */
52 /** @short default ctor to initialize a "non working guard".
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 than one resources".
59 : m_bActionLocked(false)
63 /** @short release this guard instance and make sure, that no lock
64 will exist afterwards on the internal wrapped resource.
71 /** @short set a new resource for locking at this guard.
73 @descr This call will fail, if an internal resource already exists
74 and is currently locked.
77 points to the outside resource, which should be locked.
79 @return sal_True, if new resource could be set and locked.
82 bool setResource(const css::uno::Reference
< css::document::XActionLockable
>& xLock
)
84 std::unique_lock
g(m_mutex
);
86 if (m_bActionLocked
|| !xLock
.is())
89 m_xActionLock
= xLock
;
90 m_xActionLock
->addActionLock();
91 m_bActionLocked
= m_xActionLock
->isActionLocked();
96 /** @short set a new resource for locking at this guard.
98 @descr This call will fail, if an internal resource already exists
99 and is currently locked.
102 points to the outside resource, which should be locked.
104 @return sal_True, if new resource could be set and locked.
109 // SAFE -> ..........................
110 std::unique_lock
aMutexLock(m_mutex
);
112 css::uno::Reference
< css::document::XActionLockable
> xLock
= m_xActionLock
;
113 bool bLocked
= m_bActionLocked
;
115 m_xActionLock
.clear();
116 m_bActionLocked
= false;
119 // <- SAFE ..........................
121 if (bLocked
&& xLock
.is())
122 xLock
->removeActionLock();
125 /** @short unlock the internal wrapped resource, if it's not already done. */
128 std::unique_lock
g(m_mutex
);
129 if (m_bActionLocked
&& m_xActionLock
.is())
131 m_xActionLock
->removeActionLock();
132 // don't check for any locks here ...
133 // May another guard use the same lock object :-(
134 m_bActionLocked
= false;
139 } // namespace framework
141 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */