HoverRace  2.0
Duration.h
Go to the documentation of this file.
1 
2 // Duration.h
3 //
4 // Copyright (c) 2013, 2015 Michael Imamura.
5 //
6 // Licensed under GrokkSoft HoverRace SourceCode License v1.0(the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // A copy of the license should have been attached to the package from which
10 // you have taken this file. If you can not find the license you can not use
11 // this file.
12 //
13 //
14 // The author makes no representations about the suitability of
15 // this software for any purpose. It is provided "as is" "AS IS",
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17 // implied.
18 //
19 // See the License for the specific language governing permissions
20 // and limitations under the License.
21 
22 #pragma once
23 
24 #include <iomanip>
25 
26 #include <boost/operators.hpp>
27 
28 #include "MR_Types.h"
29 #include "OS.h"
30 
31 #if defined(_WIN32) && defined(HR_ENGINE_SHARED)
32 # ifdef MR_ENGINE
33 # define MR_DllDeclare __declspec( dllexport )
34 # else
35 # define MR_DllDeclare __declspec( dllimport )
36 # endif
37 #else
38 # define MR_DllDeclare
39 #endif
40 
41 namespace HoverRace {
42 namespace Util {
43 
50  private boost::totally_ordered<Duration, Duration>,
51  private boost::totally_ordered<Duration, MR_Int64>
52 {
53 public:
54  using dur_t = MR_Int64;
55 
61  constexpr Duration(OS::timestamp_t later, OS::timestamp_t earlier)
62  noexcept :
63  duration(OS::TimeDiff(later, earlier)) { }
64 
70  constexpr Duration(const Duration &later, const Duration &earlier)
71  noexcept :
72  duration(later.duration - earlier.duration) { }
73 
78  constexpr Duration(dur_t duration = 0) noexcept : duration(duration) { }
79 
80  Duration(const std::string &s) : Duration(ParseDuration(s)) { }
81 
82  // Fix ambiguity between char* and dur_t.
83  // See specialization for T=char below.
84  template<class T>
85  Duration(const T *s);
86 
87  constexpr Duration(const Duration&) noexcept = default;
88  constexpr Duration(Duration&&) noexcept = default;
89 
90  Duration &operator=(const Duration&) noexcept = default;
91  Duration &operator=(Duration&&) noexcept = default;
92 
93  constexpr Duration operator-() const noexcept
94  {
95  return { -duration };
96  }
97 
102  constexpr dur_t ToMs() const noexcept { return duration; }
103 
104 private:
105  static dur_t ParseDuration(const std::string &s);
106 
107 public:
108  std::ostream &FmtLong(std::ostream &os) const;
109  std::string FmtLong() const;
110  std::ostream &FmtShort(std::ostream &os) const;
111  std::string FmtShort() const;
112 
113 private:
115 
116  friend constexpr bool operator==(const Duration &a, const Duration &b) noexcept;
117  friend constexpr bool operator==(const Duration &duration, Duration::dur_t ts) noexcept;
118  friend constexpr bool operator==(Duration::dur_t ts, const Duration &duration) noexcept;
119  friend constexpr bool operator<(const Duration &a, const Duration &b) noexcept;
120  friend constexpr bool operator<(const Duration &duration, Duration::dur_t ts) noexcept;
121  friend constexpr bool operator<(Duration::dur_t ts, const Duration &duration) noexcept;
122 };
123 
124 template<>
125 inline Duration::Duration(const char *s) : Duration(std::string(s)) { }
126 
127 inline std::ostream &operator<<(std::ostream &os, const Duration &dur)
128 {
129  return dur.FmtShort(os);
130 }
131 
132 inline constexpr bool operator==(const Duration &a, const Duration &b) noexcept
133 {
134  return a.duration == b.duration;
135 }
136 
137 inline constexpr bool operator==(const Duration &a, Duration::dur_t b) noexcept
138 {
139  return a.duration == b;
140 }
141 
142 inline constexpr bool operator==(Duration::dur_t a, const Duration &b) noexcept
143 {
144  return a == b.duration;
145 }
146 
147 inline constexpr bool operator<(const Duration &a, const Duration &b) noexcept
148 {
149  return a.duration < b.duration;
150 }
151 
152 inline constexpr bool operator<(const Duration &a, Duration::dur_t b) noexcept
153 {
154  return a.duration < b;
155 }
156 
157 inline constexpr bool operator<(Duration::dur_t a, const Duration &b) noexcept
158 {
159  return a < b.duration;
160 }
161 
162 } // namespace Util
163 } // namespace HoverRace
164 
165 #undef MR_DllDeclare
constexpr Duration(dur_t duration=0) noexcept
Constructor.
Definition: Duration.h:78
constexpr timestamp_t TimeDiff(timestamp_t laterTs, timestamp_t earlierTs) noexcept
Calculate the difference between two timestamps.
Definition: OS.h:82
constexpr bool operator==(const Duration &a, const Duration &b) noexcept
Definition: Duration.h:132
Duration(const std::string &s)
Definition: Duration.h:80
STL namespace.
friend constexpr bool operator<(const Duration &a, const Duration &b) noexcept
Definition: Duration.h:147
constexpr bool operator<(const Duration &a, const Duration &b) noexcept
Definition: Duration.h:147
std::ostream & operator<<(std::ostream &os, const Clock &clock)
Definition: Clock.h:115
constexpr dur_t ToMs() const noexcept
Convert the duration to milliseconds.
Definition: Duration.h:102
constexpr Duration(OS::timestamp_t later, OS::timestamp_t earlier) noexcept
Constructor from two timestamps.
Definition: Duration.h:61
friend constexpr bool operator==(const Duration &a, const Duration &b) noexcept
Definition: Duration.h:132
std::ostream & FmtShort(std::ostream &os) const
Stream out the short format.
Definition: Duration.cpp:87
MR_Int64 dur_t
Definition: Duration.h:54
constexpr Duration operator-() const noexcept
Definition: Duration.h:93
Definition: Announcement.h:24
int64_t MR_Int64
Definition: MR_Types.h:45
constexpr Duration(const Duration &later, const Duration &earlier) noexcept
Constructor from two durations.
Definition: Duration.h:70
dur_t duration
Definition: Duration.h:114
The relative time between two timestamps.
Definition: Duration.h:49
#define MR_DllDeclare
Definition: Duration.h:38
MR_Int64 timestamp_t
Definition: OS.h:55