Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

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