aboutsummaryrefslogtreecommitdiffstats
path: root/src/Chrono.hxx
blob: cc87c5ba1f89f2e97fe6fc0a1a6834f02d5b309f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * Copyright (C) 2003-2014 The Music Player Daemon Project
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef MPD_CHRONO_HXX
#define MPD_CHRONO_HXX

#include "Compiler.h"

#include <chrono>
#include <utility>
#include <cstdint>

#if defined(__GNUC__) && !GCC_CHECK_VERSION(4,7) && !defined(__clang__)
/* std::chrono::duration operators are "constexpr" since gcc 4.7 */
#define chrono_constexpr gcc_pure
#else
#define chrono_constexpr constexpr
#endif

/**
 * A time stamp within a song.  Granularity is 1 millisecond and the
 * maximum value is about 49 days.
 */
class SongTime : public std::chrono::duration<std::uint32_t, std::milli> {
	typedef std::chrono::duration<std::uint32_t, std::milli> Base;
	typedef Base::rep rep;

public:
	SongTime() = default;

	template<typename T>
	explicit constexpr SongTime(T t):Base(t) {}

	static constexpr SongTime zero() {
		return SongTime(Base::zero());
	}

	static constexpr SongTime FromS(unsigned s) {
		return SongTime(rep(s) * 1000);
	}

	static constexpr SongTime FromS(float s) {
		return SongTime(rep(s * 1000));
	}

	static constexpr SongTime FromS(double s) {
		return SongTime(rep(s * 1000));
	}

	static constexpr SongTime FromMS(rep ms) {
		return SongTime(ms);
	}

	constexpr rep ToS() const {
		return count() / rep(1000);
	}

	constexpr rep RoundS() const {
		return (count() + 500) / rep(1000);
	}

	constexpr rep ToMS() const {
		return count();
	}

	template<typename T=rep>
	constexpr T ToScale(unsigned scale) const {
		return count() * T(scale) / 1000;
	}

	/**
	 * Convert a scalar value with the given scale to a #SongTime
	 * instance.
	 *
	 * @param value the input value
	 * @param scale the value's scale in Hz
	 */
	template<typename T=rep>
	static constexpr SongTime FromScale(T value, unsigned scale) {
		return SongTime(value * T(1000) / T(scale));
	}

	constexpr double ToDoubleS() const {
		return double(count()) / 1000.;
	};

	constexpr bool IsZero() const {
		return count() == 0;
	}

	constexpr bool IsPositive() const {
		return count() > 0;
	}

	chrono_constexpr SongTime operator+(const SongTime &other) const {
		return SongTime(*(const Base *)this + (const Base &)other);
	}

	chrono_constexpr SongTime operator-(const SongTime &other) const {
		return SongTime(*(const Base *)this - (const Base &)other);
	}
};

/**
 * A variant of #SongTime that is based on a signed integer.  It can
 * be used for relative values.
 */
class SignedSongTime : public std::chrono::duration<std::int32_t, std::milli> {
	typedef std::chrono::duration<std::int32_t, std::milli> Base;
	typedef Base::rep rep;

public:
	SignedSongTime() = default;

	template<typename T>
	explicit constexpr SignedSongTime(T t):Base(t) {}

	/**
	 * Allow implicit conversion from SongTime to SignedSongTime.
	 */
	constexpr SignedSongTime(SongTime t):Base(t) {}

	static constexpr SignedSongTime zero() {
		return SignedSongTime(Base::zero());
	}

	/**
	 * Generate a negative value.
	 */
	static constexpr SignedSongTime Negative() {
		return SignedSongTime(-1);
	}

	static constexpr SignedSongTime FromS(int s) {
		return SignedSongTime(rep(s) * 1000);
	}

	static constexpr SignedSongTime FromS(unsigned s) {
		return SignedSongTime(rep(s) * 1000);
	}

	static constexpr SignedSongTime FromS(float s) {
		return SignedSongTime(rep(s * 1000));
	}

	static constexpr SignedSongTime FromS(double s) {
		return SignedSongTime(rep(s * 1000));
	}

	static constexpr SignedSongTime FromMS(rep ms) {
		return SignedSongTime(ms);
	}

	constexpr rep ToS() const {
		return count() / rep(1000);
	}

	constexpr rep RoundS() const {
		return (count() + 500) / rep(1000);
	}

	constexpr rep ToMS() const {
		return count();
	}

	template<typename T=rep>
	constexpr T ToScale(unsigned scale) const {
		return count() * T(scale) / 1000;
	}

	/**
	 * Convert a scalar value with the given scale to a
	 * #SignedSongTime instance.
	 *
	 * @param value the input value
	 * @param scale the value's scale in Hz
	 */
	template<typename T=rep>
	static constexpr SignedSongTime FromScale(T value, unsigned scale) {
		return SignedSongTime(value * T(1000) / T(scale));
	}

	constexpr double ToDoubleS() const {
		return double(count()) / 1000.;
	};

	constexpr bool IsZero() const {
		return count() == 0;
	}

	constexpr bool IsPositive() const {
		return count() > 0;
	}

	constexpr bool IsNegative() const {
		return count() < 0;
	}

	chrono_constexpr SignedSongTime operator+(const SignedSongTime &other) const {
		return SignedSongTime(*(const Base *)this + (const Base &)other);
	}

	chrono_constexpr SignedSongTime operator-(const SignedSongTime &other) const {
		return SignedSongTime(*(const Base *)this - (const Base &)other);
	}
};

#undef chrono_constexpr

#endif