Chrono.hxx 5.24 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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
 * 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

23 24
#include "Compiler.h"

25
#include <chrono>
26
#include <utility>
27 28
#include <cstdint>

29
#if GCC_OLDER_THAN(4,7)
30 31 32 33 34 35
/* std::chrono::duration operators are "constexpr" since gcc 4.7 */
#define chrono_constexpr gcc_pure
#else
#define chrono_constexpr constexpr
#endif

36 37 38 39 40 41 42 43 44 45 46 47 48 49
/**
 * 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) {}

50 51 52
	static constexpr SongTime zero() {
		return SongTime(Base::zero());
	}
53

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

70 71 72 73 74 75 76 77
	constexpr rep ToS() const {
		return count() / rep(1000);
	}

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

78 79 80 81
	constexpr rep ToMS() const {
		return count();
	}

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

87 88 89 90 91 92 93 94 95 96 97 98
	/**
	 * 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));
	}

99 100 101
	constexpr double ToDoubleS() const {
		return double(count()) / 1000.;
	};
102 103 104 105 106 107 108 109

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

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

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

115
	chrono_constexpr SongTime operator-(const SongTime &other) const {
116 117
		return SongTime(*(const Base *)this - (const Base &)other);
	}
118 119
};

120 121 122 123 124 125 126 127 128 129 130 131 132 133
/**
 * 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) {}

134 135 136 137 138
	/**
	 * Allow implicit conversion from SongTime to SignedSongTime.
	 */
	constexpr SignedSongTime(SongTime t):Base(t) {}

139 140 141
	static constexpr SignedSongTime zero() {
		return SignedSongTime(Base::zero());
	}
142

143 144 145 146 147 148 149
	/**
	 * Generate a negative value.
	 */
	static constexpr SignedSongTime Negative() {
		return SignedSongTime(-1);
	}

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

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

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

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

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

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

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

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

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

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

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

224 225
#undef chrono_constexpr

226
#endif