owlps/owlps-positioning/src/timestamp.hh

129 lines
2.8 KiB
C++
Raw Normal View History

#ifndef _OWLPS_POSITIONING_TIMESTAMP_HH_
#define _OWLPS_POSITIONING_TIMESTAMP_HH_
#include <ctime>
#include <stdint.h> // <cstdint> is not C++ 98 compliant
#include <ostream>
/// Represents a timestamp with a millisecond precision
/**
* This class boxes <em>struct timespec</em> from <ctime> to integrate
* time manipulation functions.
*/
class Timestamp
{
protected:
/// Time data
struct timespec timestamp ;
/** @name Operations */
//@{
/// \brief Initialises #timestamp at the current time whith a
/// nanosecond precision
bool now_ns(void) ;
/// Lowers the precision of #timestamp to ms
void round_to_ms(void) ;
//@}
public:
/// Default constructor
Timestamp(void) ;
/// Constructs a Timestamp from a struct timespec
Timestamp(const struct timespec &source) ;
/// Constructs a Timsestamp from a value in milliseconds
Timestamp(const uint64_t source_ms) ;
/// Copy constructor
Timestamp(const Timestamp &source) ;
~Timestamp(void) {} ///< Destructor (do nothing)
/** @name Read accessors */
//@{
/// #timestamp read accessor
const struct timespec& get_timestamp(void) const ;
/// Get the #timestamp in milliseconds
uint64_t get_timestamp_ms(void) const ;
//@}
/** @name Write accessors */
//@{
/// #timestamp write accessor
void set_timestamp(const struct timespec &source) ;
/// #timestamp write accessor with a value in milliseconds
void set_timestamp_ms(const uint64_t source_ms) ;
//@}
/** @name Operations */
//@{
/// \brief Initialises #timestamp to the current time with a
/// millisecond precision
bool now(void) ;
//@}
/** @name Operators */
//@{
const Timestamp& operator=(const Timestamp &source) ;
bool operator==(const Timestamp &source) const ;
bool operator!=(const Timestamp &source) const ;
bool operator<(const Timestamp &source) const ;
bool operator>(const Timestamp &source) const ;
bool operator<=(const Timestamp &source) const ;
bool operator>=(const Timestamp &source) const ;
//@}
/// Display a Timestamp
friend std::ostream& operator<<(std::ostream &os, const Timestamp &t) ;
} ;
/* *** Read accessors *** */
inline const struct timespec& Timestamp::get_timestamp() const
{
return timestamp ;
}
/* *** Write accessors *** */
inline void Timestamp::set_timestamp(const struct timespec &source)
{
timestamp = source ;
}
/* *** Operators *** */
inline bool Timestamp::operator!=(const Timestamp &source) const
{
return !(*this == source) ;
}
inline bool Timestamp::operator>(const Timestamp &source) const
{
return source < *this ;
}
inline bool Timestamp::operator<=(const Timestamp &source) const
{
return *this == source || *this < source ;
}
inline bool Timestamp::operator>=(const Timestamp &source) const
{
return source <= *this ;
}
#endif // _OWLPS_POSITIONING_TIMESTAMP_HH_