crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
StartStop.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  * StartStop.hpp
24  *
25  * Start/stop watch timer for getting the elapsed time
26  * in milliseconds, including pausing functionality.
27  *
28  * Created on: Oct 17, 2018
29  * Author: ans
30  */
31 
32 #ifndef TIMER_STARTSTOP_HPP_
33 #define TIMER_STARTSTOP_HPP_
34 
35 #include "../Helper/DateTime.hpp"
36 
37 #include <chrono> // std::chrono
38 #include <string> // std::string
39 
40 namespace crawlservpp::Timer {
41 
42  /*
43  * DECLARATION
44  */
45 
47 
52  class StartStop {
53  public:
56 
57  StartStop();
58 
62 
63  void start();
64  void stop();
65  void reset();
66 
70 
71  std::string totalStr();
72 
76 
77  void clear();
78 
80 
81  protected:
84 
86 
90  std::chrono::steady_clock::time_point timePoint;
91 
93 
97  std::chrono::steady_clock::duration duration;
98 
100  };
101 
102  /*
103  * IMPLEMENTATION
104  */
105 
107 
114  : timePoint(std::chrono::steady_clock::time_point::min()),
115  duration(std::chrono::steady_clock::duration::zero()) {}
116 
118 
126  inline void StartStop::start() {
127  if(this->timePoint != std::chrono::steady_clock::time_point::min()) {
128  this->stop();
129  }
130 
132  }
133 
135 
140  inline void StartStop::stop() {
142 
143  this->timePoint = std::chrono::steady_clock::time_point::min();
144  }
145 
147 
150  inline void StartStop::reset() {
151  this->timePoint = std::chrono::steady_clock::time_point::min();
152  this->duration = std::chrono::steady_clock::duration::zero();
153  }
154 
156 
165  inline std::string StartStop::totalStr() {
166  if(this->timePoint != std::chrono::steady_clock::time_point::min()) {
167  this->stop();
168  }
169 
171  std::chrono::duration_cast<std::chrono::milliseconds>(
172  this->duration
173  ).count()
174  );
175  }
176 
178  inline void StartStop::clear() {
179  this->timePoint = std::chrono::steady_clock::time_point{};
180  this->duration = std::chrono::steady_clock::duration{};
181  }
182 
183 } /* namespace crawlservpp::Timer */
184 
185 #endif /* TIMER_STARTSTOPHR_HPP_ */
std::chrono::steady_clock::time_point timePoint
(Time) point of start.
Definition: StartStop.hpp:90
void clear()
Resets the internal state of the timer.
Definition: StartStop.hpp:178
void stop()
Stops the timer.
Definition: StartStop.hpp:140
std::string totalStr()
Gets the total duration as formatted string.
Definition: StartStop.hpp:165
void reset()
Resets the timer.
Definition: StartStop.hpp:150
std::string now()
Formats the current date/time as string in the format YYYY-MM-DD HH:MM:SS.
Definition: DateTime.hpp:1045
Namespace for timers.
Definition: Simple.hpp:40
StartStop()
Constructor initializing the values.
Definition: StartStop.hpp:113
A simple start/stop watch.
Definition: StartStop.hpp:52
std::chrono::steady_clock::duration duration
Duration of previous runs.
Definition: StartStop.hpp:97
std::string millisecondsToString(std::uint64_t milliseconds)
Converts milliseconds into a well-formatted string.
Definition: DateTime.hpp:933
void start()
Starts the timer.
Definition: StartStop.hpp:126