]> git.tdb.fi Git - libs/core.git/blob - source/time/timedelta.h
Add LogicError exception class
[libs/core.git] / source / time / timedelta.h
1 /* $Id$
2
3 This file is part of libmspcore     
4 Copyright © 2006  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #ifndef MSP_TIME_TIMEDELTA_H_
9 #define MSP_TIME_TIMEDELTA_H_
10
11 #include <time.h>
12 #include <ostream>
13 #include "types.h"
14
15 namespace Msp {
16 namespace Time {
17
18 /**
19 Represents a quantity of time, such as five seconds.
20 */
21 class TimeDelta
22 {
23 private:
24         RawTime usec;
25
26 public:
27         /**
28         Constructs a zero TimeDelta.
29         */
30         TimeDelta(): usec(0) { }
31
32         /**
33         Constructs a TimeDelta from a plain number.  The purpose of this is to allow
34         serialization together with the raw() function.  For creating TimeDeltas
35         with a specific length, see units.h.
36         */
37         explicit TimeDelta(RawTime u): usec(u) { }
38
39         /**
40         Returns the raw number stored inside the TimeDelta.  This should only be used
41         for serialization and the result should not be interpreted in any way.
42         */
43         RawTime raw() const { return usec; }
44
45 #ifndef WIN32
46         /**
47         Fills in a timespec struct.  To get a meaningful scalar value from the
48         TimeDelta, divide with one of the values in units.h.
49         */
50         void fill_timespec(timespec &ts) const { ts.tv_sec=usec/1000000; ts.tv_nsec=(usec%1000000)*1000; }
51 #endif
52
53         TimeDelta operator+(const TimeDelta &t) const  { return TimeDelta(usec+t.usec); }
54         TimeDelta &operator+=(const TimeDelta &t)      { usec+=t.usec; return *this; }
55         TimeDelta operator-(const TimeDelta &t) const  { return TimeDelta(usec-t.usec); }
56         TimeDelta &operator-=(const TimeDelta &t)      { usec-=t.usec; return *this; }
57
58         template<typename T>
59         TimeDelta operator*(T a) const                 { return TimeDelta(RawTime(usec*a)); }
60         template<typename T>
61         TimeDelta &operator*=(T a)                     { usec=RawTime(usec*a); return *this; }
62
63         template<typename T>
64         TimeDelta operator/(T a) const                 { return TimeDelta(RawTime(usec/a)); }
65         template<typename T>
66         TimeDelta &operator/=(T a)                     { usec=RawTime(usec/a); return *this; }
67
68         double    operator/(const TimeDelta &t) const  { return double(usec)/t.usec; }
69
70         bool      operator>(const TimeDelta &t) const  { return usec>t.usec; }
71         bool      operator>=(const TimeDelta &t) const { return usec>=t.usec; }
72         bool      operator<(const TimeDelta &t) const  { return usec<t.usec; }
73         bool      operator<=(const TimeDelta &t) const { return usec<=t.usec; }
74         bool      operator==(const TimeDelta &t) const { return usec==t.usec; }
75         bool      operator!=(const TimeDelta &t) const { return usec!=t.usec; }
76
77         operator const void *() const                  { return usec ? this : 0; }
78 };
79
80 template<typename T>
81 inline TimeDelta operator*(T a, const TimeDelta &t)   { return t*a; }
82
83 extern std::ostream &operator<<(std::ostream &, const TimeDelta &);
84
85 } // namespace Time
86 } // namespace Msp
87
88 #endif