Chrono.hxx 4.99 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include <cstdint>

/**
 * 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) {}

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

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
	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);
	}

61 62 63 64 65 66 67 68
	constexpr rep ToS() const {
		return count() / rep(1000);
	}

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

69 70 71 72
	constexpr rep ToMS() const {
		return count();
	}

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

78 79 80 81 82 83 84 85 86 87 88 89
	/**
	 * 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));
	}

90 91 92
	constexpr double ToDoubleS() const {
		return double(count()) / 1000.;
	};
93 94 95 96 97 98 99 100

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

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

102
	constexpr SongTime operator+(const SongTime &other) const {
103 104 105
		return SongTime(*(const Base *)this + (const Base &)other);
	}

106
	constexpr SongTime operator-(const SongTime &other) const {
107 108
		return SongTime(*(const Base *)this - (const Base &)other);
	}
109 110
};

111 112 113 114 115 116 117 118 119 120 121 122 123 124
/**
 * 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) {}

125 126 127 128 129
	/**
	 * Allow implicit conversion from SongTime to SignedSongTime.
	 */
	constexpr SignedSongTime(SongTime t):Base(t) {}

130 131 132
	static constexpr SignedSongTime zero() {
		return SignedSongTime(Base::zero());
	}
133

134 135 136 137 138 139 140
	/**
	 * Generate a negative value.
	 */
	static constexpr SignedSongTime Negative() {
		return SignedSongTime(-1);
	}

141 142 143 144
	static constexpr SignedSongTime FromS(int s) {
		return SignedSongTime(rep(s) * 1000);
	}

145 146 147 148
	static constexpr SignedSongTime FromS(unsigned s) {
		return SignedSongTime(rep(s) * 1000);
	}

149 150 151 152 153 154 155 156 157 158 159 160
	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);
	}

161 162 163 164 165 166 167 168
	constexpr rep ToS() const {
		return count() / rep(1000);
	}

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

169 170 171 172 173
	constexpr rep ToMS() const {
		return count();
	}

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

178 179 180 181 182 183 184 185 186 187 188 189
	/**
	 * 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));
	}

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
	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;
	}
205

206
	constexpr SignedSongTime operator+(const SignedSongTime &other) const {
207 208 209
		return SignedSongTime(*(const Base *)this + (const Base &)other);
	}

210
	constexpr SignedSongTime operator-(const SignedSongTime &other) const {
211 212
		return SignedSongTime(*(const Base *)this - (const Base &)other);
	}
213 214
};

215
#endif