Debian Codegraph @

src/App/AutoTransaction.h

raw
1 /****************************************************************************
2 * Copyright (c) 2019 Zheng Lei (realthunder) <[email protected]> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ****************************************************************************/
22
23 #ifndef APP_AUTOTRANSACTION_H
24 #define APP_AUTOTRANSACTION_H
25
26 #include <cstddef>
27 #include <FCGlobal.h>
28
29 namespace App
30 {
31
32 class Application;
33
34 /**
35 * @brief A helper class to manage transactions (i.e. undo/redo).
36 *
37 * An AutoTransaction object is meant to be allocated on the stack and governs
38 * the transactions in that scope.
39 */
40 class AppExport AutoTransaction
41 {
42 public:
43 /// Delete the new operator to prevent heap allocation.
44 void* operator new(std::size_t) = delete;
45
46 public:
47 /**
48 * @brief Construct an auto transaction.
49 *
50 * @param[in] name: optional new transaction name on construction
51 * @param[in] tmpName: if true and a new transaction is setup, the name given is
52 * considered as temporary, and subsequent construction of this class (or
53 * calling Application::setActiveTransaction()) can override the transaction
54 * name.
55 *
56 * The constructor increments an internal counter
57 * (Application::_activeTransactionGuard). The counter prevents any new
58 * active transactions being setup. It also prevents to close
59 * (i.e. commits) the current active transaction until it reaches zero. It
60 * does not have any effect on aborting transactions though.
61 */
62 AutoTransaction(const char* name = nullptr, bool tmpName = false);
63
64 /**
65 * @brief Destruct an auto transaction.
66 *
67 * This destructor decrease an internal counter
68 * (Application::_activeTransactionGuard), and will commit any current
69 * active transaction when the counter reaches zero.
70 */
71 ~AutoTransaction();
72
73 /**
74 * @brief Close or abort the transaction.
75 *
76 * This function can be used to explicitly close (i.e. commit) the
77 * transaction, if the current transaction ID matches the one created inside
78 * the constructor. For aborting, it will abort any current transaction.
79 *
80 * @param[in] abort: if true, abort the transaction; otherwise, commit it.
81 */
82 void close(bool abort = false);
83
84 /**
85 * @brief Enable/Disable any AutoTransaction instance on the current stack.
86 *
87 * Once disabled, any empty temporary named transaction is closed. If there
88 * are non-empty or non-temporary named active transaction, it will not be
89 * auto closed.
90 *
91 * This function may be used in, for example, Gui::Document::setEdit() to
92 * allow a transaction live past any command scope.
93 *
94 * @param[in] enable: if true, enable the AutoTransaction; otherwise, disable it.
95 */
96 static void setEnable(bool enable);
97
98 private:
99 int tid = 0;
100 };
101
102
103 /**
104 * @brief Helper class to lock a transaction from being closed or aborted.
105 *
106 * The helper class is used to protect some critical transactions from being
107 * closed prematurely, e.g. when deleting some object.
108 */
109 class AppExport TransactionLocker
110 {
111 public:
112
113 /**
114 * @brief Construct a transaction locker.
115 *
116 * @param[in] lock: whether to activate the lock
117 */
118 TransactionLocker(bool lock = true);
119
120 /**
121 * @brief Destruct a transaction locker.
122 *
123 * Unlock the transaction if this locker is active
124 */
125 ~TransactionLocker();
126
127 /**
128 * @brief Activate or deactivate this locker.
129 *
130 * An internal counter is used to support recursive locker. When activated,
131 * the current active transaction cannot be closed or aborted. But the
132 * closing call (Application::closeActiveTransaction()) will be remembered,
133 * and performed when the internal lock counter reaches zero.
134 *
135 * @param enable: whether to activate the locker
136 */
137 void activate(bool enable);
138
139 /// Check if the locker is active.
140 bool isActive() const
141 {
142 return active;
143 }
144
145 /// Check if transaction is being locked.
146 static bool isLocked();
147
148 friend class Application;
149
150 public:
151 /// Delete the new operator to prevent heap allocation.
152 void* operator new(std::size_t) = delete;
153
154 private:
155 bool active;
156 };
157
158 } // namespace App
159
160 #endif // APP_AUTOTRANSACTION_H