00001 #ifndef OSL_MILLISECONDS_H 00002 #define OSL_MILLISECONDS_H 00003 00004 #include "osl/misc/cstdint.h" 00005 #include <string> 00006 #include <stdexcept> 00007 #include <cassert> 00008 #include <limits> 00009 00010 namespace osl 00011 { 00012 namespace misc 00013 { 00014 struct NoMoreTime : std::runtime_error 00015 { 00016 NoMoreTime() : std::runtime_error("time limit over") 00017 { 00018 } 00019 }; 00020 00021 class MilliSeconds 00022 { 00023 int64_t msec; 00024 public: 00025 class Interval 00026 { 00027 int64_t interval; 00028 public: 00029 explicit Interval(int64_t m=std::numeric_limits<int64_t>::max()) : interval(m) {} 00030 int64_t value() const { return interval; } 00031 double toSeconds() const { return interval/1000.0; } 00032 static const Interval infinity() 00033 { 00034 return Interval(std::numeric_limits<int64_t>::max()); 00035 } 00036 bool isInfinity() const { return interval == std::numeric_limits<int64_t>::max(); } 00037 const Interval operator+(Interval r) const { return Interval(interval + r.interval); } 00038 const Interval operator-(Interval r) const { return Interval(interval - r.interval); } 00039 const Interval operator*(int scale) const { return Interval(interval*scale); } 00040 const Interval operator/(int scale) const { return Interval(interval/scale); } 00041 }; 00042 explicit MilliSeconds(int64_t ms=0) : msec(ms) {} 00043 int64_t value() const { return msec; } 00044 const MilliSeconds operator+(int64_t diff) const { return MilliSeconds(value()+diff); } 00045 const MilliSeconds operator-(int64_t diff) const { return MilliSeconds(value()-diff); } 00046 const MilliSeconds operator+(Interval diff) const { return operator+(diff.value()); } 00047 const MilliSeconds operator-(Interval diff) const { return operator-(diff.value()); } 00048 const Interval operator-(MilliSeconds r) const 00049 { 00050 return Interval(value() - r.value()); 00051 } 00052 static const MilliSeconds now(); 00053 double elapsedSeconds() const { return (now() - *this).toSeconds(); } 00054 }; 00055 inline bool operator==(MilliSeconds l, MilliSeconds r) 00056 { 00057 return l.value() == r.value(); 00058 } 00059 inline bool operator<(MilliSeconds l, MilliSeconds r) 00060 { 00061 return l.value() < r.value(); 00062 } 00063 inline bool operator==(MilliSeconds::Interval l, MilliSeconds::Interval r) 00064 { 00065 return l.value() == r.value(); 00066 } 00067 inline bool operator<(MilliSeconds::Interval l, MilliSeconds::Interval r) 00068 { 00069 return l.value() < r.value(); 00070 } 00071 } // namespace misc 00072 using misc::MilliSeconds; 00073 } // namespace osl 00074 00075 00076 #endif // OSL_MILLISECONDS_H 00077 // ;;; Local Variables: 00078 // ;;; mode:c++ 00079 // ;;; c-basic-offset:2 00080 // ;;; End: