fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / framework / source / inc / loadenv / actionlockguard.hxx
blobcaa70314a7e656ffa25409d33dec27c622740534
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
26 namespace framework{
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!
34 class ActionLockGuard
37 // member
39 private:
40 osl::Mutex m_mutex;
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. */
47 bool m_bActionLocked;
49 // interface
51 public:
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".
59 ActionLockGuard()
60 : m_bActionLocked(false)
64 /** @short initialize new guard instance and lock the given resource immediately.
66 @param xLock
67 points to the outside resource, which should be locked.
69 ActionLockGuard(const css::uno::Reference< css::document::XActionLockable >& xLock)
70 : m_bActionLocked(false)
72 setResource(xLock);
75 /** @short release this guard instance and make sure, that no lock
76 will exist afterwards on the internal wrapped resource.
78 virtual ~ActionLockGuard()
80 unlock();
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.
88 @param xLock
89 points to the outside resource, which should be locked.
91 @return sal_True, if new resource could be set and locked.
92 sal_False otherwise.
94 bool setResource(const css::uno::Reference< css::document::XActionLockable >& xLock)
96 osl::MutexGuard g(m_mutex);
98 if (m_bActionLocked || !xLock.is())
99 return false;
101 m_xActionLock = xLock;
102 m_xActionLock->addActionLock();
103 m_bActionLocked = m_xActionLock->isActionLocked();
105 return true;
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.
113 @param xLock
114 points to the outside resource, which should be locked.
116 @return sal_True, if new resource could be set and locked.
117 sal_False otherwise.
119 void freeResource()
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;
130 aMutexLock.clear();
131 // <- SAFE ..........................
133 if (bLocked && xLock.is())
134 xLock->removeActionLock();
137 /** @short lock the internal wrapped resource, if its not already done. */
138 void lock()
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. */
149 void unlock()
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: */