1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
#ifndef MOMENT_HH
#define MOMENT_HH
#include <X11/Xos.h>
class Moment {
long _sec;
long _usec;
static const long MicroPerSec = 1000000;
static Moment genesis;
Moment(Moment *);
public:
Moment() { }
Moment(long sec, long usec = 0) : _sec(sec), _usec(usec) { }
Moment(const struct timeval &t) : _sec(t.tv_sec), _usec(t.tv_usec) { }
static Moment now();
operator struct timeval() const;
long sec() const { return _sec; }
long usec() const { return _usec; }
Moment &operator+=(const Moment &);
Moment &operator-=(const Moment &);
friend Moment operator+(const Moment &, const Moment &);
friend Moment operator-(const Moment &, const Moment &);
friend bool operator>=(const Moment &, const Moment &);
friend bool operator>(const Moment &, const Moment &);
};
inline Moment &
Moment::operator+=(const Moment &a)
{
_sec += a._sec;
if ((_usec += a._usec) >= MicroPerSec) {
_sec++;
_usec -= MicroPerSec;
}
return *this;
}
inline Moment &
Moment::operator-=(const Moment &a)
{
_sec -= a._sec;
if ((_usec -= a._usec) < 0) {
_sec--;
_usec += MicroPerSec;
}
return *this;
}
inline Moment
operator+(const Moment &a, const Moment &b)
{
Moment c = a;
return c += b;
}
inline Moment
operator-(const Moment &a, const Moment &b)
{
Moment c = a;
return c -= b;
}
inline bool
operator>=(const Moment &a, const Moment &b)
{
return a._sec > b._sec || (a._sec == b._sec && a._usec >= b._usec);
}
inline bool
operator>(const Moment &a, const Moment &b)
{
return a._sec > b._sec || (a._sec == b._sec && a._usec > b._usec);
}
inline
Moment::operator struct timeval() const
{
struct timeval t;
t.tv_sec = _sec;
t.tv_usec = _usec;
return t;
}
#endif
|