| 1 |
#ifndef PROTON_DURATION_HPP |
| 2 |
#define PROTON_DURATION_HPP |
| 3 |
|
| 4 |
/* |
| 5 |
* |
| 6 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 7 |
* or more contributor license agreements. See the NOTICE file |
| 8 |
* distributed with this work for additional information |
| 9 |
* regarding copyright ownership. The ASF licenses this file |
| 10 |
* to you under the Apache License, Version 2.0 (the |
| 11 |
* "License"); you may not use this file except in compliance |
| 12 |
* with the License. You may obtain a copy of the License at |
| 13 |
* |
| 14 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 15 |
* |
| 16 |
* Unless required by applicable law or agreed to in writing, |
| 17 |
* software distributed under the License is distributed on an |
| 18 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 19 |
* KIND, either express or implied. See the License for the |
| 20 |
* specific language governing permissions and limitations |
| 21 |
* under the License. |
| 22 |
* |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "./internal/export.hpp" |
| 26 |
#include "./internal/comparable.hpp" |
| 27 |
#include "./types_fwd.hpp" |
| 28 |
|
| 29 |
#include <proton/type_compat.h> |
| 30 |
|
| 31 |
#include <iosfwd> |
| 32 |
|
| 33 |
/// @file |
| 34 |
/// @copybrief proton::duration |
| 35 |
|
| 36 |
namespace proton { |
| 37 |
|
| 38 |
/// A span of time in milliseconds. |
| 39 |
class duration : private internal::comparable<duration> { |
| 40 |
public: |
| 41 |
/// A numeric type holding a value in milliseconds. |
| 42 |
typedef int64_t numeric_type; |
| 43 |
|
| 44 |
/// Construct from a value in milliseconds. |
| 45 |
explicit duration(numeric_type ms = 0) : ms_(ms) {} |
| 46 |
|
| 47 |
/// Assign a value in milliseconds. |
| 48 |
duration& operator=(numeric_type ms) { ms_ = ms; return *this; } |
| 49 |
|
| 50 |
/// Get the value in milliseconds. |
| 51 |
numeric_type milliseconds() const { return ms_; } |
| 52 |
|
| 53 |
PN_CPP_EXTERN static const duration FOREVER; ///< Wait forever |
| 54 |
PN_CPP_EXTERN static const duration IMMEDIATE; ///< Don't wait at all |
| 55 |
PN_CPP_EXTERN static const duration SECOND; ///< One second |
| 56 |
PN_CPP_EXTERN static const duration MILLISECOND; ///< One millisecond |
| 57 |
PN_CPP_EXTERN static const duration MINUTE; ///< One minute |
| 58 |
|
| 59 |
private: |
| 60 |
numeric_type ms_; |
| 61 |
}; |
| 62 |
|
| 63 |
/// Print a duration. |
| 64 |
PN_CPP_EXTERN std::ostream& operator<<(std::ostream&, duration); |
| 65 |
|
| 66 |
/// @name Comparison and arithmetic operators |
| 67 |
/// @{ |
| 68 |
inline bool operator<(duration x, duration y) { return x.milliseconds() < y.milliseconds(); } |
| 69 |
inline bool operator==(duration x, duration y) { return x.milliseconds() == y.milliseconds(); } |
| 70 |
|
| 71 |
inline duration operator+(duration x, duration y) { return duration(x.milliseconds() + y.milliseconds()); } |
| 72 |
inline duration operator-(duration x, duration y) { return duration(x.milliseconds() - y.milliseconds()); } |
| 73 |
inline duration operator*(duration d, uint64_t n) { return duration(d.milliseconds()*n); } |
| 74 |
inline duration operator*(uint64_t n, duration d) { return d * n; } |
| 75 |
inline duration operator/(duration d, uint64_t n) { return duration(d.milliseconds() / n); } |
| 76 |
/// @} |
| 77 |
|
| 78 |
} // proton |
| 79 |
|
| 80 |
#endif // PROTON_DURATION_HPP |