#ifndef _OWLPS_POSITIONING_TIMESTAMP_HH_ #define _OWLPS_POSITIONING_TIMESTAMP_HH_ #include #include #include // is not C++ 98 compliant #include /// Represents a timestamp with a nanosecond precision /** * This class boxes struct timespec from ctime (time.h) to * integrate time manipulation functions. */ class Timestamp { protected: /// Time data struct timespec timestamp ; /** @name Internal accessors */ //@{ void set(const struct timespec &source) ; void set(const owl_timestamp &source) ; void set(const uint_fast32_t source_s, const uint_fast32_t source_ns) ; /// Initialises the Timestamp with a value in milliseconds (deprecated) void set(const uint64_t source_ms) ; //@} /** @name Comparison functions */ //@{ bool equals(const struct timespec &source) const ; /// Compares to a millisecond value (deprecated) bool equals(const uint64_t source) const ; bool less_than(const struct timespec &source) const ; bool greater_than(const struct timespec &source) const ; //@} /** @name Operations */ //@{ /// Lowers the precision of #timestamp to ms (deprecated) void round_to_ms(void) ; //@} public: Timestamp(void) ; Timestamp(const struct timespec &source) ; Timestamp(const owl_timestamp &source) ; Timestamp(const uint_fast32_t source_s, const uint_fast32_t source_ns) ; /// Constructs a Timsestamp from a value in milliseconds (deprecated) Timestamp(const uint64_t source) ; Timestamp(const Timestamp &source) ; ~Timestamp(void) {} /// \brief Initialises #timestamp at the current time whith a /// nanosecond precision bool now(void) ; void clear(void) ; /** @name Operators */ //@{ Timestamp& operator=(const Timestamp &source) ; Timestamp& operator=(const struct timespec &source) ; Timestamp& operator=(const uint64_t source) ; //< deprecated bool operator==(const Timestamp &source) const ; bool operator==(const struct timespec &source) const ; bool operator==(const uint64_t source) const ; //< deprecated bool operator!=(const Timestamp &source) const ; bool operator!=(const struct timespec &source) const ; bool operator!=(const uint64_t source) const ; //< deprecated bool operator<(const Timestamp &source) const ; bool operator<(const struct timespec &source) const ; bool operator<(const uint64_t source) const ; //< deprecated bool operator>(const Timestamp &source) const ; bool operator>(const struct timespec &source) const ; bool operator>(const uint64_t source) const ; //< deprecated bool operator<=(const Timestamp &source) const ; bool operator<=(const struct timespec &source) const ; bool operator<=(const uint64_t source) const ; //< deprecated bool operator>=(const Timestamp &source) const ; bool operator>=(const struct timespec &source) const ; bool operator>=(const uint64_t source) const ; //< deprecated operator bool(void) const ; operator const struct timespec&(void) const ; /// Casts to milliseconds operator uint64_t(void) const ; //@} /// Displays a Timestamp friend std::ostream& operator<<(std::ostream &os, const Timestamp &t) ; // Hashes a Timestamp friend size_t hash_value(const Timestamp &source) ; } ; inline void Timestamp::clear(void) { timestamp.tv_sec = 0 ; timestamp.tv_nsec = 0 ; } /* *** Operators *** */ inline Timestamp& Timestamp::operator=(const struct timespec &source) { set(source) ; return *this ; } inline Timestamp& Timestamp::operator=(const uint64_t source) { set(source) ; return *this ; } inline bool Timestamp::operator==(const struct timespec &source) const { return equals(source) ; } inline bool Timestamp::operator==(const uint64_t source) const { return equals(source) ; } inline bool Timestamp::operator!=(const Timestamp &source) const { return !(*this == source) ; } inline bool Timestamp::operator!=(const struct timespec &source) const { return !(*this == source) ; } inline bool Timestamp::operator!=(const uint64_t source) const { return !(*this == source) ; } inline bool Timestamp::operator<(const Timestamp &source) const { return less_than(source.timestamp) ; } inline bool Timestamp::operator<(const struct timespec &source) const { return less_than(source) ; } inline bool Timestamp::operator<(const uint64_t source) const { return static_cast(*this) < source ; } inline bool Timestamp::operator>(const Timestamp &source) const { return greater_than(source.timestamp) ; } inline bool Timestamp::operator>(const struct timespec &source) const { return greater_than(source) ; } inline bool Timestamp::operator>(const uint64_t source) const { return static_cast(*this) > source ; } inline bool Timestamp::operator<=(const Timestamp &source) const { return *this == source || *this < source ; } inline bool Timestamp::operator<=(const struct timespec &source) const { return *this == source || *this < source ; } inline bool Timestamp::operator<=(const uint64_t source) const { return *this == source || *this < source ; } inline bool Timestamp::operator>=(const Timestamp &source) const { return source <= *this ; } inline bool Timestamp::operator>=(const struct timespec &source) const { return *this == source || !(*this < source) ; } inline bool Timestamp::operator>=(const uint64_t source) const { return *this == source || !(*this < source) ; } inline Timestamp::operator bool() const { return timestamp.tv_sec > 0 || timestamp.tv_nsec > 0 ; } inline Timestamp::operator const struct timespec&() const { return timestamp ; } inline Timestamp::operator uint64_t(void) const { return static_cast(timestamp.tv_sec) * 1000 + static_cast(timestamp.tv_nsec) / 1000000 ; } #endif // _OWLPS_POSITIONING_TIMESTAMP_HH_