e50a256cced38973aac30a9a16299d9390fe3cce
[ardour.git] / libs / backends / wavesaudio / wavesapi / miscutils / UMicroseconds.h
1 #ifndef __UMicroseconds_h__
2         #define __UMicroseconds_h__
3         
4 /* Copy to include
5 #include "UMicroseconds.h"
6 */
7
8
9
10 #include "BasicTypes/WUDefines.h"
11 #include "BasicTypes/WUTypes.h"
12
13 namespace wvNS { 
14 // a wraper for Microseconds function from Timer.h
15 class DllExport UMicroseconds
16 {
17 public:
18
19 #ifdef _WINDOWS
20         typedef int64_t TimeKeeper;
21 #endif
22 #ifdef __MACOS__
23         typedef uint64_t TimeKeeper;
24 #endif
25 #ifdef __linux__
26         typedef uint64_t TimeKeeper;
27 #endif
28
29 private:
30         TimeKeeper theTime;
31
32 public:
33
34         UMicroseconds()
35         {
36                 ReadTime();
37         }
38
39         UMicroseconds(const TimeKeeper in_initVal) : theTime(in_initVal) {}
40
41         UMicroseconds(const UMicroseconds& inUM) : theTime(inUM.theTime) {}
42         UMicroseconds& operator=(const UMicroseconds& inUM) {theTime = inUM.theTime;  return *this;}
43         UMicroseconds& operator+=(const TimeKeeper in_timeToAdd)  {theTime += in_timeToAdd;  return *this;}
44
45         UMicroseconds& ReadTime();
46   
47         TimeKeeper GetNativeTime() const {return theTime;}
48         operator uint64_t () {return static_cast<uint64_t>(theTime);}
49         operator double () const {return static_cast<const double>(theTime);}
50
51         double Seconds() const {return static_cast<double>(theTime) / double(1000000);}
52         double MilliSeconds() const {return static_cast<double>(theTime) / double(1000);}
53         double MicroSeconds() const {return static_cast<double>(theTime);}
54
55 #ifdef __MACOS__
56         uint32_t hi();
57         uint32_t lo();
58 #endif
59 };
60
61 inline UMicroseconds operator-(const UMicroseconds& in_one, const UMicroseconds& in_two)
62 {
63         UMicroseconds retVal(in_one.GetNativeTime() - in_two.GetNativeTime());
64         return retVal;
65 }
66
67 class UMicrosecondsAccumulator
68 {
69 public:
70         UMicrosecondsAccumulator() : m_start_time(0), m_accumulator(0) {}
71         
72         void Start();
73         void Stop();
74         void Clear();
75         
76         UMicroseconds GetAccumulatedTime() const;
77         
78         UMicrosecondsAccumulator& operator+=(const UMicrosecondsAccumulator&);
79         
80 protected:
81         UMicroseconds m_start_time;
82         UMicroseconds m_accumulator;
83 };
84
85 inline UMicroseconds operator-(const UMicrosecondsAccumulator& in_one, const UMicrosecondsAccumulator& in_two)
86 {
87         UMicroseconds retVal(in_one.GetAccumulatedTime() - in_two.GetAccumulatedTime());
88         return retVal;
89 }
90
91 //=========================================================================================//
92 inline void MicrosecondDelay(double amt)
93 //=========================================================================================//
94 {
95         UMicroseconds than;
96         UMicroseconds now;
97
98         do
99         {
100                 now.ReadTime();
101         }       while ((now.MicroSeconds() - than.MicroSeconds()) < amt);
102 }
103         
104 } // namespace wvNS { 
105 #endif //#ifndef __UMicroseconds_h__