Chrono.hxx 5.24 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * 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 <chrono>
24
#include <utility>
25 26
#include <cstdint>

27 28
using FloatDuration = std::chrono::duration<double>;

29 30 31 32 33 34 35 36 37 38 39 40 41
/**
 * 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;

public:
	SongTime() = default;

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

42 43 44
	static constexpr SongTime zero() {
		return SongTime(Base::zero());
	}
45

46 47 48 49 50
	template<typename D>
	static constexpr SongTime Cast(D src) {
		return SongTime(std::chrono::duration_cast<Base>(src));
	}

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
	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);
	}

67 68 69 70 71 72 73 74
	constexpr rep ToS() const {
		return count() / rep(1000);
	}

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

75 76 77 78
	constexpr rep ToMS() const {
		return count();
	}

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

84 85 86 87 88 89 90 91 92 93 94 95
	/**
	 * 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));
	}

96 97 98
	constexpr double ToDoubleS() const {
		return double(count()) / 1000.;
	};
99 100 101 102 103 104 105 106

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

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

108
	constexpr SongTime operator+(const SongTime &other) const {
109 110 111
		return SongTime(*(const Base *)this + (const Base &)other);
	}

112
	constexpr SongTime operator-(const SongTime &other) const {
113 114
		return SongTime(*(const Base *)this - (const Base &)other);
	}
115 116
};

117 118 119 120 121 122 123 124 125 126 127 128 129
/**
 * 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;

public:
	SignedSongTime() = default;

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

130 131 132 133 134
	/**
	 * Allow implicit conversion from SongTime to SignedSongTime.
	 */
	constexpr SignedSongTime(SongTime t):Base(t) {}

135 136 137
	static constexpr SignedSongTime zero() {
		return SignedSongTime(Base::zero());
	}
138

139 140 141 142 143 144 145
	/**
	 * Generate a negative value.
	 */
	static constexpr SignedSongTime Negative() {
		return SignedSongTime(-1);
	}

146 147 148 149 150
	template<typename D>
	static constexpr SongTime Cast(D src) {
		return SongTime(std::chrono::duration_cast<Base>(src));
	}

151 152 153 154
	static constexpr SignedSongTime FromS(int s) {
		return SignedSongTime(rep(s) * 1000);
	}

155 156 157 158
	static constexpr SignedSongTime FromS(unsigned s) {
		return SignedSongTime(rep(s) * 1000);
	}

159 160 161 162 163 164 165 166 167 168 169 170
	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);
	}

171 172 173 174 175 176 177 178
	constexpr rep ToS() const {
		return count() / rep(1000);
	}

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

179 180 181 182 183
	constexpr rep ToMS() const {
		return count();
	}

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

188 189 190 191 192 193 194 195 196 197 198 199
	/**
	 * 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));
	}

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
	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;
	}
215

216
	constexpr SignedSongTime operator+(const SignedSongTime &other) const {
217 218 219
		return SignedSongTime(*(const Base *)this + (const Base &)other);
	}

220
	constexpr SignedSongTime operator-(const SignedSongTime &other) const {
221 222
		return SignedSongTime(*(const Base *)this - (const Base &)other);
	}
223 224
};

225
#endif