Debian Codegraph @

src/Base/Sequencer.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Library General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Library General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Library General Public *
19 * License along with this library; see the file COPYING.LIB. If not, *
20 * write to the Free Software Foundation, Inc., 59 Temple Place, *
21 * Suite 330, Boston, MA 02111-1307, USA *
22 * *
23 ***************************************************************************/
24
25 #include <mutex>
26 #include <vector>
27 #include <algorithm>
28 #include <cstdio>
29
30 #include "Sequencer.h"
31
32 using namespace Base;
33
34 namespace Base
35 {
36 struct SequencerP
37 {
38 // members
39 static std::vector<SequencerBase*> _instances; /**< A vector of all created instances */
40 static SequencerLauncher* _topLauncher; /**< The outermost launcher */
41 static std::recursive_mutex mutex; /**< A mutex-locker for the launcher */
42 /** Sets a global sequencer object.
43 * Access to the last registered object is performed by @see Sequencer().
44 */
45 static void appendInstance(SequencerBase* sb)
46 {
47 _instances.push_back(sb);
48 }
49 static void removeInstance(SequencerBase* sb)
50 {
51 const auto it = std::ranges::find(_instances, sb);
52 _instances.erase(it);
53 }
54 static SequencerBase& getInstance()
55 {
56 return *_instances.back();
57 }
58 };
59
60 /**
61 * The _instances member just stores the pointer of the
62 * all instantiated SequencerBase objects.
63 */
64 std::vector<SequencerBase*> SequencerP::_instances;
65 SequencerLauncher* SequencerP::_topLauncher = nullptr;
66 std::recursive_mutex SequencerP::mutex;
67 } // namespace Base
68
69 SequencerBase& SequencerBase::Instance()
70 {
71 // not initialized?
72 if (SequencerP::_instances.empty()) {
73 new ConsoleSequencer();
74 }
75
76 return SequencerP::getInstance();
77 }
78
79 SequencerBase::SequencerBase()
80 {
81 SequencerP::appendInstance(this);
82 }
83
84 SequencerBase::~SequencerBase()
85 {
86 SequencerP::removeInstance(this);
87 }
88
89 bool SequencerBase::start(const char* pszStr, size_t steps)
90 {
91 // reset current state of progress (in percent)
92 this->_nLastPercentage = -1;
93
94 this->nTotalSteps = steps;
95 this->nProgress = 0;
96 this->_bCanceled = false;
97
98 setText(pszStr);
99
100 // reimplemented in sub-classes
101 if (!this->_bLocked) {
102 startStep();
103 }
104
105 return true;
106 }
107
108 size_t SequencerBase::numberOfSteps() const
109 {
110 return this->nTotalSteps;
111 }
112
113 void SequencerBase::startStep()
114 {}
115
116 bool SequencerBase::next(bool canAbort)
117 {
118 this->nProgress++;
119 float fDiv = this->nTotalSteps > 0 ? static_cast<float>(this->nTotalSteps) : 1000.0F;
120 int perc = int((float(this->nProgress) * (100.0F / fDiv)));
121
122 // do only an update if we have increased by one percent
123 if (perc > this->_nLastPercentage) {
124 this->_nLastPercentage = perc;
125
126 // if not locked
127 if (!this->_bLocked) {
128 nextStep(canAbort);
129 }
130 }
131
132 return this->nProgress < this->nTotalSteps;
133 }
134
135 void SequencerBase::nextStep(bool /*next*/)
136 {}
137
138 void SequencerBase::setProgress(size_t /*value*/)
139 {}
140
141 bool SequencerBase::stop()
142 {
143 resetData();
144 return true;
145 }
146
147 void SequencerBase::pause()
148 {}
149
150 void SequencerBase::resume()
151 {}
152
153 bool SequencerBase::isBlocking() const
154 {
155 return true;
156 }
157
158 bool SequencerBase::setLocked(bool bLocked)
159 {
160 std::lock_guard<std::recursive_mutex> locker(SequencerP::mutex);
161 bool old = this->_bLocked;
162 this->_bLocked = bLocked;
163 return old;
164 }
165
166 bool SequencerBase::isLocked() const
167 {
168 std::lock_guard<std::recursive_mutex> locker(SequencerP::mutex);
169 return this->_bLocked;
170 }
171
172 bool SequencerBase::isRunning() const
173 {
174 std::lock_guard<std::recursive_mutex> locker(SequencerP::mutex);
175 return (SequencerP::_topLauncher != nullptr);
176 }
177
178 bool SequencerBase::wasCanceled() const
179 {
180 std::lock_guard<std::recursive_mutex> locker(SequencerP::mutex);
181 return this->_bCanceled;
182 }
183
184 void SequencerBase::tryToCancel()
185 {
186 this->_bCanceled = true;
187 }
188
189 void SequencerBase::rejectCancel()
190 {
191 this->_bCanceled = false;
192 }
193
194 int SequencerBase::progressInPercent() const
195 {
196 return this->_nLastPercentage;
197 }
198
199 void SequencerBase::resetData()
200 {
201 this->_bCanceled = false;
202 }
203
204 void SequencerBase::setText(const char* /*text*/)
205 {}
206
207 // ---------------------------------------------------------
208
209 using Base::ConsoleSequencer;
210
211 void ConsoleSequencer::setText(const char* pszTxt)
212 {
213 printf("%s...\n", pszTxt);
214 }
215
216 void ConsoleSequencer::startStep()
217 {}
218
219 void ConsoleSequencer::nextStep(bool /*canAbort*/)
220 {
221 if (this->nTotalSteps != 0) {
222 printf("\t\t\t\t\t\t(%d %%)\t\r", progressInPercent());
223 }
224 }
225
226 void ConsoleSequencer::resetData()
227 {
228 SequencerBase::resetData();
229 printf("\t\t\t\t\t\t\t\t\r");
230 }
231
232 // ---------------------------------------------------------
233
234 SequencerLauncher::SequencerLauncher(const char* pszStr, size_t steps)
235 {
236 std::lock_guard<std::recursive_mutex> locker(SequencerP::mutex);
237 // Have we already an instance of SequencerLauncher created?
238 if (!SequencerP::_topLauncher) {
239 SequencerBase::Instance().start(pszStr, steps);
240 SequencerP::_topLauncher = this;
241 }
242 }
243
244 SequencerLauncher::~SequencerLauncher()
245 {
246 std::lock_guard<std::recursive_mutex> locker(SequencerP::mutex);
247 if (SequencerP::_topLauncher == this) {
248 SequencerBase::Instance().stop();
249 SequencerP::_topLauncher = nullptr;
250 }
251 }
252
253 void SequencerLauncher::setText(const char* pszTxt)
254 {
255 std::lock_guard<std::recursive_mutex> locker(SequencerP::mutex);
256 SequencerBase::Instance().setText(pszTxt);
257 }
258
259 bool SequencerLauncher::next(bool canAbort)
260 {
261 std::lock_guard<std::recursive_mutex> locker(SequencerP::mutex);
262 if (SequencerP::_topLauncher != this) {
263 return true; // ignore
264 }
265 return SequencerBase::Instance().next(canAbort);
266 }
267
268 void SequencerLauncher::setProgress(size_t pos)
269 {
270 std::lock_guard<std::recursive_mutex> locker(SequencerP::mutex);
271 SequencerBase::Instance().setProgress(pos);
272 }
273
274 size_t SequencerLauncher::numberOfSteps() const
275 {
276 std::lock_guard<std::recursive_mutex> locker(SequencerP::mutex);
277 return SequencerBase::Instance().numberOfSteps();
278 }
279
280 bool SequencerLauncher::wasCanceled() const
281 {
282 return SequencerBase::Instance().wasCanceled();
283 }