crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
StatusSetter.hpp
Go to the documentation of this file.
1 /*
2  *
3  * ---
4  *
5  * Copyright (C) 2022 Anselm Schmidt (ans[ät]ohai.su)
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version in addition to the terms of any
11  * licences already herein identified.
12  *
13  * This program 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 General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  *
21  * ---
22  *
23  * StatusSetter.hpp
24  *
25  * Allows the receiving function to update the status of the current thread.
26  *
27  * Created on: Aug 2, 2020
28  * Author: ans
29  */
30 
31 #ifndef STRUCT_STATUSSETTER_HPP_
32 #define STRUCT_STATUSSETTER_HPP_
33 
34 #include "../Helper/CommaLocale.hpp"
35 
36 #include <cstddef> // std::size_t
37 #include <functional> // std::function
38 #include <ios> // std::fixed
39 #include <sstream> // std::ostringstream
40 #include <string> // std::string
41 #include <utility> // std::move
42 
43 namespace crawlservpp::Struct {
44 
47 
49  inline constexpr auto precisionProgress{2};
50 
52  inline constexpr auto percentageFactor{100};
53 
55 
57  struct StatusSetter {
60 
62  std::string currentStatus;
63 
65 
70 
74 
76  std::function<void(const std::string&)> callbackSetStatus;
77 
79  std::function<void(float)> callbackSetProgress;
80 
82  std::function<bool()> callbackIsRunning;
83 
87 
89 
118  const std::string& setCurrentStatus,
119  float setProgressAfter,
120  std::function<void(const std::string&)> callbackToSetStatus,
121  std::function<void(float)> callbackToSetProgress,
122  std::function<bool()> callbackToCheckIsRunning
123  ) : currentStatus(setCurrentStatus),
124  progressAfter(setProgressAfter),
125  callbackSetStatus(std::move(callbackToSetStatus)),
126  callbackSetProgress(std::move(callbackToSetProgress)),
127  callbackIsRunning(std::move(callbackToCheckIsRunning)) {
128  if(this->callbackIsRunning()) {
129  this->callbackSetStatus(this->currentStatus);
130  this->callbackSetProgress(0.F);
131  }
132  }
133 
135 
143  bool change(const std::string& statusMessage) { //NOLINT(modernize-use-nodiscard)
144  this->currentStatus = statusMessage;
145  this->callbackSetStatus(statusMessage);
146 
147  this->callbackSetProgress(0.F);
148 
149  return this->callbackIsRunning();
150  }
151 
153 
161  bool update(std::size_t done, std::size_t total) const { //NOLINT(modernize-use-nodiscard)
162  std::ostringstream statusStrStr;
163 
164  statusStrStr.imbue(Helper::CommaLocale::locale());
165 
166  statusStrStr << this->currentStatus;
167  statusStrStr << " [";
168  statusStrStr << done;
169  statusStrStr << "/";
170  statusStrStr << total;
171  statusStrStr << "]";
172 
173  this->callbackSetStatus(statusStrStr.str());
174  this->callbackSetProgress(static_cast<float>(done) / total);
175 
176  return this->callbackIsRunning();
177  }
178 
180 
192  bool update(float percentage, bool precise) const { //NOLINT(modernize-use-nodiscard)
193  std::ostringstream statusStrStr;
194 
195  if(precise) {
196  statusStrStr.precision(precisionProgress);
197  }
198  else {
199  statusStrStr.precision(0);
200  }
201 
202  statusStrStr << this->currentStatus;
203  statusStrStr << " [";
204  statusStrStr << std::fixed << percentage * percentageFactor;
205  statusStrStr << "%]";
206 
207  this->callbackSetStatus(statusStrStr.str());
208  this->callbackSetProgress(percentage);
209 
210  return this->callbackIsRunning();
211  }
212 
214 
227  bool update(std::size_t done, std::size_t total, bool precise) const { //NOLINT(modernize-use-nodiscard)
228  return this->update(static_cast<float>(done) / total, precise);
229  }
230 
232 
236  [[nodiscard]] bool isRunning() const {
237  return this->callbackIsRunning();
238  }
239 
241  void finish() const {
242  this->callbackSetStatus(this->currentStatus + " [done]");
243  this->callbackSetProgress(this->progressAfter);
244  }
245  };
246 
247 } /* namespace crawlservpp::Struct */
248 
249 #endif /* STRUCT_STATUSSETTER_HPP_ */
float progressAfter
The progress to which the thread will be (re-)set when the current task has been finished, in percent.
Definition: StatusSetter.hpp:69
std::function< bool()> callbackIsRunning
Callback function to check whether the current thread should still be running.
Definition: StatusSetter.hpp:82
constexpr auto precisionProgress
The precision (number of fractal digits) when showing the progress in percent.
Definition: StatusSetter.hpp:49
bool isRunning() const
Checks whether the thread is still supposed to run.
Definition: StatusSetter.hpp:236
bool update(std::size_t done, std::size_t total) const
Updates the status with a fractal progress.
Definition: StatusSetter.hpp:161
std::string currentStatus
The current status, to which the current progress will be added.
Definition: StatusSetter.hpp:62
bool update(std::size_t done, std::size_t total, bool precise) const
Calculates the current percentage and updates the status accordingly.
Definition: StatusSetter.hpp:227
bool change(const std::string &statusMessage)
Changes the status message and resets the current progress.
Definition: StatusSetter.hpp:143
static std::locale locale()
Definition: CommaLocale.hpp:44
constexpr auto percentageFactor
The factor for converting a fractal into a percentage.
Definition: StatusSetter.hpp:52
Structure containing all the data needed to keep the status of a thread updated.
Definition: StatusSetter.hpp:57
bool update(float percentage, bool precise) const
Updates the status with a percentage.
Definition: StatusSetter.hpp:192
std::function< void(float)> callbackSetProgress
Callback function to update the progress of the current thread, in percent.
Definition: StatusSetter.hpp:79
StatusSetter(const std::string &setCurrentStatus, float setProgressAfter, std::function< void(const std::string &)> callbackToSetStatus, std::function< void(float)> callbackToSetProgress, std::function< bool()> callbackToCheckIsRunning)
Constructor setting the initial status.
Definition: StatusSetter.hpp:117
void finish() const
Re-sets the progress of the thread.
Definition: StatusSetter.hpp:241
Namespace for data structures.
Definition: AlgoThreadProperties.hpp:43
std::function< void(const std::string &)> callbackSetStatus
Callback function to update the status message of the current thread.
Definition: StatusSetter.hpp:76