2010-12-21 02:05:13 +01:00
|
|
|
/*
|
|
|
|
|
TIMEVALUE.hpp - basic definitions for time values and time intervals
|
|
|
|
|
|
|
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2010, Hermann Vosseler <Ichthyostega@web.de>
|
|
|
|
|
|
|
|
|
|
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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef LIB_TIME_TIMEVALUE_H
|
|
|
|
|
#define LIB_TIME_TIMEVALUE_H
|
|
|
|
|
|
2010-12-22 04:49:57 +01:00
|
|
|
#include <boost/operators.hpp>
|
2010-12-31 05:59:53 +01:00
|
|
|
#include <boost/rational.hpp>
|
2010-12-28 05:22:20 +01:00
|
|
|
#include <cstdlib>
|
2010-12-21 02:05:13 +01:00
|
|
|
#include <string>
|
|
|
|
|
|
2010-12-22 04:49:57 +01:00
|
|
|
extern "C" {
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <gavl/gavltime.h>
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-21 02:05:13 +01:00
|
|
|
|
|
|
|
|
namespace lib {
|
|
|
|
|
namespace time {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2010-12-27 06:58:27 +01:00
|
|
|
* basic constant internal time value.
|
|
|
|
|
* These time values provide the implementation base
|
|
|
|
|
* for all further time types. They can be created by
|
|
|
|
|
* wrapping up a raw micro tick value (gavl_time_t),
|
|
|
|
|
* are totally ordered, but besides that,
|
|
|
|
|
* they are opaque and non-mutable.
|
|
|
|
|
* @note clients should prefer to use Time instances,
|
|
|
|
|
* which explicitly denote an Lumiera internal
|
|
|
|
|
* time value and are easier to use.
|
|
|
|
|
* @see TimeVar when full arithmetics are required
|
2010-12-21 02:05:13 +01:00
|
|
|
*/
|
|
|
|
|
class TimeValue
|
2010-12-27 02:04:34 +01:00
|
|
|
: boost::totally_ordered<TimeValue,
|
|
|
|
|
boost::totally_ordered<TimeValue, gavl_time_t> >
|
2010-12-22 04:49:57 +01:00
|
|
|
{
|
2010-12-26 23:00:34 +01:00
|
|
|
protected:
|
2010-12-27 05:55:15 +01:00
|
|
|
/** the raw (internal) time value
|
|
|
|
|
* used to implement the time types */
|
2010-12-22 04:49:57 +01:00
|
|
|
gavl_time_t t_;
|
|
|
|
|
|
|
|
|
|
|
2010-12-27 05:55:15 +01:00
|
|
|
/** Assigning of time values is not allowed,
|
|
|
|
|
* but derived classed might allow that */
|
|
|
|
|
TimeValue&
|
|
|
|
|
operator= (TimeValue const& o)
|
|
|
|
|
{
|
|
|
|
|
t_ = o.t_;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
2010-12-22 04:49:57 +01:00
|
|
|
explicit
|
|
|
|
|
TimeValue (gavl_time_t val=0)
|
|
|
|
|
: t_(val)
|
|
|
|
|
{ }
|
|
|
|
|
|
2010-12-27 05:55:15 +01:00
|
|
|
/** copy initialisation allowed */
|
|
|
|
|
TimeValue (TimeValue const& o)
|
|
|
|
|
: t_(o.t_)
|
|
|
|
|
{ }
|
2010-12-22 04:49:57 +01:00
|
|
|
|
2011-01-06 13:31:13 +01:00
|
|
|
/** @internal to pass Time values to C functions */
|
|
|
|
|
friend gavl_time_t _raw (TimeValue const& time) { return time.t_; }
|
|
|
|
|
|
2010-12-22 04:49:57 +01:00
|
|
|
// Supporting totally_ordered
|
2010-12-26 23:00:34 +01:00
|
|
|
friend bool operator< (TimeValue const& t1, TimeValue const& t2) { return t1.t_ < t2.t_; }
|
|
|
|
|
friend bool operator< (TimeValue const& t1, gavl_time_t t2) { return t1.t_ < t2 ; }
|
|
|
|
|
friend bool operator> (TimeValue const& t1, gavl_time_t t2) { return t1.t_ > t2 ; }
|
|
|
|
|
friend bool operator== (TimeValue const& t1, TimeValue const& t2) { return t1.t_ == t2.t_; }
|
|
|
|
|
friend bool operator== (TimeValue const& t1, gavl_time_t t2) { return t1.t_ == t2 ; }
|
2010-12-22 04:49:57 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2010-12-27 05:55:15 +01:00
|
|
|
|
|
|
|
|
/** a mutable time value,
|
2010-12-27 06:58:27 +01:00
|
|
|
* behaving like a plain number,
|
|
|
|
|
* allowing copy and re-accessing
|
2010-12-28 07:24:54 +01:00
|
|
|
* @note supports scaling by a factor,
|
|
|
|
|
* which \em deliberately is chosen
|
|
|
|
|
* as int, not gavl_time_t, because the
|
|
|
|
|
* multiplying times is meaningless.
|
2010-12-27 05:55:15 +01:00
|
|
|
*/
|
2010-12-22 04:49:57 +01:00
|
|
|
class TimeVar
|
|
|
|
|
: public TimeValue
|
2010-12-27 06:58:27 +01:00
|
|
|
, boost::additive<TimeVar,
|
|
|
|
|
boost::additive<TimeVar, TimeValue,
|
|
|
|
|
boost::multipliable<TimeVar, int>
|
|
|
|
|
> >
|
2010-12-22 04:49:57 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2011-01-05 18:15:44 +01:00
|
|
|
TimeVar (TimeValue const& time = TimeValue())
|
2010-12-22 04:49:57 +01:00
|
|
|
: TimeValue(time)
|
|
|
|
|
{ }
|
|
|
|
|
|
2010-12-27 05:55:15 +01:00
|
|
|
// Allowing copy and assignment
|
|
|
|
|
TimeVar (TimeVar const& o)
|
|
|
|
|
: TimeValue(o)
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
TimeVar&
|
|
|
|
|
operator= (TimeValue const& o)
|
|
|
|
|
{
|
|
|
|
|
t_ = TimeVar(o);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-05 18:15:44 +01:00
|
|
|
/** @internal diagnostics */
|
|
|
|
|
operator std::string () const;
|
2010-12-27 05:55:15 +01:00
|
|
|
|
|
|
|
|
// Supporting mixing with plain long int arithmetics
|
|
|
|
|
operator gavl_time_t () const { return t_; }
|
2010-12-22 04:49:57 +01:00
|
|
|
|
|
|
|
|
// Supporting additive
|
|
|
|
|
TimeVar& operator+= (TimeVar const& tx) { t_ += tx.t_; return *this; }
|
|
|
|
|
TimeVar& operator-= (TimeVar const& tx) { t_ -= tx.t_; return *this; }
|
|
|
|
|
|
|
|
|
|
// Supporting multiplication with integral factor
|
2010-12-27 06:58:27 +01:00
|
|
|
TimeVar& operator*= (int fact) { t_ *= fact; return *this; }
|
2011-01-08 19:11:42 +01:00
|
|
|
|
|
|
|
|
// Supporting flip sign
|
|
|
|
|
TimeVar operator- () const { return TimeVar(*this)*=-1; }
|
2010-12-22 04:49:57 +01:00
|
|
|
|
|
|
|
|
// baseclass TimeValue is already totally_ordered
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2010-12-28 05:22:20 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Offset measures a distance in time.
|
|
|
|
|
* It may be used to relate two points in time,
|
|
|
|
|
* or to create a modification for time-like entities.
|
|
|
|
|
* Similar to (basic) time values, offsets can be compared,
|
2010-12-28 07:24:54 +01:00
|
|
|
* but are otherwise opaque and immutable. Yet they allow
|
|
|
|
|
* to build derived values, including
|
|
|
|
|
* - the \em absolute (positive) distance for this offset
|
|
|
|
|
* - a combined offset by chaining another offset
|
2010-12-28 05:22:20 +01:00
|
|
|
*/
|
|
|
|
|
class Offset
|
|
|
|
|
: public TimeValue
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
explicit
|
|
|
|
|
Offset (TimeValue const& distance)
|
|
|
|
|
: TimeValue(distance)
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
Offset (TimeValue const& origin, TimeValue const& target)
|
|
|
|
|
: TimeValue(TimeVar(target) -= origin)
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
TimeValue
|
|
|
|
|
abs() const
|
|
|
|
|
{
|
|
|
|
|
return TimeValue(std::llabs (t_));
|
|
|
|
|
}
|
2010-12-28 07:24:54 +01:00
|
|
|
|
|
|
|
|
Offset
|
|
|
|
|
operator+ (Offset const& toChain) const
|
|
|
|
|
{
|
|
|
|
|
TimeVar distance(*this);
|
|
|
|
|
distance += toChain;
|
|
|
|
|
return Offset(distance);
|
|
|
|
|
}
|
2010-12-28 05:22:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* ======= specific Time entities ==================== */
|
|
|
|
|
|
2011-01-02 18:56:44 +01:00
|
|
|
/** rational representation of fractional seconds
|
|
|
|
|
* @warning do not mix up gavl_time_t and FSecs */
|
|
|
|
|
typedef boost::rational<long> FSecs;
|
2010-12-31 05:59:53 +01:00
|
|
|
|
2010-12-22 04:49:57 +01:00
|
|
|
/**
|
2010-12-28 05:22:20 +01:00
|
|
|
* Lumiera's internal time value datatype.
|
|
|
|
|
* This is a TimeValue, but now more specifically denoting
|
|
|
|
|
* a point in time, measured in reference to an internal
|
|
|
|
|
* (opaque) time scale.
|
|
|
|
|
*
|
|
|
|
|
* Lumiera Time provides some limited capabilities for
|
|
|
|
|
* direct manipulation; Time values can be created directly
|
|
|
|
|
* from \c (h,m,s,ms) specification and there is an string
|
|
|
|
|
* representation intended for internal use (reporting and
|
|
|
|
|
* debugging). Any real output, formatting and persistent
|
|
|
|
|
* storage should be based on the (quantised) timecode
|
|
|
|
|
* formats though, which can be generated from time values.
|
|
|
|
|
*
|
|
|
|
|
* Non constant Time objects can receive an encapsulated
|
|
|
|
|
* \em mutation message, which is also the basis for
|
|
|
|
|
* changing time spans, durations and for re-aligning
|
|
|
|
|
* quantised values to some other grid.
|
|
|
|
|
*
|
|
|
|
|
* @todo define these mutations
|
2010-12-22 04:49:57 +01:00
|
|
|
*/
|
2010-12-27 02:04:34 +01:00
|
|
|
class Time
|
|
|
|
|
: public TimeValue
|
|
|
|
|
{
|
|
|
|
|
public:
|
2010-12-27 05:55:15 +01:00
|
|
|
static const Time MAX ;
|
|
|
|
|
static const Time MIN ;
|
|
|
|
|
|
2010-12-27 02:04:34 +01:00
|
|
|
explicit
|
2010-12-28 05:22:20 +01:00
|
|
|
Time (TimeValue const& val =TimeValue(0))
|
2010-12-27 02:04:34 +01:00
|
|
|
: TimeValue(val)
|
|
|
|
|
{ }
|
|
|
|
|
|
2010-12-28 07:24:54 +01:00
|
|
|
Time (TimeVar const& calcResult)
|
|
|
|
|
: TimeValue(calcResult)
|
|
|
|
|
{ }
|
|
|
|
|
|
2011-01-02 18:56:44 +01:00
|
|
|
Time (FSecs const& fractionalSeconds);
|
2010-12-31 05:59:53 +01:00
|
|
|
|
2010-12-27 02:04:34 +01:00
|
|
|
Time ( long millis
|
|
|
|
|
, uint secs
|
|
|
|
|
, uint mins =0
|
|
|
|
|
, uint hours=0
|
|
|
|
|
);
|
2010-12-28 05:22:20 +01:00
|
|
|
|
|
|
|
|
/** @internal diagnostics */
|
|
|
|
|
operator std::string () const;
|
2011-01-07 16:28:13 +01:00
|
|
|
|
|
|
|
|
/** convenience start for time calculations */
|
|
|
|
|
TimeVar operator+ (TimeValue const& tval) const { return TimeVar(*this) + tval; }
|
|
|
|
|
TimeVar operator- (TimeValue const& tval) const { return TimeVar(*this) - tval; }
|
2010-12-27 02:04:34 +01:00
|
|
|
};
|
2010-12-22 04:49:57 +01:00
|
|
|
|
|
|
|
|
|
2010-12-28 05:22:20 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Duration is the internal Lumiera time metric.
|
|
|
|
|
* It is an absolute (positive) value, but can be
|
|
|
|
|
* promoted from an offset. Similar to Time,
|
|
|
|
|
* Duration can receive mutation messages.
|
|
|
|
|
*
|
|
|
|
|
* @todo define these mutations
|
|
|
|
|
*/
|
|
|
|
|
class Duration
|
|
|
|
|
: public Offset
|
2010-12-22 04:49:57 +01:00
|
|
|
{
|
2010-12-21 02:05:13 +01:00
|
|
|
public:
|
2010-12-28 05:22:20 +01:00
|
|
|
Duration (Offset const& distance)
|
|
|
|
|
: Offset(distance.abs())
|
2010-12-22 04:49:57 +01:00
|
|
|
{ }
|
|
|
|
|
};
|
2010-12-27 06:58:27 +01:00
|
|
|
|
2010-12-21 02:05:13 +01:00
|
|
|
|
|
|
|
|
|
2010-12-28 05:22:20 +01:00
|
|
|
/**
|
|
|
|
|
* A time interval anchored at a specific point in time.
|
|
|
|
|
* The start point of this timespan is also its nominal
|
|
|
|
|
* position, and the end point is normalised to happen
|
|
|
|
|
* never before the start point. A TimeSpan is enough
|
|
|
|
|
* to fully specify the temporal properties of an
|
|
|
|
|
* object within the model.
|
|
|
|
|
*
|
2010-12-28 07:24:54 +01:00
|
|
|
* Similar to Time and Duration, a TimeSpan may
|
|
|
|
|
* also receive an (opaque) mutation message.
|
2010-12-28 05:22:20 +01:00
|
|
|
*/
|
2010-12-22 04:49:57 +01:00
|
|
|
class TimeSpan
|
|
|
|
|
: public Time
|
|
|
|
|
{
|
|
|
|
|
Duration dur_;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
TimeSpan(Time start, Duration length)
|
|
|
|
|
: Time(start)
|
|
|
|
|
, dur_(length)
|
|
|
|
|
{ }
|
2010-12-28 07:24:54 +01:00
|
|
|
///////////TODO creating timespans needs to be more convenient....
|
2010-12-28 05:22:20 +01:00
|
|
|
|
|
|
|
|
operator Duration() const
|
|
|
|
|
{
|
|
|
|
|
return dur_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Time
|
|
|
|
|
getEnd() const
|
|
|
|
|
{
|
|
|
|
|
TimeVar startPoint (*this);
|
2010-12-28 07:24:54 +01:00
|
|
|
return (startPoint + dur_);
|
2010-12-28 05:22:20 +01:00
|
|
|
}
|
2010-12-22 04:49:57 +01:00
|
|
|
};
|
|
|
|
|
|
2010-12-21 02:05:13 +01:00
|
|
|
|
|
|
|
|
}} // lib::time
|
|
|
|
|
#endif
|