]> git.tdb.fi Git - libs/core.git/blob - source/time/timedelta.h
Select RawTime definition based on compiler, not platform
[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 <msp/strings/lexicalcast.h>
13 #include "rawtime.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         TimeDelta operator+(const TimeDelta &t) const  { return TimeDelta(usec+t.usec); }
46         TimeDelta &operator+=(const TimeDelta &t)      { usec+=t.usec; return *this; }
47         TimeDelta operator-(const TimeDelta &t) const  { return TimeDelta(usec-t.usec); }
48         TimeDelta &operator-=(const TimeDelta &t)      { usec-=t.usec; return *this; }
49
50         template<typename T>
51         TimeDelta operator*(T a) const                 { return TimeDelta(RawTime(usec*a)); }
52         template<typename T>
53         TimeDelta &operator*=(T a)                     { usec=RawTime(usec*a); return *this; }
54
55         template<typename T>
56         TimeDelta operator/(T a) const                 { return TimeDelta(RawTime(usec/a)); }
57         template<typename T>
58         TimeDelta &operator/=(T a)                     { usec=RawTime(usec/a); return *this; }
59
60         double    operator/(const TimeDelta &t) const  { return double(usec)/t.usec; }
61
62         bool      operator>(const TimeDelta &t) const  { return usec>t.usec; }
63         bool      operator>=(const TimeDelta &t) const { return usec>=t.usec; }
64         bool      operator<(const TimeDelta &t) const  { return usec<t.usec; }
65         bool      operator<=(const TimeDelta &t) const { return usec<=t.usec; }
66         bool      operator==(const TimeDelta &t) const { return usec==t.usec; }
67         bool      operator!=(const TimeDelta &t) const { return usec!=t.usec; }
68
69 #ifndef WIN32
70         operator timeval() const { return rawtime_to_timeval(usec); }
71         operator timespec() const { return rawtime_to_timespec(usec); }
72 #endif
73
74         operator const void *() const                  { return usec ? this : 0; }
75 };
76
77 template<typename T>
78 inline TimeDelta operator*(T a, const TimeDelta &t)   { return t*a; }
79
80 void operator<<(LexicalConverter &, const TimeDelta &);
81
82 } // namespace Time
83 } // namespace Msp
84
85 #endif