Honeycomb  0.1
Component-Model Framework
Duration.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 
4 #include "Honey/Math/Ratio.h"
5 #include "Honey/Math/Numeral.h"
6 
7 namespace honey { template<class Rep, class Period> class Duration; }
8 
9 namespace std
10 {
12 
13  template<class Rep, class Period, class Rep2, class Period2>
14  struct common_type<honey::Duration<Rep,Period>, honey::Duration<Rep2,Period2>>
15  {
16  typedef honey::Duration<typename common_type<Rep,Rep2>::type, typename common_type<Period,Period2>::type> type;
17  };
18 }
19 
20 namespace honey
21 {
22 
24 template<class Rep_, class Period_ = Ratio<1>>
25 class Duration
26 {
27  template<class Rep, class Period> friend class Duration;
28 public:
29  typedef Rep_ Rep;
30  typedef Period_ Period;
31 
32  static_assert(Period::num > 0, "Period can't be negative or 0");
33 
34  constexpr Duration() = default;
36  template<class Rep2>
37  constexpr explicit Duration(const Rep2& rep) : _rep(rep) {}
39  template<class Rep2, class Period2, class Ratio = typename Period2::template div<Period>::type>
40  constexpr Duration(const Duration<Rep2,Period2>& d) : _rep(d._rep * Ratio::num / Ratio::den) {}
41 
42  template<class Rep2, class Period2, class Ratio = typename Period2::template div<Period>::type>
43  Duration& operator=(const Duration<Rep2,Period2>& rhs) { _rep = rhs._rep * Ratio::num / Ratio::den; return *this; }
44 
45  constexpr Duration operator+() const { return *this; }
46  Duration& operator++() { ++_rep; return *this; }
47  Duration operator++(int) const { return Duration(_rep++); }
48  Duration& operator+=(const Duration& rhs) { _rep += rhs._rep; return *this; }
49 
50  constexpr Duration operator-() const { return Duration(-_rep); }
51  Duration& operator--() { --_rep; return *this; }
52  Duration operator--(int) const { return Duration(_rep--); }
53  Duration& operator-=(const Duration& rhs) { _rep -= rhs._rep; return *this; }
54 
55  Duration& operator*=(const Rep& rhs) { _rep *= rhs; return *this; }
56  Duration& operator/=(const Rep& rhs) { _rep /= rhs; return *this; }
57  Duration& operator%=(const Rep& rhs) { _rep %= rhs; return *this; }
58  Duration& operator%=(const Duration& rhs) { _rep %= rhs._rep; return *this; }
59 
60  template<class Rep2, class Period2, class DurCommon = typename std::common_type<Duration, Duration<Rep2,Period2>>::type>
61  constexpr bool operator==(const Duration<Rep2,Period2>& rhs) const { return DurCommon(*this)._rep == DurCommon(rhs)._rep; }
62 
63  template<class Rep2, class Period2>
64  constexpr bool operator!=(const Duration<Rep2,Period2>& rhs) const { return !(*this == rhs); }
65 
66  template<class Rep2, class Period2, class DurCommon = typename std::common_type<Duration, Duration<Rep2,Period2>>::type>
67  constexpr bool operator<(const Duration<Rep2,Period2>& rhs) const { return DurCommon(*this)._rep < DurCommon(rhs)._rep; }
68 
69  template<class Rep2, class Period2>
70  constexpr bool operator>(const Duration<Rep2,Period2>& rhs) const { return rhs < *this; }
71 
72  template<class Rep2, class Period2>
73  constexpr bool operator<=(const Duration<Rep2,Period2>& rhs) const { return !(rhs < *this); }
74 
75  template<class Rep2, class Period2>
76  constexpr bool operator>=(const Duration<Rep2,Period2>& rhs) const { return !(*this < rhs); }
77 
79  constexpr Rep count() const { return _rep; }
80 
82  static constexpr Duration zero() { return Duration(0); }
84  static constexpr Duration min() { return Duration(numeral<Rep>().min()); }
86  static constexpr Duration max() { return Duration(numeral<Rep>().max()); }
87 
88 private:
89  Rep _rep;
90 };
91 
95 
97 template<class Rep, class Period, class Rep2, class Period2,
98  class DurCommon = typename std::common_type<Duration<Rep,Period>, Duration<Rep2,Period2>>::type>
99 constexpr DurCommon operator+(const Duration<Rep,Period>& lhs, const Duration<Rep2,Period2>& rhs)
100 {
101  return DurCommon(DurCommon(lhs).count() + DurCommon(rhs).count());
102 }
104 template<class Rep, class Period, class Rep2, class Period2,
105  class DurCommon = typename std::common_type<Duration<Rep,Period>, Duration<Rep2,Period2>>::type>
106 constexpr DurCommon operator-(const Duration<Rep,Period>& lhs, const Duration<Rep2,Period2>& rhs)
107 {
108  return DurCommon(DurCommon(lhs).count() - DurCommon(rhs).count());
109 }
111 template<class Rep, class Period, class Rep2,
112  class DurCommon = Duration<typename std::common_type<Rep,Rep2>::type, Period>>
113 constexpr DurCommon operator*(const Duration<Rep,Period>& lhs, const Rep2& rhs)
114 {
115  return DurCommon(DurCommon(lhs).count() * rhs);
116 }
118 template<class Rep, class Rep2, class Period,
119  class DurCommon = Duration<typename std::common_type<Rep,Rep2>::type, Period>>
120 constexpr DurCommon operator*(const Rep& lhs, const Duration<Rep2,Period>& rhs)
121 {
122  return rhs * lhs;
123 }
125 template<class Rep, class Period, class Rep2,
127  class DurCommon = Duration<typename std::common_type<Rep,Rep2>::type, Period>>
128 constexpr DurCommon operator/(const Duration<Rep,Period>& lhs, const Rep2& rhs)
129 {
130  return DurCommon(DurCommon(lhs).count() / rhs);
131 }
133 template<class Rep, class Period, class Rep2, class Period2,
134  class DurCommon = typename std::common_type<Duration<Rep,Period>, Duration<Rep2,Period2>>::type,
135  class RepCommon = typename std::common_type<Rep,Rep2>::type>
136 constexpr RepCommon operator/(const Duration<Rep,Period>& lhs, const Duration<Rep2,Period2>& rhs)
137 {
138  return DurCommon(lhs).count() / DurCommon(rhs).count();
139 }
141 template<class Rep, class Period, class Rep2,
142  typename mt::disable_if<mt::isSpecializationOf<Rep2, Duration>::value, int>::type=0,
143  class DurCommon = Duration<typename std::common_type<Rep,Rep2>::type, Period>>
144 constexpr DurCommon operator%(const Duration<Rep,Period>& lhs, const Rep2& rhs)
145 {
146  return DurCommon(DurCommon(lhs).count() % rhs);
147 }
149 template<class Rep, class Period, class Rep2, class Period2,
150  class DurCommon = typename std::common_type<Duration<Rep,Period>, Duration<Rep2,Period2>>::type>
151 constexpr DurCommon operator%(const Duration<Rep,Period>& lhs, const Duration<Rep2,Period2>& rhs)
152 {
153  return DurCommon(DurCommon(lhs).count() % DurCommon(rhs).count());
154 }
156 
168 
172 
174 constexpr Nanosec operator"" _ns(unsigned long long int n) { return Nanosec(static_cast<Nanosec::Rep>(n)); }
176 constexpr Microsec operator"" _us(unsigned long long int n) { return Microsec(static_cast<Microsec::Rep>(n)); }
178 constexpr Millisec operator"" _ms(unsigned long long int n) { return Millisec(static_cast<Millisec::Rep>(n)); }
180 constexpr Seconds operator"" _s(unsigned long long int n) { return Seconds(static_cast<Seconds::Rep>(n)); }
182 constexpr Minutes operator"" _min(unsigned long long int n) { return Minutes(static_cast<Minutes::Rep>(n)); }
184 constexpr Hours operator"" _h(unsigned long long int n) { return Hours(static_cast<Hours::Rep>(n)); }
186 
187 }
188 
189 namespace std
190 {
192 
193  template<class Rep, class Period>
194  ostream& operator<<(ostream& os, const honey::Duration<Rep,Period>& d) { return os << d.count(); }
195 }
constexpr bool operator==(const Duration< Rep2, Period2 > &rhs) const
Definition: Duration.h:61
constexpr Duration()=default
Duration< int64, ratio::Unit > Seconds
Definition: Duration.h:163
Rep_ Rep
Definition: Duration.h:29
Period_ Period
Definition: Duration.h:30
constexpr Duration operator-() const
Definition: Duration.h:50
Class to hold compile-time finite rational numbers, ie. the fraction num / den.
Definition: Ratio.h:11
constexpr Duration(const Duration< Rep2, Period2 > &d)
Construct from another type of duration.
Definition: Duration.h:40
Duration & operator*=(const Rep &rhs)
Definition: Duration.h:55
static const int64 num
Definition: Ratio.h:20
Duration & operator++()
Definition: Duration.h:46
Duration< int64, ratio::Micro > Microsec
Definition: Duration.h:161
static const int64 den
Definition: Ratio.h:21
constexpr DurCommon operator/(const Duration< Rep, Period > &lhs, const Rep2 &rhs)
operator/(Duration, Rep). Returns a duration of best-fit period and repetition types.
Definition: Duration.h:128
std::enable_if<!b, T > disable_if
Opposite of std::enable_if.
Definition: Meta.h:62
constexpr bool operator!=(const Duration< Rep2, Period2 > &rhs) const
Definition: Duration.h:64
STL namespace.
constexpr Rep count() const
Get number of repetitions of period.
Definition: Duration.h:79
constexpr DurCommon operator*(const Duration< Rep, Period > &lhs, const Rep2 &rhs)
operator*(Duration, Rep). Returns a duration of best-fit period and repetition types.
Definition: Duration.h:113
Duration operator--(int) const
Definition: Duration.h:52
Duration< int64, ratio::Nano > Nanosec
Definition: Duration.h:160
Duration< int, Seconds::Period::mul< Ratio< 60 > >::type > Minutes
Definition: Duration.h:164
constexpr bool operator>=(const Duration< Rep2, Period2 > &rhs) const
Definition: Duration.h:76
Duration< int64, ratio::Milli > Millisec
Definition: Duration.h:162
constexpr bool operator>(const Duration< Rep2, Period2 > &rhs) const
Definition: Duration.h:70
constexpr DurCommon operator%(const Duration< Rep, Period > &lhs, const Duration< Rep2, Period2 > &rhs)
operator%(Duration, Duration). Returns a duration of best-fit period and repetition types...
Definition: Duration.h:151
constexpr DurCommon operator%(const Duration< Rep, Period > &lhs, const Rep2 &rhs)
operator%(Duration, Rep). Returns a duration of best-fit period and repetition types.
Definition: Duration.h:144
Duration & operator--()
Definition: Duration.h:51
constexpr DurCommon operator+(const Duration< Rep, Period > &lhs, const Duration< Rep2, Period2 > &rhs)
operator+(Duration, Duration). Returns a duration with best-fit period and repetition types...
Definition: Duration.h:99
constexpr Duration operator+() const
Definition: Duration.h:45
constexpr DurCommon operator*(const Rep &lhs, const Duration< Rep2, Period > &rhs)
operator*(Rep, Duration). Returns a duration of best-fit period and repetition types.
Definition: Duration.h:120
Duration & operator%=(const Rep &rhs)
Definition: Duration.h:57
static constexpr Duration max()
Maximum duration (positive reps)
Definition: Duration.h:86
Duration & operator%=(const Duration &rhs)
Definition: Duration.h:58
Duration & operator/=(const Rep &rhs)
Definition: Duration.h:56
Duration< int, Minutes::Period::mul< Ratio< 60 > >::type > Hours
Definition: Duration.h:165
honey::Duration< typename common_type< Rep, Rep2 >::type, typename common_type< Period, Period2 >::type > type
Definition: Duration.h:16
Duration represented by repetitions of a period. The period must be a ratio.
Definition: Duration.h:7
constexpr Duration(const Rep2 &rep)
Construct from repetition.
Definition: Duration.h:37
constexpr DurCommon operator-(const Duration< Rep, Period > &lhs, const Duration< Rep2, Period2 > &rhs)
operator-(Duration, Duration). Returns a duration with best-fit period and repetition types...
Definition: Duration.h:106
constexpr RepCommon operator/(const Duration< Rep, Period > &lhs, const Duration< Rep2, Period2 > &rhs)
operator/(Duration, Duration). Returns a repetition of best-fit type.
Definition: Duration.h:136
static constexpr Duration zero()
Zero duration.
Definition: Duration.h:82
Duration & operator+=(const Duration &rhs)
Definition: Duration.h:48
Duration< int, Hours::Period::mul< Ratio< 24 > >::type > Days
Definition: Duration.h:166
Duration operator++(int) const
Definition: Duration.h:47
Duration & operator=(const Duration< Rep2, Period2 > &rhs)
Definition: Duration.h:43
static constexpr Duration min()
Minimum duration (negative reps)
Definition: Duration.h:84
Duration & operator-=(const Duration &rhs)
Definition: Duration.h:53
Global Honeycomb namespace.