| 1 |
#pragma once |
| 2 |
|
| 3 |
#include <ctime> |
| 4 |
#include <ostream> |
| 5 |
|
| 6 |
#include "../base_types.hpp" |
| 7 |
#include "../operator.hpp" |
| 8 |
#include "../alibxx.hpp" |
| 9 |
|
| 10 |
#include "chrono_exception.hpp" |
| 11 |
|
| 12 |
namespace A_LIB_NAMESPACE{ |
| 13 |
|
| 14 |
namespace Chrono { |
| 15 |
|
| 16 |
class Clock; |
| 17 |
class Duration; |
| 18 |
class TimePoint; |
| 19 |
|
| 20 |
/// @class TimePoint |
| 21 |
/// represent a time position |
| 22 |
class TimePoint : public Operator::Equality<TimePoint, TimePoint>, |
| 23 |
public Operator::Compare<TimePoint, TimePoint>, |
| 24 |
public Operator::Subtractable<TimePoint, Duration>, |
| 25 |
public Operator::Addable<TimePoint, Duration> |
| 26 |
{ |
| 27 |
public: |
| 28 |
|
| 29 |
TimePoint(); |
| 30 |
~TimePoint(); |
| 31 |
|
| 32 |
bool operator==(const TimePoint & other) const; |
| 33 |
bool operator<(const TimePoint & other) const; |
| 34 |
|
| 35 |
// add time duration |
| 36 |
TimePoint & operator+=(const Duration & duration); |
| 37 |
TimePoint & operator-=(const Duration & duration); |
| 38 |
|
| 39 |
|
| 40 |
// extract duration from TimePoint |
| 41 |
// if time1 - time2 with time1 < time2 => throw ChronoException |
| 42 |
Duration operator-(const TimePoint & other) const; |
| 43 |
|
| 44 |
Type::UInt64 toTimestamp() const; |
| 45 |
|
| 46 |
bool isValid() const; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
private: |
| 51 |
struct timespec t; |
| 52 |
friend class Clock; |
| 53 |
friend class Duration; |
| 54 |
friend std::ostream& operator<<(std::ostream& os, const TimePoint & t); |
| 55 |
}; |
| 56 |
|
| 57 |
class Duration{ |
| 58 |
public: |
| 59 |
Duration(); |
| 60 |
Duration(Type::UInt64 seconds); |
| 61 |
~Duration(); |
| 62 |
|
| 63 |
static Duration milliseconds(Type::UInt64 v); |
| 64 |
|
| 65 |
/// return duration value in seconds |
| 66 |
Type::UInt64 toTimeValue() const; |
| 67 |
|
| 68 |
/// return duration value in milliseconds |
| 69 |
Type::UInt64 toMilliseconds() const; |
| 70 |
|
| 71 |
private: |
| 72 |
struct timespec t; |
| 73 |
friend class Clock; |
| 74 |
friend class TimePoint; |
| 75 |
friend std::ostream& operator<<(std::ostream& os, const Duration & d); |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
class Clock{ |
| 82 |
public: |
| 83 |
enum Type{ |
| 84 |
Monolitic, |
| 85 |
RealTime |
| 86 |
}; |
| 87 |
enum Precision{ |
| 88 |
Second, |
| 89 |
MilliSecond, |
| 90 |
MicroSecond, |
| 91 |
NanoSecond |
| 92 |
}; |
| 93 |
|
| 94 |
Clock(Type clock_type, Precision tick = Second); |
| 95 |
~Clock(); |
| 96 |
|
| 97 |
TimePoint now() const; |
| 98 |
|
| 99 |
private: |
| 100 |
Type _type; |
| 101 |
Precision _precision; |
| 102 |
}; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
std::ostream& operator<<(std::ostream& os, const Duration & d); |
| 107 |
std::ostream& operator<<(std::ostream& os, const TimePoint & d); |
| 108 |
|
| 109 |
|
| 110 |
} // namespace Chrono |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
} // A_LIB_NAMESPACE |