LibreOffice Module framework (master) 1
actionlockguard.hxx
Go to the documentation of this file.
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#pragma once
21
22#include <com/sun/star/document/XActionLockable.hpp>
23#include <mutex>
24
25namespace framework{
26
33class ActionLockGuard final
34{
35
36 // member
37
38 private:
39 std::mutex m_mutex;
40
42 css::uno::Reference< css::document::XActionLockable > m_xActionLock;
43
47
48 // interface
49
50 public:
51
59 : m_bActionLocked(false)
60 {
61 }
62
67 {
68 unlock();
69 }
70
82 bool setResource(const css::uno::Reference< css::document::XActionLockable >& xLock)
83 {
84 std::unique_lock g(m_mutex);
85
86 if (m_bActionLocked || !xLock.is())
87 return false;
88
89 m_xActionLock = xLock;
90 m_xActionLock->addActionLock();
91 m_bActionLocked = m_xActionLock->isActionLocked();
92
93 return true;
94 }
95
108 {
109 // SAFE -> ..........................
110 std::unique_lock aMutexLock(m_mutex);
111
112 css::uno::Reference< css::document::XActionLockable > xLock = m_xActionLock;
113 bool bLocked = m_bActionLocked;
114
115 m_xActionLock.clear();
116 m_bActionLocked = false;
117
118 aMutexLock.unlock();
119 // <- SAFE ..........................
120
121 if (bLocked && xLock.is())
122 xLock->removeActionLock();
123 }
124
126 void unlock()
127 {
128 std::unique_lock g(m_mutex);
129 if (m_bActionLocked && m_xActionLock.is())
130 {
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;
135 }
136 }
137};
138
139} // namespace framework
140
141/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
implements a guard, which can use the interface <type scope="css::document">XActionLockable</type>.
css::uno::Reference< css::document::XActionLockable > m_xActionLock
points to the object, which can be locked from outside.
bool m_bActionLocked
knows if a lock exists on the internal lock object forced by this guard instance.
~ActionLockGuard()
release this guard instance and make sure, that no lock will exist afterwards on the internal wrapped...
void unlock()
unlock the internal wrapped resource, if it's not already done.
void freeResource()
set a new resource for locking at this guard.
ActionLockGuard()
default ctor to initialize a "non working guard".
bool setResource(const css::uno::Reference< css::document::XActionLockable > &xLock)
set a new resource for locking at this guard.