PIVX Core  5.6.99
P2P Digital Currency
cxxtimer.h
Go to the documentation of this file.
1 /*
2 MIT License
3 Copyright (c) 2017 AndrĂ© L. Maravilha
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 The above copyright notice and this permission notice shall be included in all
11 copies or substantial portions of the Software.
12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18 SOFTWARE.
19 */
20 
21 /*
22  * https://github.com/andremaravilha/cxxtimer
23  * commit: a48bd81d9168a4e258123a84fc150912dd65ce79
24  */
25 
26 #ifndef CXX_TIMER_HPP
27 #define CXX_TIMER_HPP
28 
29 #include <chrono>
30 
31 
32 namespace cxxtimer {
33 
37 class Timer {
38 
39 public:
40 
48  explicit Timer(bool start = false);
49 
56  Timer(const Timer& other) = default;
57 
64  Timer(Timer&& other) = default;
65 
69  virtual ~Timer() = default;
70 
79  Timer& operator=(const Timer& other) = default;
80 
89  Timer& operator=(Timer&& other) = default;
90 
94  void start();
95 
99  void stop();
100 
104  void reset();
105 
116  template <class duration_t = std::chrono::milliseconds>
117  typename duration_t::rep count() const;
118 
119 private:
120 
121  bool started_;
122  bool paused_;
124  std::chrono::duration<long double> accumulated_;
125 };
126 
127 }
128 
129 
130 inline cxxtimer::Timer::Timer(bool start) :
131  started_(false), paused_(false),
132  reference_(std::chrono::steady_clock::now()),
133  accumulated_(std::chrono::duration<long double>(0)) {
134  if (start) {
135  this->start();
136  }
137 }
138 
139 inline void cxxtimer::Timer::start() {
140  if (!started_) {
141  started_ = true;
142  paused_ = false;
143  accumulated_ = std::chrono::duration<long double>(0);
144  reference_ = std::chrono::steady_clock::now();
145  } else if (paused_) {
146  reference_ = std::chrono::steady_clock::now();
147  paused_ = false;
148  }
149 }
150 
151 inline void cxxtimer::Timer::stop() {
152  if (started_ && !paused_) {
153  std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
154  accumulated_ = accumulated_ + std::chrono::duration_cast< std::chrono::duration<long double> >(now - reference_);
155  paused_ = true;
156  }
157 }
158 
159 inline void cxxtimer::Timer::reset() {
160  if (started_) {
161  started_ = false;
162  paused_ = false;
163  reference_ = std::chrono::steady_clock::now();
164  accumulated_ = std::chrono::duration<long double>(0);
165  }
166 }
167 
168 template <class duration_t>
169 typename duration_t::rep cxxtimer::Timer::count() const {
170  if (started_) {
171  if (paused_) {
172  return std::chrono::duration_cast<duration_t>(accumulated_).count();
173  } else {
174  return std::chrono::duration_cast<duration_t>(
175  accumulated_ + (std::chrono::steady_clock::now() - reference_)).count();
176  }
177  } else {
178  return duration_t(0).count();
179  }
180 }
181 
182 
183 #endif
false
Definition: bls_dkg.cpp:151
This class works as a stopwatch.
Definition: cxxtimer.h:37
Timer(Timer &&other)=default
Transfer constructor.
void start()
Start/resume the timer.
Definition: cxxtimer.h:139
std::chrono::duration< long double > accumulated_
Definition: cxxtimer.h:124
void reset()
Reset the timer.
Definition: cxxtimer.h:159
void stop()
Stop/pause the timer.
Definition: cxxtimer.h:151
Timer(bool start=false)
Constructor.
Definition: cxxtimer.h:130
Timer & operator=(const Timer &other)=default
Assignment operator by copy.
Timer(const Timer &other)=default
Copy constructor.
virtual ~Timer()=default
Destructor.
Timer & operator=(Timer &&other)=default
Assignment operator by transfer.
duration_t::rep count() const
Return the elapsed time.
Definition: cxxtimer.h:169
std::chrono::steady_clock::time_point reference_
Definition: cxxtimer.h:123
clock::time_point time_point
Definition: bench.h:48
clock::duration duration
Definition: bench.h:49
Definition: uint256.h:212