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 .
20 #ifndef INCLUDED_FRAMEWORK_SOURCE_INC_LOADENV_ACTIONLOCKGUARD_HXX
21 #define INCLUDED_FRAMEWORK_SOURCE_INC_LOADENV_ACTIONLOCKGUARD_HXX
23 #include <com/sun/star/document/XActionLockable.hpp>
24 #include <osl/mutex.hxx>
28 /** @short implements a guard, which can use the interface
29 <type scope="com::sun::star::document">XActionLockable</type>.
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!
42 /** @short points to the object, which can be locked from outside. */
43 css::uno::Reference
< css::document::XActionLockable
> m_xActionLock
;
45 /** @short knows if a lock exists on the internal lock object
46 forced by this guard instance. */
53 /** @short default ctor to initialize a "non working guard".
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".
60 : m_bActionLocked(false)
64 /** @short initialize new guard instance and lock the given resource immediately.
67 points to the outside resource, which should be locked.
69 ActionLockGuard(const css::uno::Reference
< css::document::XActionLockable
>& xLock
)
70 : m_bActionLocked(false)
75 /** @short release this guard instance and make sure, that no lock
76 will exist afterwards on the internal wrapped resource.
78 virtual ~ActionLockGuard()
83 /** @short set a new resource for locking at this guard.
85 @descr This call will fail, if an internal resource already exists
86 and is currently locked.
89 points to the outside resource, which should be locked.
91 @return sal_True, if new resource could be set and locked.
94 bool setResource(const css::uno::Reference
< css::document::XActionLockable
>& xLock
)
96 osl::MutexGuard
g(m_mutex
);
98 if (m_bActionLocked
|| !xLock
.is())
101 m_xActionLock
= xLock
;
102 m_xActionLock
->addActionLock();
103 m_bActionLocked
= m_xActionLock
->isActionLocked();
108 /** @short set a new resource for locking at this guard.
110 @descr This call will fail, if an internal resource already exists
111 and is currently locked.
114 points to the outside resource, which should be locked.
116 @return sal_True, if new resource could be set and locked.
121 // SAFE -> ..........................
122 osl::ClearableMutexGuard
aMutexLock(m_mutex
);
124 css::uno::Reference
< css::document::XActionLockable
> xLock
= m_xActionLock
;
125 bool bLocked
= m_bActionLocked
;
127 m_xActionLock
.clear();
128 m_bActionLocked
= false;
131 // <- SAFE ..........................
133 if (bLocked
&& xLock
.is())
134 xLock
->removeActionLock();
137 /** @short lock the internal wrapped resource, if its not already done. */
140 osl::MutexGuard
g(m_mutex
);
141 if (!m_bActionLocked
&& m_xActionLock
.is())
143 m_xActionLock
->addActionLock();
144 m_bActionLocked
= m_xActionLock
->isActionLocked();
148 /** @short unlock the internal wrapped resource, if its not already done. */
151 osl::MutexGuard
g(m_mutex
);
152 if (m_bActionLocked
&& m_xActionLock
.is())
154 m_xActionLock
->removeActionLock();
155 // dont check for any locks here ...
156 // May another guard use the same lock object :-(
157 m_bActionLocked
= false;
162 } // namespace framework
164 #endif // INCLUDED_FRAMEWORK_SOURCE_INC_LOADENV_ACTIONLOCKGUARD_HXX
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */