From 0c45fc47f3f335809a6d1ad9d35ebc5f824cc7ae Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Mon, 27 Dec 2010 05:55:15 +0100 Subject: [PATCH] defining the first elementary operations for time values --- src/lib/time/lumitime.cpp | 10 ++++++ src/lib/time/timevalue.hpp | 55 ++++++++++++++++++++++++------ tests/lib/time/time-value-test.cpp | 49 ++++++++++++++++++++++---- 3 files changed, 97 insertions(+), 17 deletions(-) diff --git a/src/lib/time/lumitime.cpp b/src/lib/time/lumitime.cpp index 6f1a7ac23..5794b445e 100644 --- a/src/lib/time/lumitime.cpp +++ b/src/lib/time/lumitime.cpp @@ -34,6 +34,16 @@ extern "C" { using std::string; +namespace lib { +namespace time { + + const Time Time::MAX ( TimeValue (+std::numeric_limits::max()) ); + const Time Time::MIN ( TimeValue (-std::numeric_limits::max()) ); + + +}} // namespace lib::Time + +///////////////////////////////////////////////////////////////////////////TODO leftover of the existing/initial lumitime-Implementation namespace lumiera { diff --git a/src/lib/time/timevalue.hpp b/src/lib/time/timevalue.hpp index 9388e5842..bc14d4baa 100644 --- a/src/lib/time/timevalue.hpp +++ b/src/lib/time/timevalue.hpp @@ -47,20 +47,30 @@ namespace time { boost::totally_ordered > { protected: + /** the raw (internal) time value + * used to implement the time types */ gavl_time_t t_; - public: - static const TimeValue MAX ; - static const TimeValue MIN ; + /** Assigning of time values is not allowed, + * but derived classed might allow that */ + TimeValue& + operator= (TimeValue const& o) + { + t_ = o.t_; + return *this; + } + + public: explicit TimeValue (gavl_time_t val=0) : t_(val) { } - // using standard copy operations - - operator gavl_time_t () const { return t_; } + /** copy initialisation allowed */ + TimeValue (TimeValue const& o) + : t_(o.t_) + { } // Supporting totally_ordered friend bool operator< (TimeValue const& t1, TimeValue const& t2) { return t1.t_ < t2.t_; } @@ -71,6 +81,11 @@ namespace time { }; + + /** a mutable time value, + * behaving like a plain number + * and allowing copying and re-accessing + */ class TimeVar : public TimeValue , boost::additive @@ -81,6 +96,21 @@ namespace time { : TimeValue(time) { } + // Allowing copy and assignment + TimeVar (TimeVar const& o) + : TimeValue(o) + { } + + TimeVar& + operator= (TimeValue const& o) + { + t_ = TimeVar(o); + return *this; + } + + + // Supporting mixing with plain long int arithmetics + operator gavl_time_t () const { return t_; } // Supporting additive TimeVar& operator+= (TimeVar const& tx) { t_ += tx.t_; return *this; } @@ -102,6 +132,9 @@ namespace time { : public TimeValue { public: + static const Time MAX ; + static const Time MIN ; + explicit Time (TimeValue val= TimeValue(0)) : TimeValue(val) @@ -121,17 +154,17 @@ namespace time { public: explicit - Offset (TimeValue distance) + Offset (TimeValue const& distance) : TimeValue(distance) { } }; inline Offset - operator- (Time const& end, Time const& start) + operator- (TimeValue const& end, TimeValue const& start) { -// TimeVar distance(end); -// distance -= start; -// return Offset(distance); + TimeVar distance(end); + distance -= start; + return Offset(distance); } typedef const Offset TimeDistance; diff --git a/tests/lib/time/time-value-test.cpp b/tests/lib/time/time-value-test.cpp index 645f49b26..6ea636c4d 100644 --- a/tests/lib/time/time-value-test.cpp +++ b/tests/lib/time/time-value-test.cpp @@ -49,27 +49,64 @@ namespace test{ */ class TimeValue_test : public Test { + gavl_time_t + random_or_get (Arg arg) + { + if (isnil(arg)) + return (rand() % 10000); + else + return lexical_cast (arg[1]); + } + + virtual void run (Arg arg) { - long refval= isnil(arg)? 1 : lexical_cast (arg[1]); + TimeValue ref (random_or_get(arg)); - TimeValue ref (refval); - - checkBasics (ref); + checkBasicTimeValues (ref); checkComparisons (ref); checkComponentAccess(); } + /** @test creating some time values and performing trivial comparisons. + * @note you can't do much beyond that, because TimeValues as such + * are a "dead end": they are opaque and can't be altered. + */ void - checkBasics (TimeValue ref) + checkBasicTimeValues (TimeValue org) { + TimeValue zero; + TimeValue one (1); + TimeValue max (Time::MAX); + TimeValue min (Time::MIN); + + TimeValue val (org); + + CHECK (zero == zero); + CHECK (zero <= zero); + CHECK (zero >= zero); + + CHECK (zero < one); + CHECK (min < max); + CHECK (min < val); + CHECK (val < max); + + // mixed comparisons with raw numeric time + gavl_time_t g2 (-2); + CHECK (zero > g2); + CHECK (one > g2); + CHECK (one >= g2); + CHECK (g2 < max); + + CHECK (!(g2 > max)); + CHECK (!(g2 < min)); } void - checkComparisons (TimeValue ref) + checkComparisons (TimeValue org) { }