Honeycomb  0.1
Component-Model Framework
TimePoint.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
5 
6 namespace honey
7 {
8 
10 template<class Clock_, class Dur = typename Clock_::Duration>
11 class TimePoint
12 {
13  friend class TimePoint;
14 public:
15  typedef Clock_ Clock;
16  typedef Dur Duration;
17 
19  constexpr TimePoint() : _dur(Duration::zero()) {}
21  constexpr explicit TimePoint(const Duration& d) : _dur(d) {}
23  template<class Dur2>
24  constexpr TimePoint(const TimePoint<Clock, Dur2>& t) : _dur(t._dur) {}
26  template<class Clock2, class Dur2>
28 
29  template<class Dur2>
30  TimePoint& operator=(const TimePoint<Clock, Dur2>& rhs) { _dur = rhs._dur; return *this; }
31  template<class Clock2, class Dur2>
32  TimePoint& operator=(const TimePoint<Clock2, Dur2>& rhs) { _dur = Clock::now() + (rhs._dur - Clock2::now()); return *this; }
33 
34  TimePoint& operator+=(const Duration& rhs) { _dur += rhs; return *this; }
35  TimePoint& operator-=(const Duration& rhs) { _dur -= rhs; return *this; }
36 
37  template<class Dur2>
38  constexpr bool operator==(const TimePoint<Clock,Dur2>& rhs) const { return _dur == rhs._dur; }
39 
40  template<class Dur2>
41  constexpr bool operator!=(const TimePoint<Clock,Dur2>& rhs) const { return !(*this == rhs); }
42 
43  template<class Dur2>
44  constexpr bool operator<(const TimePoint<Clock,Dur2>& rhs) const { return _dur < rhs._dur; }
45 
46  template<class Dur2>
47  constexpr bool operator>(const TimePoint<Clock,Dur2>& rhs) const { return rhs < *this; }
48 
49  template<class Dur2>
50  constexpr bool operator<=(const TimePoint<Clock,Dur2>& rhs) const { return !(rhs < *this); }
51 
52  template<class Dur2>
53  constexpr bool operator>=(const TimePoint<Clock,Dur2>& rhs) const { return !(*this < rhs); }
54 
56  constexpr Duration time() const { return _dur; }
57 
59  static constexpr TimePoint min() { return TimePoint(Dur(numeral<typename Dur::Rep>().min())); }
61  static constexpr TimePoint max() { return TimePoint(Dur(numeral<typename Dur::Rep>().max())); }
62 
63 private:
64  Duration _dur;
65 };
66 
70 
72 template<class Clock, class Dur, class Rep, class Period,
73  class TimeCommon = TimePoint<Clock, typename std::common_type<Dur, Duration<Rep,Period>>::type>>
74 constexpr TimeCommon operator+(const TimePoint<Clock,Dur>& lhs, const Duration<Rep,Period>& rhs)
75 {
76  return TimeCommon(lhs.time() + rhs);
77 }
79 template<class Clock, class Dur, class Rep, class Period,
81 constexpr TimeCommon operator+(const Duration<Rep,Period>& lhs, const TimePoint<Clock,Dur>& rhs)
82 {
83  return rhs + lhs;
84 }
86 template<class Clock, class Dur, class Rep, class Period,
88 constexpr TimeCommon operator-(const TimePoint<Clock,Dur>& lhs, const Duration<Rep,Period>& rhs)
89 {
90  return lhs + (-rhs);
91 }
93 template<class Clock, class Dur, class Dur2,
94  class DurCommon = typename std::common_type<Dur, Dur2>::type>
95 constexpr DurCommon operator-(const TimePoint<Clock,Dur>& lhs, const TimePoint<Clock,Dur2>& rhs)
96 {
97  return lhs.time() - rhs.time();
98 }
100 
101 }
102 
TimePoint & operator-=(const Duration &rhs)
Definition: TimePoint.h:35
TimePoint represented by a duration since a clock's epoch time.
Definition: TimePoint.h:11
static constexpr TimePoint min()
Minimum time point (negative duration)
Definition: TimePoint.h:59
constexpr TimeCommon operator-(const TimePoint< Clock, Dur > &lhs, const Duration< Rep, Period > &rhs)
operator-(TimePoint, Duration). Returns a time point of best-fit duration.
Definition: TimePoint.h:88
Clock_ Clock
Definition: TimePoint.h:15
constexpr bool operator!=(const TimePoint< Clock, Dur2 > &rhs) const
Definition: TimePoint.h:41
TimePoint & operator=(const TimePoint< Clock2, Dur2 > &rhs)
Definition: TimePoint.h:32
constexpr TimePoint(const Duration &d)
Construct with a duration from clock's epoch.
Definition: TimePoint.h:21
static constexpr TimePoint max()
Maximum time point (positive duration)
Definition: TimePoint.h:61
constexpr TimePoint()
Initialized to zero time, the clock's epoch.
Definition: TimePoint.h:19
constexpr TimePoint(const TimePoint< Clock, Dur2 > &t)
Construct from a time point with the same clock but different duration.
Definition: TimePoint.h:24
TimePoint(const TimePoint< Clock2, Dur2 > &t)
Construct from a different clock.
Definition: TimePoint.h:27
constexpr bool operator==(const TimePoint< Clock, Dur2 > &rhs) const
Definition: TimePoint.h:38
constexpr bool operator>(const TimePoint< Clock, Dur2 > &rhs) const
Definition: TimePoint.h:47
Dur Duration
Definition: TimePoint.h:16
constexpr TimeCommon operator+(const Duration< Rep, Period > &lhs, const TimePoint< Clock, Dur > &rhs)
operator+(Duration, TimePoint). Returns a time point of best-fit duration.
Definition: TimePoint.h:81
TimePoint & operator+=(const Duration &rhs)
Definition: TimePoint.h:34
constexpr TimeCommon operator+(const TimePoint< Clock, Dur > &lhs, const Duration< Rep, Period > &rhs)
operator+(TimePoint, Duration). Returns a time point of best-fit duration.
Definition: TimePoint.h:74
constexpr Duration time() const
Get duration since clock epoch time.
Definition: TimePoint.h:56
constexpr DurCommon operator-(const TimePoint< Clock, Dur > &lhs, const TimePoint< Clock, Dur2 > &rhs)
operator-(TimePoint, TimePoint). Returns a best-fit duration.
Definition: TimePoint.h:95
TimePoint & operator=(const TimePoint< Clock, Dur2 > &rhs)
Definition: TimePoint.h:30
Global Honeycomb namespace.
constexpr bool operator>=(const TimePoint< Clock, Dur2 > &rhs) const
Definition: TimePoint.h:53