/* * This file is part of the Owl Positioning System (OwlPS). * OwlPS is a project of the University of Franche-Comte * (Université de Franche-Comté), France. * * Copyright © Université de Franche-Comté 2007-2012. * * Corresponding author: Matteo Cypriani * *********************************************************************** * * This software is governed by the CeCILL license under French law and * abiding by the rules of distribution of free software. You can use, * modify and/or redistribute the software under the terms of the CeCILL * license as circulated by CEA, CNRS and INRIA at the following URL: * http://www.cecill.info * * As a counterpart to the access to the source code and rights to copy, * modify and redistribute granted by the license, users are provided * only with a limited warranty and the software's authors, the holder * of the economic rights, and the successive licensors have only * limited liability. * * In this respect, the user's attention is drawn to the risks * associated with loading, using, modifying and/or developing or * reproducing the software by the user in light of its specific status * of free software, that may mean that it is complicated to manipulate, * and that also therefore means that it is reserved for developers and * experienced professionals having in-depth computer knowledge. Users * are therefore encouraged to load and test the software's suitability * as regards their requirements in conditions enabling the security of * their systems and/or data to be ensured and, more generally, to use * and operate it in the same conditions as regards security. * * The fact that you are presently reading this means that you have had * knowledge of the CeCILL license and that you accept its terms. * *********************************************************************** */ #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 { private: static Timestamp current_time ; 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 before(const struct timespec &source) const ; bool after(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) {} /** @name Operations */ //@{ /// Returns a Timestamp containing the current time static Timestamp get_current_time(void) ; /// Sets #current_time to a new timestamp static void update_current_time(const Timestamp &source) ; /// \brief Initialises #timestamp at the current time whith a /// nanosecond precision bool now(void) ; /// Returns the time elapsed until now Timestamp elapsed(void) const ; /// Returns the time elapsed since or until \em source Timestamp elapsed(const Timestamp &source) const ; /// Resets #timestamp 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 ; /// Casts to the owl_timestamp type from libowlps operator owl_timestamp(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 before(source.timestamp) ; } inline bool Timestamp::operator<(const struct timespec &source) const { return before(source) ; } inline bool Timestamp::operator<(const uint64_t source) const { return static_cast(*this) < source ; } inline bool Timestamp::operator>(const Timestamp &source) const { return after(source.timestamp) ; } inline bool Timestamp::operator>(const struct timespec &source) const { return after(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_