MPD  0.20.18
PeriodClock.hxx
Go to the documentation of this file.
1 /*
2  * Copyright 2003-2017 The Music Player Daemon Project
3  * http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef MPD_PERIOD_CLOCK_HXX
21 #define MPD_PERIOD_CLOCK_HXX
22 
23 #include <chrono>
24 
29 class PeriodClock {
30 public:
31  typedef std::chrono::steady_clock::duration Duration;
32  typedef Duration Delta;
33  typedef std::chrono::steady_clock::time_point Stamp;
34 
35 private:
36  Stamp last;
37 
38 public:
45  constexpr
46  PeriodClock():last() {}
47 
48 protected:
49  static Stamp GetNow() {
50  return std::chrono::steady_clock::now();
51  }
52 
53  constexpr Delta Elapsed(Stamp now) const {
54  return last == Stamp()
55  ? Delta(-1)
56  : Delta(now - last);
57  }
58 
59  constexpr bool Check(Stamp now, Duration duration) const {
60  return now >= last + duration;
61  }
62 
63  void Update(Stamp now) {
64  last = now;
65  }
66 
67 public:
68  constexpr bool IsDefined() const {
69  return last > Stamp();
70  }
71 
75  void Reset() {
76  last = Stamp();
77  }
78 
83  Delta Elapsed() const {
84  return Elapsed(GetNow());
85  }
86 
91  const auto now = GetNow();
92  const auto result = Elapsed(now);
93  Update(now);
94  return result;
95  }
96 
103  bool Check(Duration duration) const {
104  return Check(GetNow(), duration);
105  }
106 
110  void Update() {
111  Update(GetNow());
112  }
113 
118  void UpdateWithOffset(Delta offset) {
119  Update(GetNow() + offset);
120  }
121 
128  bool CheckUpdate(Duration duration) {
129  Stamp now = GetNow();
130  if (Check(now, duration)) {
131  Update(now);
132  return true;
133  } else
134  return false;
135  }
136 
143  bool CheckAlwaysUpdate(Duration duration) {
144  Stamp now = GetNow();
145  bool ret = Check(now, duration);
146  Update(now);
147  return ret;
148  }
149 };
150 
151 #endif
constexpr bool Check(Stamp now, Duration duration) const
Definition: PeriodClock.hxx:59
constexpr PeriodClock()
Initializes the object, setting the last time stamp to "0", i.e.
Definition: PeriodClock.hxx:46
Delta Elapsed() const
Returns the time elapsed since the last update().
Definition: PeriodClock.hxx:83
bool CheckUpdate(Duration duration)
Checks whether the specified duration has passed since the last update.
std::chrono::steady_clock::duration Duration
Definition: PeriodClock.hxx:31
bool Check(Duration duration) const
Checks whether the specified duration has passed since the last update.
std::chrono::steady_clock::time_point Stamp
Definition: PeriodClock.hxx:33
void Update()
Updates the time stamp, setting it to the current clock.
Delta ElapsedUpdate()
Combines a call to Elapsed() and Update().
Definition: PeriodClock.hxx:90
void Update(Stamp now)
Definition: PeriodClock.hxx:63
constexpr bool IsDefined() const
Definition: PeriodClock.hxx:68
void Reset()
Resets the clock.
Definition: PeriodClock.hxx:75
bool CheckAlwaysUpdate(Duration duration)
Checks whether the specified duration has passed since the last update.
static Stamp GetNow()
Definition: PeriodClock.hxx:49
This is a stopwatch which saves the timestamp of an event, and can check whether a specified time spa...
Definition: PeriodClock.hxx:29
Duration Delta
Definition: PeriodClock.hxx:32
void UpdateWithOffset(Delta offset)
Updates the time stamp, setting it to the current clock plus the specified offset.
constexpr Delta Elapsed(Stamp now) const
Definition: PeriodClock.hxx:53