defining the first elementary operations for time values

This commit is contained in:
Fischlurch 2010-12-27 05:55:15 +01:00
parent 9d91869098
commit 0c45fc47f3
3 changed files with 97 additions and 17 deletions

View file

@ -34,6 +34,16 @@ extern "C" {
using std::string;
namespace lib {
namespace time {
const Time Time::MAX ( TimeValue (+std::numeric_limits<gavl_time_t>::max()) );
const Time Time::MIN ( TimeValue (-std::numeric_limits<gavl_time_t>::max()) );
}} // namespace lib::Time
///////////////////////////////////////////////////////////////////////////TODO leftover of the existing/initial lumitime-Implementation
namespace lumiera {

View file

@ -47,20 +47,30 @@ namespace time {
boost::totally_ordered<TimeValue, gavl_time_t> >
{
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<TimeVar>
@ -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;

View file

@ -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<gavl_time_t> (arg[1]);
}
virtual void
run (Arg arg)
{
long refval= isnil(arg)? 1 : lexical_cast<long> (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)
{
}