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 sure, 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 than one resources".
57 : */
58 5594 : ActionLockGuard()
59 5594 : : m_bActionLocked(false)
60 : {
61 5594 : }
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 sure, that no lock
75 : will exist afterwards on the internal wrapped resource.
76 : */
77 5594 : virtual ~ActionLockGuard()
78 11188 : {
79 5594 : unlock();
80 5594 : }
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 5576 : virtual bool setResource(const css::uno::Reference< css::document::XActionLockable >& xLock)
94 : {
95 5576 : osl::MutexGuard g(m_mutex);
96 :
97 5576 : if (m_bActionLocked || !xLock.is())
98 0 : return false;
99 :
100 5576 : m_xActionLock = xLock;
101 5576 : m_xActionLock->addActionLock();
102 5576 : m_bActionLocked = m_xActionLock->isActionLocked();
103 :
104 5576 : 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 5594 : virtual void freeResource()
119 : {
120 : // SAFE -> ..........................
121 5594 : osl::ClearableMutexGuard aMutexLock(m_mutex);
122 :
123 11188 : css::uno::Reference< css::document::XActionLockable > xLock = m_xActionLock;
124 5594 : bool bLocked = m_bActionLocked;
125 :
126 5594 : m_xActionLock.clear();
127 5594 : m_bActionLocked = false;
128 :
129 5594 : aMutexLock.clear();
130 : // <- SAFE ..........................
131 :
132 5594 : if (bLocked && xLock.is())
133 11170 : xLock->removeActionLock();
134 5594 : }
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 5594 : virtual void unlock()
149 : {
150 5594 : osl::MutexGuard g(m_mutex);
151 5594 : 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 5594 : }
158 5594 : }
159 : };
160 :
161 : } // namespace framework
162 :
163 : #endif // INCLUDED_FRAMEWORK_SOURCE_INC_LOADENV_ACTIONLOCKGUARD_HXX
164 :
165 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|