aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2014-08-28 07:12:58 +0200
committerMax Kellermann <max@duempel.org>2014-08-28 13:03:18 +0200
commit854258f37626d40271a821d4835e9cfb946c8ad8 (patch)
treeefadb5d4c12cf96b311e7f3bc264bd4ed3ab9043 /src
parent26f2d7fbae9caba8f5961555d8965215c3fcd60f (diff)
downloadmpd-854258f37626d40271a821d4835e9cfb946c8ad8.tar.gz
mpd-854258f37626d40271a821d4835e9cfb946c8ad8.tar.xz
mpd-854258f37626d40271a821d4835e9cfb946c8ad8.zip
Chrono: override operator+ and operator-
Make sure we return the correct type. This obsoletes the cast constructor trick.
Diffstat (limited to 'src')
-rw-r--r--src/Chrono.hxx37
1 files changed, 23 insertions, 14 deletions
diff --git a/src/Chrono.hxx b/src/Chrono.hxx
index 9832a680f..de5122fd0 100644
--- a/src/Chrono.hxx
+++ b/src/Chrono.hxx
@@ -21,6 +21,7 @@
#define MPD_CHRONO_HXX
#include <chrono>
+#include <utility>
#include <cstdint>
/**
@@ -37,13 +38,9 @@ public:
template<typename T>
explicit constexpr SongTime(T t):Base(t) {}
- /**
- * This constructor allows implicit conversion from the base
- * class to this class. It is necessary because all of
- * std::chrono::duration's operators return another
- * std::chrono::duration and not an instance of this class.
- */
- constexpr SongTime(Base b):Base(b) {}
+ static constexpr SongTime zero() {
+ return SongTime(Base::zero());
+ }
static constexpr SongTime FromS(unsigned s) {
return SongTime(rep(s) * 1000);
@@ -81,6 +78,14 @@ public:
constexpr bool IsPositive() const {
return count() > 0;
}
+
+ constexpr SongTime operator+(const SongTime &other) const {
+ return SongTime(*(const Base *)this + (const Base &)other);
+ }
+
+ constexpr SongTime operator-(const SongTime &other) const {
+ return SongTime(*(const Base *)this - (const Base &)other);
+ }
};
/**
@@ -97,13 +102,9 @@ public:
template<typename T>
explicit constexpr SignedSongTime(T t):Base(t) {}
- /**
- * This constructor allows implicit conversion from the base
- * class to this class. It is necessary because all of
- * std::chrono::duration's operators return another
- * std::chrono::duration and not an instance of this class.
- */
- constexpr SignedSongTime(Base b):Base(b) {}
+ static constexpr SignedSongTime zero() {
+ return SignedSongTime(Base::zero());
+ }
static constexpr SignedSongTime FromS(int s) {
return SignedSongTime(rep(s) * 1000);
@@ -145,6 +146,14 @@ public:
constexpr bool IsNegative() const {
return count() < 0;
}
+
+ constexpr SignedSongTime operator+(const SignedSongTime &other) const {
+ return SignedSongTime(*(const Base *)this + (const Base &)other);
+ }
+
+ constexpr SignedSongTime operator-(const SignedSongTime &other) const {
+ return SignedSongTime(*(const Base *)this - (const Base &)other);
+ }
};
#endif