MPD  0.20.15
Chrono.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_CHRONO_HXX
21 #define MPD_CHRONO_HXX
22 
23 #include <chrono>
24 #include <utility>
25 #include <cstdint>
26 
31 class SongTime : public std::chrono::duration<std::uint32_t, std::milli> {
32  typedef std::chrono::duration<std::uint32_t, std::milli> Base;
33  typedef Base::rep rep;
34 
35 public:
36  SongTime() = default;
37 
38  template<typename T>
39  explicit constexpr SongTime(T t):Base(t) {}
40 
41  static constexpr SongTime zero() {
42  return SongTime(Base::zero());
43  }
44 
45  static constexpr SongTime FromS(unsigned s) {
46  return SongTime(rep(s) * 1000);
47  }
48 
49  static constexpr SongTime FromS(float s) {
50  return SongTime(rep(s * 1000));
51  }
52 
53  static constexpr SongTime FromS(double s) {
54  return SongTime(rep(s * 1000));
55  }
56 
57  static constexpr SongTime FromMS(rep ms) {
58  return SongTime(ms);
59  }
60 
61  constexpr rep ToS() const {
62  return count() / rep(1000);
63  }
64 
65  constexpr rep RoundS() const {
66  return (count() + 500) / rep(1000);
67  }
68 
69  constexpr rep ToMS() const {
70  return count();
71  }
72 
73  template<typename T=rep>
74  constexpr T ToScale(unsigned scale) const {
75  return count() * T(scale) / 1000;
76  }
77 
85  template<typename T=rep>
86  static constexpr SongTime FromScale(T value, unsigned scale) {
87  return SongTime(value * T(1000) / T(scale));
88  }
89 
90  constexpr double ToDoubleS() const {
91  return double(count()) / 1000.;
92  };
93 
94  constexpr bool IsZero() const {
95  return count() == 0;
96  }
97 
98  constexpr bool IsPositive() const {
99  return count() > 0;
100  }
101 
102  constexpr SongTime operator+(const SongTime &other) const {
103  return SongTime(*(const Base *)this + (const Base &)other);
104  }
105 
106  constexpr SongTime operator-(const SongTime &other) const {
107  return SongTime(*(const Base *)this - (const Base &)other);
108  }
109 };
110 
115 class SignedSongTime : public std::chrono::duration<std::int32_t, std::milli> {
116  typedef std::chrono::duration<std::int32_t, std::milli> Base;
117  typedef Base::rep rep;
118 
119 public:
120  SignedSongTime() = default;
121 
122  template<typename T>
123  explicit constexpr SignedSongTime(T t):Base(t) {}
124 
128  constexpr SignedSongTime(SongTime t):Base(t) {}
129 
130  static constexpr SignedSongTime zero() {
131  return SignedSongTime(Base::zero());
132  }
133 
137  static constexpr SignedSongTime Negative() {
138  return SignedSongTime(-1);
139  }
140 
141  static constexpr SignedSongTime FromS(int s) {
142  return SignedSongTime(rep(s) * 1000);
143  }
144 
145  static constexpr SignedSongTime FromS(unsigned s) {
146  return SignedSongTime(rep(s) * 1000);
147  }
148 
149  static constexpr SignedSongTime FromS(float s) {
150  return SignedSongTime(rep(s * 1000));
151  }
152 
153  static constexpr SignedSongTime FromS(double s) {
154  return SignedSongTime(rep(s * 1000));
155  }
156 
157  static constexpr SignedSongTime FromMS(rep ms) {
158  return SignedSongTime(ms);
159  }
160 
161  constexpr rep ToS() const {
162  return count() / rep(1000);
163  }
164 
165  constexpr rep RoundS() const {
166  return (count() + 500) / rep(1000);
167  }
168 
169  constexpr rep ToMS() const {
170  return count();
171  }
172 
173  template<typename T=rep>
174  constexpr T ToScale(unsigned scale) const {
175  return count() * T(scale) / 1000;
176  }
177 
185  template<typename T=rep>
186  static constexpr SignedSongTime FromScale(T value, unsigned scale) {
187  return SignedSongTime(value * T(1000) / T(scale));
188  }
189 
190  constexpr double ToDoubleS() const {
191  return double(count()) / 1000.;
192  };
193 
194  constexpr bool IsZero() const {
195  return count() == 0;
196  }
197 
198  constexpr bool IsPositive() const {
199  return count() > 0;
200  }
201 
202  constexpr bool IsNegative() const {
203  return count() < 0;
204  }
205 
206  constexpr SignedSongTime operator+(const SignedSongTime &other) const {
207  return SignedSongTime(*(const Base *)this + (const Base &)other);
208  }
209 
210  constexpr SignedSongTime operator-(const SignedSongTime &other) const {
211  return SignedSongTime(*(const Base *)this - (const Base &)other);
212  }
213 };
214 
215 #endif
static constexpr SignedSongTime FromS(double s)
Definition: Chrono.hxx:153
constexpr SignedSongTime(T t)
Definition: Chrono.hxx:123
constexpr SongTime operator+(const SongTime &other) const
Definition: Chrono.hxx:102
constexpr rep ToS() const
Definition: Chrono.hxx:161
static constexpr SongTime zero()
Definition: Chrono.hxx:41
SongTime()=default
A time stamp within a song.
Definition: Chrono.hxx:31
static constexpr SongTime FromS(unsigned s)
Definition: Chrono.hxx:45
constexpr SongTime(T t)
Definition: Chrono.hxx:39
static constexpr SignedSongTime zero()
Definition: Chrono.hxx:130
constexpr SignedSongTime(SongTime t)
Allow implicit conversion from SongTime to SignedSongTime.
Definition: Chrono.hxx:128
constexpr T ToScale(unsigned scale) const
Definition: Chrono.hxx:74
constexpr rep RoundS() const
Definition: Chrono.hxx:65
constexpr bool IsZero() const
Definition: Chrono.hxx:94
constexpr rep RoundS() const
Definition: Chrono.hxx:165
constexpr rep ToMS() const
Definition: Chrono.hxx:169
static constexpr SignedSongTime FromMS(rep ms)
Definition: Chrono.hxx:157
static constexpr SignedSongTime FromS(int s)
Definition: Chrono.hxx:141
static constexpr SignedSongTime FromScale(T value, unsigned scale)
Convert a scalar value with the given scale to a SignedSongTime instance.
Definition: Chrono.hxx:186
static constexpr SongTime FromS(double s)
Definition: Chrono.hxx:53
static constexpr SignedSongTime Negative()
Generate a negative value.
Definition: Chrono.hxx:137
constexpr bool IsNegative() const
Definition: Chrono.hxx:202
static constexpr SongTime FromS(float s)
Definition: Chrono.hxx:49
static constexpr SongTime FromMS(rep ms)
Definition: Chrono.hxx:57
constexpr bool IsPositive() const
Definition: Chrono.hxx:98
constexpr double ToDoubleS() const
Definition: Chrono.hxx:190
constexpr SignedSongTime operator+(const SignedSongTime &other) const
Definition: Chrono.hxx:206
constexpr bool IsZero() const
Definition: Chrono.hxx:194
constexpr rep ToMS() const
Definition: Chrono.hxx:69
static constexpr SignedSongTime FromS(unsigned s)
Definition: Chrono.hxx:145
A variant of SongTime that is based on a signed integer.
Definition: Chrono.hxx:115
constexpr double ToDoubleS() const
Definition: Chrono.hxx:90
constexpr bool IsPositive() const
Definition: Chrono.hxx:198
constexpr rep ToS() const
Definition: Chrono.hxx:61
static constexpr SongTime FromScale(T value, unsigned scale)
Convert a scalar value with the given scale to a SongTime instance.
Definition: Chrono.hxx:86
SignedSongTime()=default
constexpr SongTime operator-(const SongTime &other) const
Definition: Chrono.hxx:106
constexpr SignedSongTime operator-(const SignedSongTime &other) const
Definition: Chrono.hxx:210
constexpr T ToScale(unsigned scale) const
Definition: Chrono.hxx:174
static constexpr SignedSongTime FromS(float s)
Definition: Chrono.hxx:149