rcinrval.h (4880B)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 /* 7 ** C++ interval times (ref: prinrval.h) 8 ** 9 ** An interval is a period of time. The start of the interval (epoch) 10 ** must be defined by the application. The unit of time of an interval 11 ** is platform dependent, therefore so is the maximum interval that is 12 ** representable. However, that interval is never less than ~6 hours. 13 */ 14 #if defined(_RCINTERVAL_H) 15 #else 16 #define _RCINTERVAL_H 17 18 #include "rcbase.h" 19 #include <prinrval.h> 20 21 class PR_IMPLEMENT(RCInterval): public RCBase 22 { 23 public: 24 typedef enum {now, no_timeout, no_wait} RCReservedInterval; 25 26 virtual ~RCInterval(); 27 28 RCInterval(); 29 30 RCInterval(PRIntervalTime interval); 31 RCInterval(const RCInterval& copy); 32 RCInterval(RCReservedInterval special); 33 34 void SetToNow(); 35 36 void operator=(const RCInterval&); 37 void operator=(PRIntervalTime interval); 38 39 PRBool operator<(const RCInterval&); 40 PRBool operator>(const RCInterval&); 41 PRBool operator==(const RCInterval&); 42 PRBool operator>=(const RCInterval&); 43 PRBool operator<=(const RCInterval&); 44 45 RCInterval operator+(const RCInterval&); 46 RCInterval operator-(const RCInterval&); 47 RCInterval& operator+=(const RCInterval&); 48 RCInterval& operator-=(const RCInterval&); 49 50 RCInterval operator/(PRUint32); 51 RCInterval operator*(PRUint32); 52 RCInterval& operator/=(PRUint32); 53 RCInterval& operator*=(PRUint32); 54 55 56 PRUint32 ToSeconds() const; 57 PRUint32 ToMilliseconds() const; 58 PRUint32 ToMicroseconds() const; 59 operator PRIntervalTime() const; 60 61 static PRIntervalTime FromSeconds(PRUint32 seconds); 62 static PRIntervalTime FromMilliseconds(PRUint32 milli); 63 static PRIntervalTime FromMicroseconds(PRUint32 micro); 64 65 friend class RCCondition; 66 67 private: 68 PRIntervalTime interval; 69 70 }; /* RCInterval */ 71 72 73 inline RCInterval::RCInterval(): RCBase() { } 74 75 inline RCInterval::RCInterval(const RCInterval& his): RCBase() 76 { 77 interval = his.interval; 78 } 79 80 inline RCInterval::RCInterval(PRIntervalTime ticks): RCBase() 81 { 82 interval = ticks; 83 } 84 85 inline void RCInterval::SetToNow() { 86 interval = PR_IntervalNow(); 87 } 88 89 inline void RCInterval::operator=(const RCInterval& his) 90 { 91 interval = his.interval; 92 } 93 94 inline void RCInterval::operator=(PRIntervalTime his) 95 { 96 interval = his; 97 } 98 99 inline PRBool RCInterval::operator==(const RCInterval& his) 100 { 101 return (interval == his.interval) ? PR_TRUE : PR_FALSE; 102 } 103 inline PRBool RCInterval::operator<(const RCInterval& his) 104 { 105 return (interval < his.interval)? PR_TRUE : PR_FALSE; 106 } 107 inline PRBool RCInterval::operator>(const RCInterval& his) 108 { 109 return (interval > his.interval) ? PR_TRUE : PR_FALSE; 110 } 111 inline PRBool RCInterval::operator<=(const RCInterval& his) 112 { 113 return (interval <= his.interval) ? PR_TRUE : PR_FALSE; 114 } 115 inline PRBool RCInterval::operator>=(const RCInterval& his) 116 { 117 return (interval <= his.interval) ? PR_TRUE : PR_FALSE; 118 } 119 120 inline RCInterval RCInterval::operator+(const RCInterval& his) 121 { 122 return RCInterval((PRIntervalTime)(interval + his.interval)); 123 } 124 inline RCInterval RCInterval::operator-(const RCInterval& his) 125 { 126 return RCInterval((PRIntervalTime)(interval - his.interval)); 127 } 128 inline RCInterval& RCInterval::operator+=(const RCInterval& his) 129 { 130 interval += his.interval; 131 return *this; 132 } 133 inline RCInterval& RCInterval::operator-=(const RCInterval& his) 134 { 135 interval -= his.interval; 136 return *this; 137 } 138 139 inline RCInterval RCInterval::operator/(PRUint32 him) 140 { 141 return RCInterval((PRIntervalTime)(interval / him)); 142 } 143 inline RCInterval RCInterval::operator*(PRUint32 him) 144 { 145 return RCInterval((PRIntervalTime)(interval * him)); 146 } 147 148 inline RCInterval& RCInterval::operator/=(PRUint32 him) 149 { 150 interval /= him; 151 return *this; 152 } 153 154 inline RCInterval& RCInterval::operator*=(PRUint32 him) 155 { 156 interval *= him; 157 return *this; 158 } 159 160 inline PRUint32 RCInterval::ToSeconds() const 161 { 162 return PR_IntervalToSeconds(interval); 163 } 164 inline PRUint32 RCInterval::ToMilliseconds() const 165 { 166 return PR_IntervalToMilliseconds(interval); 167 } 168 inline PRUint32 RCInterval::ToMicroseconds() const 169 { 170 return PR_IntervalToMicroseconds(interval); 171 } 172 inline RCInterval::operator PRIntervalTime() const { 173 return interval; 174 } 175 176 inline PRIntervalTime RCInterval::FromSeconds(PRUint32 seconds) 177 { 178 return PR_SecondsToInterval(seconds); 179 } 180 inline PRIntervalTime RCInterval::FromMilliseconds(PRUint32 milli) 181 { 182 return PR_MillisecondsToInterval(milli); 183 } 184 inline PRIntervalTime RCInterval::FromMicroseconds(PRUint32 micro) 185 { 186 return PR_MicrosecondsToInterval(micro); 187 } 188 189 #endif /* defined(_RCINTERVAL_H) */ 190 191 /* RCInterval.h */