milliSeconds.h Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
milliSeconds.h
Go to the documentation of this file.
1 #ifndef OSL_MILLISECONDS_H
2 #define OSL_MILLISECONDS_H
3
4 #include "osl/misc/cstdint.h"
5 #include <string>
6 #include <stdexcept>
7 #include <cassert>
8 #include <limits>
9
10 namespace osl
11 {
12 namespace misc
13 {
14 struct NoMoreTime : std::runtime_error
15 {
16 NoMoreTime() : std::runtime_error("time limit over")
17 {
18 }
19 };
20
21 class MilliSeconds
22 {
23 int64_t msec;
24 public:
25 class Interval
26 {
27 int64_t interval;
28 public:
29 explicit Interval(int64_t m=std::numeric_limits<int64_t>::max()) : interval(m) {}
30 int64_t value() const { return interval; }
31 double toSeconds() const { return interval/1000.0; }
32 static const Interval infinity()
33 {
34 return Interval(std::numeric_limits<int64_t>::max());
35 }
36 bool isInfinity() const { return interval == std::numeric_limits<int64_t>::max(); }
37 const Interval operator+(Interval r) const { return Interval(interval + r.interval); }
38 const Interval operator-(Interval r) const { return Interval(interval - r.interval); }
39 const Interval operator*(int scale) const { return Interval(interval*scale); }
40 const Interval operator/(int scale) const { return Interval(interval/scale); }
41 };
42 explicit MilliSeconds(int64_t ms=0) : msec(ms) {}
43 int64_t value() const { return msec; }
44 const MilliSeconds operator+(int64_t diff) const { return MilliSeconds(value()+diff); }
45 const MilliSeconds operator-(int64_t diff) const { return MilliSeconds(value()-diff); }
46 const MilliSeconds operator+(Interval diff) const { return operator+(diff.value()); }
47 const MilliSeconds operator-(Interval diff) const { return operator-(diff.value()); }
48 const Interval operator-(MilliSeconds r) const
49 {
50 return Interval(value() - r.value());
51 }
52 static const MilliSeconds now();
53 double elapsedSeconds() const { return (now() - *this).toSeconds(); }
54 };
55 inline bool operator==(MilliSeconds l, MilliSeconds r)
56 {
57 return l.value() == r.value();
58 }
59 inline bool operator<(MilliSeconds l, MilliSeconds r)
60 {
61 return l.value() < r.value();
62 }
63 inline bool operator==(MilliSeconds::Interval l, MilliSeconds::Interval r)
64 {
65 return l.value() == r.value();
66 }
67 inline bool operator<(MilliSeconds::Interval l, MilliSeconds::Interval r)
68 {
69 return l.value() < r.value();
70 }
71 } // namespace misc
72 using misc::MilliSeconds;
73 } // namespace osl
74
75
76 #endif // OSL_MILLISECONDS_H
77 // ;;; Local Variables:
78 // ;;; mode:c++
79 // ;;; c-basic-offset:2
80 // ;;; End: