Sirikata
|
00001 /* Sirikata Kernel -- Task scheduling system 00002 * Time.hpp 00003 * 00004 * Copyright (c) 2008, Patrick Reiter Horn 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions are 00009 * met: 00010 * * Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * * Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in 00014 * the documentation and/or other materials provided with the 00015 * distribution. 00016 * * Neither the name of Sirikata nor the names of its contributors may 00017 * be used to endorse or promote products derived from this software 00018 * without specific prior written permission. 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 00021 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 00022 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00023 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 00024 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00025 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00026 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 */ 00032 00033 #ifndef SIRIKATA_Time_HPP__ 00034 #define SIRIKATA_Time_HPP__ 00035 00036 namespace Sirikata { 00037 00042 namespace Task { 00043 00044 00055 class SIRIKATA_EXPORT DeltaTime { 00056 int64 mDeltaTime; 00057 00058 explicit DeltaTime(int64 us) 00059 : mDeltaTime(us) 00060 { 00061 } 00062 public: 00063 DeltaTime() 00064 : mDeltaTime(0) 00065 { 00066 } 00067 00068 inline double operator/ (const DeltaTime&other)const { 00069 return (double)(mDeltaTime/other.mDeltaTime)+(double)(mDeltaTime%other.mDeltaTime)/(double)other.mDeltaTime; 00070 } 00071 00072 inline DeltaTime operator/(float32 rhs) const { 00073 return DeltaTime((int64)((float64)mDeltaTime / (float64)rhs)); 00074 } 00075 inline void operator/=(float32 rhs) { 00076 mDeltaTime = (int64)((float64)mDeltaTime / (float64)rhs); 00077 } 00078 inline DeltaTime operator/(float64 rhs) const { 00079 return DeltaTime((int64)((float64)mDeltaTime / (float64)rhs)); 00080 } 00081 inline void operator/=(float64 rhs) { 00082 mDeltaTime = (int64)((float64)mDeltaTime / (float64)rhs); 00083 } 00084 inline DeltaTime operator/(int32 rhs) const { 00085 return (*this)/(float64)rhs; 00086 } 00087 inline void operator/=(int32 rhs) { 00088 (*this) /= (float64)rhs; 00089 } 00090 inline DeltaTime operator/(uint32 rhs) const { 00091 return (*this)/(float64)rhs; 00092 } 00093 inline void operator/=(uint32 rhs) { 00094 (*this) /= (float64)rhs; 00095 } 00096 inline DeltaTime operator/(int64 rhs) const { 00097 return (*this)/(float64)rhs; 00098 } 00099 inline void operator/=(int64 rhs) { 00100 (*this) /= (float64)rhs; 00101 } 00102 inline DeltaTime operator/(uint64 rhs) const { 00103 return (*this)/(float64)rhs; 00104 } 00105 inline void operator/=(uint64 rhs) { 00106 (*this) /= (float64)rhs; 00107 } 00108 00109 inline DeltaTime operator*(float32 rhs) const { 00110 return DeltaTime((int64)((float64)mDeltaTime * (float64)rhs)); 00111 } 00112 inline void operator*=(float32 rhs) { 00113 mDeltaTime = (int64)((float64)mDeltaTime * (float64)rhs); 00114 } 00115 inline DeltaTime operator*(float64 rhs) const { 00116 return DeltaTime((int64)((float64)mDeltaTime * (float64)rhs)); 00117 } 00118 inline void operator*=(float64 rhs) { 00119 mDeltaTime = (int64)((float64)mDeltaTime * (float64)rhs); 00120 } 00121 inline DeltaTime operator*(int32 rhs) const { 00122 return (*this) * (float64)rhs; 00123 } 00124 inline void operator*=(int32 rhs) { 00125 (*this) *= (float64)rhs; 00126 } 00127 inline DeltaTime operator*(uint32 rhs) const { 00128 return (*this) * (float64)rhs; 00129 } 00130 inline void operator*=(uint32 rhs) { 00131 (*this) *= (float64)rhs; 00132 } 00133 inline DeltaTime operator*(int64 rhs) const { 00134 return (*this) * (float64)rhs; 00135 } 00136 inline void operator*=(int64 rhs) { 00137 (*this) *= (float64)rhs; 00138 } 00139 inline DeltaTime operator*(uint64 rhs) const { 00140 return (*this) * (float64)rhs; 00141 } 00142 inline void operator*=(uint64 rhs) { 00143 (*this) *= (float64)rhs; 00144 } 00145 00146 static DeltaTime hours(double s) { 00147 return minutes(s*60); 00148 } 00149 static DeltaTime minutes(double s) { 00150 return seconds(s*60); 00151 } 00152 static DeltaTime seconds(double s) { 00153 return DeltaTime((int64)(s*1000000.)); 00154 } 00155 static DeltaTime milliseconds(double ms) { 00156 return DeltaTime((int64)(ms*1000.)); 00157 } 00158 static DeltaTime milliseconds(int64 ms) { 00159 return DeltaTime(ms*1000); 00160 } 00161 static DeltaTime milliseconds(uint64 ms) { 00162 return DeltaTime(ms*1000); 00163 } 00164 static DeltaTime milliseconds(int32 ms) { 00165 return DeltaTime(((int64)ms)*1000); 00166 } 00167 static DeltaTime milliseconds(uint32 ms) { 00168 return DeltaTime(((int64)ms)*1000); 00169 } 00170 static DeltaTime microseconds(double us) { 00171 return DeltaTime((int64)us); 00172 } 00173 static DeltaTime microseconds(int64 us) { 00174 return DeltaTime(us); 00175 } 00176 static DeltaTime microseconds(uint64 us) { 00177 return DeltaTime(us); 00178 } 00179 static DeltaTime microseconds(int32 us) { 00180 return DeltaTime((int64)us); 00181 } 00182 static DeltaTime microseconds(uint32 us) { 00183 return DeltaTime((int64)us); 00184 } 00185 static DeltaTime nanoseconds(int64 ns) { 00186 return DeltaTime(ns/1000); 00187 } 00188 static DeltaTime nanoseconds(uint64 ns) { 00189 return DeltaTime(ns/1000); 00190 } 00191 static DeltaTime nanoseconds(int32 ns) { 00192 return DeltaTime(((int64)ns)/1000); 00193 } 00194 static DeltaTime nanoseconds(uint32 ns) { 00195 return DeltaTime(((int64)ns)/1000); 00196 } 00197 00199 static DeltaTime zero() { 00200 return DeltaTime::nanoseconds(0); 00201 } 00202 00204 class LocalTime fromNow() const; 00205 00207 inline DeltaTime operator- (const DeltaTime &other) const { 00208 return DeltaTime(mDeltaTime - other.mDeltaTime); 00209 } 00211 inline DeltaTime operator+ (const DeltaTime &other) const { 00212 return DeltaTime(mDeltaTime + other.mDeltaTime); 00213 } 00215 inline DeltaTime operator+= (const DeltaTime &other) { 00216 return DeltaTime(mDeltaTime += other.mDeltaTime); 00217 } 00219 inline DeltaTime operator-= (const DeltaTime &other) { 00220 return DeltaTime(mDeltaTime -= other.mDeltaTime); 00221 } 00223 inline DeltaTime operator- () const { 00224 return DeltaTime(-mDeltaTime); 00225 } 00226 double toSeconds () const { 00227 return mDeltaTime/1000000.; 00228 } 00229 double seconds() const { 00230 return toSeconds(); 00231 } 00233 int64 toMilliseconds() const { 00234 return mDeltaTime/1000; 00235 } 00236 int64 milliseconds() const { 00237 return toMilliseconds(); 00238 } 00240 int64 toMicroseconds() const { 00241 return mDeltaTime; 00242 } 00243 int64 microseconds() const { 00244 return toMicroseconds(); 00245 } 00247 int64 toMicro() const { 00248 return toMicroseconds(); 00249 } 00250 00252 inline bool operator== (const DeltaTime &other) const { 00253 return (mDeltaTime == other.mDeltaTime); 00254 } 00255 inline bool operator!= (const DeltaTime& other) const { 00256 return (mDeltaTime != other.mDeltaTime); 00257 } 00259 inline bool operator< (const DeltaTime &other) const { 00260 return (mDeltaTime < other.mDeltaTime); 00261 } 00262 inline bool operator<= (const DeltaTime &other) const { 00263 return (mDeltaTime <= other.mDeltaTime); 00264 } 00265 inline bool operator> (const DeltaTime &other) const { 00266 return (mDeltaTime > other.mDeltaTime); 00267 } 00268 inline bool operator>= (const DeltaTime &other) const { 00269 return (mDeltaTime >= other.mDeltaTime); 00270 } 00271 00272 String toString() const; 00273 }; 00274 00287 class SIRIKATA_EXPORT LocalTime { 00288 00289 uint64 mTime; 00290 protected: 00292 explicit LocalTime(uint64 t) { 00293 this->mTime = t; 00294 } 00295 00296 public: 00297 LocalTime() 00298 : mTime(0) 00299 {} 00300 00301 uint64 raw() const { 00302 return mTime; 00303 } 00304 00306 inline bool operator== (const LocalTime &other) const { 00307 return (mTime == other.mTime); 00308 } 00309 inline bool operator!= (const LocalTime& other) const { 00310 return (mTime != other.mTime); 00311 } 00312 00314 inline bool operator< (const LocalTime &other) const { 00315 return (mTime < other.mTime); 00316 } 00317 inline bool operator<= (const LocalTime &other) const { 00318 return (mTime <= other.mTime); 00319 } 00320 inline bool operator> (const LocalTime &other) const { 00321 return (mTime > other.mTime); 00322 } 00323 inline bool operator>= (const LocalTime &other) const { 00324 return (mTime >= other.mTime); 00325 } 00326 00332 inline DeltaTime operator- (const LocalTime &other) const { 00333 return DeltaTime::microseconds((int64)mTime - (int64)other.mTime); 00334 } 00340 inline LocalTime operator+ (const DeltaTime &otherDelta) const { 00341 return LocalTime(mTime + otherDelta.toMicro()); 00342 } 00343 inline LocalTime operator- (const DeltaTime &otherDelta) const { 00344 return (*this) + (-otherDelta); 00345 } 00346 inline void operator+= (const DeltaTime &otherDelta) { 00347 mTime += otherDelta.toMicro(); 00348 } 00349 inline void operator-= (const DeltaTime &otherDelta) { 00350 (*this) += (-otherDelta); 00351 } 00352 00353 static LocalTime microseconds(int64 abstime){ 00354 return LocalTime(abstime); 00355 } 00356 00363 static LocalTime now(); // Only way to generate an LocalTime for now... 00364 00370 static LocalTime epoch() { return LocalTime(0); } 00371 00379 static LocalTime null() { return LocalTime(0); } 00380 00381 00382 }; 00383 00384 SIRIKATA_EXPORT std::ostream& operator<<(std::ostream& os, const DeltaTime& rhs); 00385 SIRIKATA_EXPORT std::istream& operator>>(std::istream& is, DeltaTime& rhs); 00386 inline LocalTime DeltaTime::fromNow() const { 00387 return LocalTime::now() + (*this); 00388 } 00389 00390 } 00391 } 00392 00393 #endif