/*
 * This file is part of the Owl Positioning System (OwlPS) project.
 * It is subject to the copyright notice and license terms in the
 * COPYRIGHT.t2t file found in the top-level directory of this
 * distribution and at
 *   https://code.lm7.fr/mcy/owlps/src/master/COPYRIGHT.t2t
 * No part of the OwlPS Project, including this file, may be copied,
 * modified, propagated, or distributed except according to the terms
 * contained in the COPYRIGHT.t2t file; the COPYRIGHT.t2t file must be
 * distributed along with this file, either separately or by replacing
 * this notice by the COPYRIGHT.t2t file's contents.
 */


#ifndef _OWLPS_POSITIONING_TIMESTAMP_HH_
#define _OWLPS_POSITIONING_TIMESTAMP_HH_

#include <owlps.h>

#include <ctime>
#include <stdint.h> // <cstdint> is not C++ 98 compliant
#include <ostream>

/// Represents a timestamp with a nanosecond precision
/**
 * This class boxes <em>struct timespec</em> 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) ;
  /// 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 `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 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<uint64_t>(*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<uint64_t>(*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<uint64_t>(timestamp.tv_sec) * 1000 +
    static_cast<uint64_t>(timestamp.tv_nsec) / 1000000 ;
}



namespace std
{
template<> struct hash<Timestamp>
{
public:
  size_t operator()(const Timestamp &source) const
  {
    hash<uint64_t> h ;
    return h(source) ;
  }
} ;
}



#endif // _OWLPS_POSITIONING_TIMESTAMP_HH_
