CUGL
Cornell University Game Library
CUTimestamp.h
1 //
2 // CUTimestamp.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module simplifies timestamp support a bit. Accurate timestamp support
6 // is necessary for touch and mouse support, but timestamps requires some
7 // really arcane C++11.
8 //
9 // CUGL zlib License:
10 // This software is provided 'as-is', without any express or implied
11 // warranty. In no event will the authors be held liable for any damages
12 // arising from the use of this software.
13 //
14 // Permission is granted to anyone to use this software for any purpose,
15 // including commercial applications, and to alter it and redistribute it
16 // freely, subject to the following restrictions:
17 //
18 // 1. The origin of this software must not be misrepresented; you must not
19 // claim that you wrote the original software. If you use this software
20 // in a product, an acknowledgment in the product documentation would be
21 // appreciated but is not required.
22 //
23 // 2. Altered source versions must be plainly marked as such, and must not
24 // be misrepresented as being the original software.
25 //
26 // 3. This notice may not be removed or altered from any source distribution.
27 //
28 // Author: Walker White
29 // Version: 2/10/15
30 //
31 #ifndef __CU_TIMESTAMP_H__
32 #define __CU_TIMESTAMP_H__
33 #include <chrono>
34 #include "../base/CUBase.h"
35 #include "CUDebug.h"
36 
37 #pragma mark -
38 #pragma mark Clock Data Types
39 
41 typedef std::chrono::steady_clock cuclock_t;
43 typedef cuclock_t::time_point timestamp_t;
44 
45 namespace cugl {
46 
47 #pragma mark -
48 #pragma mark TimeStamp Class
49 
61 class Timestamp {
62 protected:
64  timestamp_t _time;
65 
66 #pragma mark Constructors
67 public:
73  Timestamp() { mark(); }
74 
83  Timestamp(const Timestamp& stamp) { set(stamp); }
84 
95  const Timestamp& operator=(const Timestamp& stamp) {
96  return set(stamp);
97  }
98 
109  const Timestamp& set(const Timestamp& stamp) {
110  _time = stamp.getTime();
111  return *this;
112  }
113 
119  void mark() {
120  _time = cuclock_t::now();
121  }
122 
123 #pragma mark Reading Time
124 
133  timestamp_t getTime() const { return _time; }
134 
145  static Uint64 ellapsedMillis(const Timestamp& start, const Timestamp& end) {
146  auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end.getTime()-start.getTime());
147  return (Uint64)elapsed.count();
148  }
149 
162  Uint64 ellapsedMillis(const Timestamp& stamp) const {
163  return Timestamp::ellapsedMillis(stamp,*this);
164  }
165 
176  static Uint64 ellapsedMicros(const Timestamp& start, const Timestamp& end) {
177  auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end.getTime()-start.getTime());
178  return (Uint64)elapsed.count();
179  }
180 
193  Uint64 ellapsedMicros(const Timestamp& stamp) const {
194  return Timestamp::ellapsedMicros(stamp,*this);
195  }
196 
207  static Uint64 ellapsedNanos(const Timestamp& start, const Timestamp& end) {
208  auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end.getTime()-start.getTime());
209  return (Uint64)elapsed.count();
210  }
211 
224  long ellapsedNanos(const Timestamp& stamp) {
225  return Timestamp::ellapsedNanos(stamp,*this);
226  }
227 
228 #pragma mark Operators
229 
242  Timestamp& operator+=(Uint32 millis) {
243  _time += std::chrono::milliseconds(millis);
244  return *this;
245  }
246 
259  Timestamp& operator-=(Uint32 millis) {
260  _time -= std::chrono::milliseconds(millis);
261  return *this;
262  }
263 
276  const Timestamp operator+(Uint32 millis) const {
277  return Timestamp(*this) += millis;
278  }
279 
292  const Timestamp operator-(Uint32 millis) const {
293  return Timestamp(*this) -= millis;
294  }
295 
296 };
297 
298 }
299 #endif /* __CU_TIMESTAMP_H__ */
long ellapsedNanos(const Timestamp &stamp)
Definition: CUTimestamp.h:224
timestamp_t _time
Definition: CUTimestamp.h:64
static Uint64 ellapsedNanos(const Timestamp &start, const Timestamp &end)
Definition: CUTimestamp.h:207
const Timestamp & operator=(const Timestamp &stamp)
Definition: CUTimestamp.h:95
Definition: CUTimestamp.h:61
const Timestamp operator-(Uint32 millis) const
Definition: CUTimestamp.h:292
static Uint64 ellapsedMicros(const Timestamp &start, const Timestamp &end)
Definition: CUTimestamp.h:176
Timestamp()
Definition: CUTimestamp.h:73
timestamp_t getTime() const
Definition: CUTimestamp.h:133
Timestamp & operator-=(Uint32 millis)
Definition: CUTimestamp.h:259
Timestamp(const Timestamp &stamp)
Definition: CUTimestamp.h:83
Uint64 ellapsedMillis(const Timestamp &stamp) const
Definition: CUTimestamp.h:162
Uint64 ellapsedMicros(const Timestamp &stamp) const
Definition: CUTimestamp.h:193
const Timestamp & set(const Timestamp &stamp)
Definition: CUTimestamp.h:109
Timestamp & operator+=(Uint32 millis)
Definition: CUTimestamp.h:242
Definition: CUAnimationNode.h:52
void mark()
Definition: CUTimestamp.h:119
const Timestamp operator+(Uint32 millis) const
Definition: CUTimestamp.h:276
static Uint64 ellapsedMillis(const Timestamp &start, const Timestamp &end)
Definition: CUTimestamp.h:145