aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Horn2010-01-06 12:15:05 +0000
committerMax Horn2010-01-06 12:15:05 +0000
commit1c6ccf8000c8eb34e4ae3d68312a23261f87862c (patch)
treebf4bc59de8797536a2a91d985d0fbfde44fcaffe
parent5b635fd610a2d1f895adadbb43af3a76a36f1fd0 (diff)
downloadscummvm-rg350-1c6ccf8000c8eb34e4ae3d68312a23261f87862c.tar.gz
scummvm-rg350-1c6ccf8000c8eb34e4ae3d68312a23261f87862c.tar.bz2
scummvm-rg350-1c6ccf8000c8eb34e4ae3d68312a23261f87862c.zip
Add more comparision operators to Timestamp
svn-id: r47071
-rw-r--r--sound/timestamp.cpp34
-rw-r--r--sound/timestamp.h13
-rw-r--r--test/sound/timestamp.h34
3 files changed, 73 insertions, 8 deletions
diff --git a/sound/timestamp.cpp b/sound/timestamp.cpp
index 3d9178da2b..9d95fbe556 100644
--- a/sound/timestamp.cpp
+++ b/sound/timestamp.cpp
@@ -72,12 +72,40 @@ Timestamp Timestamp::convertToFramerate(int newFramerate) const {
}
bool Timestamp::operator==(const Timestamp &ts) const {
- return (ts._secs == _secs) &&
- (ts._numberOfFrames * _framerate == _numberOfFrames * ts._framerate);
+ return cmp(ts) == 0;
}
bool Timestamp::operator!=(const Timestamp &ts) const {
- return !(*this == ts);
+ return cmp(ts) != 0;
+}
+
+bool Timestamp::operator<(const Timestamp &ts) const {
+ return cmp(ts) < 0;
+}
+
+bool Timestamp::operator<=(const Timestamp &ts) const {
+ return cmp(ts) <= 0;
+}
+
+bool Timestamp::operator>(const Timestamp &ts) const {
+ return cmp(ts) > 0;
+}
+
+bool Timestamp::operator>=(const Timestamp &ts) const {
+ return cmp(ts) >= 0;
+}
+
+int Timestamp::cmp(const Timestamp &ts) const {
+ int delta = _secs - ts._secs;
+ if (!delta) {
+ const uint g = gcd(_framerate, ts._framerate);
+ const uint p = _framerate / g;
+ const uint q = ts._framerate / g;
+
+ delta = (_numberOfFrames * q - ts._numberOfFrames * p);
+ }
+
+ return delta;
}
diff --git a/sound/timestamp.h b/sound/timestamp.h
index 2e8689a81d..2d4f3db693 100644
--- a/sound/timestamp.h
+++ b/sound/timestamp.h
@@ -90,12 +90,11 @@ public:
* as equal even if they use different framerates.
*/
bool operator==(const Timestamp &ts) const;
-
bool operator!=(const Timestamp &ts) const;
-// bool operator<(const Timestamp &ts) const;
-// bool operator<=(const Timestamp &ts) const;
-// bool operator>(const Timestamp &ts) const;
-// bool operator>=(const Timestamp &ts) const;
+ bool operator<(const Timestamp &ts) const;
+ bool operator<=(const Timestamp &ts) const;
+ bool operator>(const Timestamp &ts) const;
+ bool operator>=(const Timestamp &ts) const;
/**
* Returns a new timestamp, which corresponds to the time encoded
@@ -129,6 +128,10 @@ public:
/** Return the framerate used by this timestamp. */
int getFramerate() const { return _framerate / _framerateFactor; }
+
+protected:
+
+ int cmp(const Timestamp &ts) const;
};
diff --git a/test/sound/timestamp.h b/test/sound/timestamp.h
index d4cd0edc44..7219bb54aa 100644
--- a/test/sound/timestamp.h
+++ b/test/sound/timestamp.h
@@ -103,6 +103,40 @@ class TimestampTestSuite : public CxxTest::TestSuite
}
+ void test_compare() {
+ const Audio::Timestamp a = Audio::Timestamp(60, 1000);
+ Audio::Timestamp b = Audio::Timestamp(60, 60);
+ Audio::Timestamp c = Audio::Timestamp(60, 44100);
+
+ TS_ASSERT(a <= b);
+ TS_ASSERT(b <= c);
+ TS_ASSERT(a <= c);
+
+ TS_ASSERT(b >= a);
+ TS_ASSERT(c >= b);
+ TS_ASSERT(c >= a);
+
+ b = b.addFrames(60 / 12);
+ c = c.addFrames(44100 / 10);
+
+ TS_ASSERT(a < b);
+ TS_ASSERT(b < c);
+ TS_ASSERT(a < c);
+
+ TS_ASSERT(b > a);
+ TS_ASSERT(c > b);
+ TS_ASSERT(c > a);
+
+ TS_ASSERT(a <= b);
+ TS_ASSERT(b <= c);
+ TS_ASSERT(a <= c);
+
+ TS_ASSERT(b >= a);
+ TS_ASSERT(c >= b);
+ TS_ASSERT(c >= a);
+ }
+
+
void test_framerate() {
const Audio::Timestamp a = Audio::Timestamp(500, 1000);
const Audio::Timestamp b = Audio::Timestamp(500, 67);