crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
StartStopHR.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  * StartStopHR.hpp
24  *
25  * High resolution start/stop watch timer for getting the
26  * elapsed time in microseconds, including pausing functionality.
27  *
28  * Created on: Oct 17, 2018
29  * Author: ans
30  */
31 
32 #ifndef TIMER_STARTSTOPHR_HPP_
33 #define TIMER_STARTSTOPHR_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 StartStopHR {
53  public:
56 
57  StartStopHR();
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::high_resolution_clock::time_point timePoint;
91 
93 
97  std::chrono::high_resolution_clock::duration duration;
98 
100  };
101 
102  /*
103  * IMPLEMENTATION
104  */
105 
107 
114  : timePoint(std::chrono::high_resolution_clock::time_point::min()),
115  duration(std::chrono::high_resolution_clock::duration::zero()) {}
116 
118 
126  inline void StartStopHR::start() {
127  if(this->timePoint != std::chrono::high_resolution_clock::time_point::min()) {
128  this->stop();
129  }
130 
132  }
133 
135 
140  inline void StartStopHR::stop() {
142 
143  this->timePoint = std::chrono::high_resolution_clock::time_point::min();
144  }
145 
147 
150  inline void StartStopHR::reset() {
151  this->timePoint = std::chrono::high_resolution_clock::time_point::min();
152  this->duration = std::chrono::high_resolution_clock::duration::zero();
153  }
154 
156 
165  inline std::string StartStopHR::totalStr() {
166  if(
167  this->timePoint !=
168  std::chrono::high_resolution_clock::time_point::min()
169  ) {
170  this->stop();
171  }
172 
174  std::chrono::duration_cast<std::chrono::microseconds>(
175  this->duration
176  ).count()
177  );
178  }
179 
181  inline void StartStopHR::clear() {
182  this->timePoint = std::chrono::high_resolution_clock::time_point{};
183  this->duration = std::chrono::high_resolution_clock::duration{};
184  }
185 
186 } /* namespace crawlservpp::Timer */
187 
188 #endif /* TIMER_STARTSTOPHR_HPP_ */
std::string microsecondsToString(std::uint64_t microseconds)
Converts microseconds into a well-formatted string.
Definition: DateTime.hpp:859
StartStopHR()
Constructor initializing the values.
Definition: StartStopHR.hpp:113
void reset()
Resets the timer.
Definition: StartStopHR.hpp:150
std::chrono::high_resolution_clock::duration duration
Duration of previous runs.
Definition: StartStopHR.hpp:97
std::string now()
Formats the current date/time as string in the format YYYY-MM-DD HH:MM:SS.
Definition: DateTime.hpp:1045
std::string totalStr()
Gets the total duration as formatted string.
Definition: StartStopHR.hpp:165
Namespace for timers.
Definition: Simple.hpp:40
void start()
Starts the timer.
Definition: StartStopHR.hpp:126
void clear()
Resets the internal state of the timer.
Definition: StartStopHR.hpp:181
std::chrono::high_resolution_clock::time_point timePoint
(Time) point of start.
Definition: StartStopHR.hpp:90
void stop()
Stops the timer.
Definition: StartStopHR.hpp:140
A simple start/stop watch with high resolution.
Definition: StartStopHR.hpp:52