| 22 | namespace process { |
| 23 | |
| 24 | class Timeout |
| 25 | { |
| 26 | public: |
| 27 | Timeout() : timeout(Clock::now()) {} |
| 28 | |
| 29 | explicit Timeout(const Time& time) : timeout(time) {} |
| 30 | |
| 31 | Timeout(const Timeout& that) : timeout(that.timeout) {} |
| 32 | |
| 33 | // Constructs a Timeout instance from a Time that is the 'duration' |
| 34 | // from now. |
| 35 | static Timeout in(const Duration& duration) |
| 36 | { |
| 37 | // We need now() + duration < Duration::max() to avoid overflow. |
| 38 | // Therefore, we check for now() < duration::max() - duration. |
| 39 | if (Clock::now().duration() < Duration::max() - duration) { |
| 40 | return Timeout(Clock::now() + duration); |
| 41 | } |
| 42 | |
| 43 | return Timeout(Time::max()); |
| 44 | } |
| 45 | |
| 46 | Timeout& operator=(const Timeout& that) |
| 47 | { |
| 48 | if (this != &that) { |
| 49 | timeout = that.timeout; |
| 50 | } |
| 51 | |
| 52 | return *this; |
| 53 | } |
| 54 | |
| 55 | Timeout& operator=(const Duration& duration) |
| 56 | { |
| 57 | timeout = Clock::now() + duration; |
| 58 | return *this; |
| 59 | } |
| 60 | |
| 61 | bool operator==(const Timeout& that) const |
| 62 | { |
| 63 | return timeout == that.timeout; |
| 64 | } |
| 65 | |
| 66 | bool operator<(const Timeout& that) const |
| 67 | { |
| 68 | return timeout < that.timeout; |
| 69 | } |
| 70 | |
| 71 | bool operator<=(const Timeout& that) const |
| 72 | { |
| 73 | return timeout <= that.timeout; |
| 74 | } |
| 75 | |
| 76 | // Returns the value of the timeout as a Time object. |
| 77 | Time time() const |
| 78 | { |
| 79 | return timeout; |
| 80 | } |
| 81 | |