OpenClonk
C4TimeMilliseconds.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2013-2016, The OpenClonk Team and contributors
5  *
6  * Distributed under the terms of the ISC license; see accompanying file
7  * "COPYING" for details.
8  *
9  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
10  * See accompanying file "TRADEMARK" for details.
11  *
12  * To redistribute this file separately, substitute the full license texts
13  * for the above references.
14  */
15 #ifndef INC_C4TimeMilliseconds
16 #define INC_C4TimeMilliseconds
17 
18 /* Class to store times in milliseconds for measurement purposes.
19 
20  This class behaves for the most part like an unsigned integer, with the
21  difference that it handles an overflow correctly. For example:
22  C4TimeMilliseconds start = UINT32_MAX-10;
23  C4TimeMilliseconds stop = 10;
24  start < stop returns true. stop - start is 20.
25 
26  A commonly used operation is to measure the time difference between two
27  C4TimeMilliseconds, that is why if two C4TimeMilliseconds are subtracted from another,
28  the return value is of the type uint32_t.
29 
30  Otherwise, there should be no use case other than for printing, or packing/
31  unpacking for network to have a uint32_t representation of this. You can use
32  AsInt()/AsString() for that. */
33 
35 {
36 public:
37  enum Infinity
38  {
42  };
43 
44 private:
45  uint32_t time{0};
46  Infinity inf{NoInfinity};
47 
48 public:
49 
50  static C4TimeMilliseconds Now();
51 
52  C4TimeMilliseconds() = default;
53  C4TimeMilliseconds(uint32_t millis) : time(millis) { }
54  C4TimeMilliseconds(C4TimeMilliseconds::Infinity infinity) : inf(infinity) { }
55  C4TimeMilliseconds(const C4TimeMilliseconds& rhs) = default;
56  ~C4TimeMilliseconds() = default;
57 
58  /* Returns the stored time. Do not use this for comparisons because this method always
59  returns the stored time, independent of whether this variable is actually infinite. */
60  uint32_t AsInt() const { return time; }
61  /* Returns whether this variable is set to some infinity. This does normally mean that
62  the variable is not initialized yet. */
63  bool IsInfinite() const { return inf != NoInfinity; }
64  /* Returns a string representation useful for debugging and logging purposes. */
65  StdCopyStrBuf AsString() const;
66 
68 
69  inline C4TimeMilliseconds& operator-=(const uint32_t& rhs) { time -= rhs; return *this; }
70  inline C4TimeMilliseconds& operator+=(const uint32_t& rhs) { time += rhs; return *this; }
71 
72  friend bool operator==( const C4TimeMilliseconds& lhs, const C4TimeMilliseconds& rhs );
73  friend bool operator<( const C4TimeMilliseconds& lhs, const C4TimeMilliseconds& rhs );
74  friend int32_t operator-(const C4TimeMilliseconds& lhs, const C4TimeMilliseconds& rhs);
75 };
76 
77 bool operator==( const C4TimeMilliseconds& lhs, const C4TimeMilliseconds& rhs );
78 bool operator<( const C4TimeMilliseconds& lhs, const C4TimeMilliseconds& rhs );
79 
80 inline bool operator!=( const C4TimeMilliseconds& lhs, const C4TimeMilliseconds& rhs ) { return !(lhs == rhs); }
81 inline bool operator>( const C4TimeMilliseconds& lhs, const C4TimeMilliseconds& rhs ) { return rhs < lhs; }
82 inline bool operator<=( const C4TimeMilliseconds& lhs, const C4TimeMilliseconds& rhs ) { return !(lhs > rhs); }
83 inline bool operator>=( const C4TimeMilliseconds& lhs, const C4TimeMilliseconds& rhs ) { return !(lhs < rhs); }
84 
85 int32_t operator-(const C4TimeMilliseconds& lhs, const C4TimeMilliseconds& rhs);
86 
87 inline C4TimeMilliseconds operator+(C4TimeMilliseconds lhs, const uint32_t& rhs) { lhs += rhs; return lhs; }
88 inline C4TimeMilliseconds operator-(C4TimeMilliseconds lhs, const uint32_t& rhs) { lhs -= rhs; return lhs; }
89 
90 #endif
bool operator!=(const C4TimeMilliseconds &lhs, const C4TimeMilliseconds &rhs)
bool operator<=(const C4TimeMilliseconds &lhs, const C4TimeMilliseconds &rhs)
C4TimeMilliseconds operator+(C4TimeMilliseconds lhs, const uint32_t &rhs)
bool operator<(const C4TimeMilliseconds &lhs, const C4TimeMilliseconds &rhs)
bool operator>=(const C4TimeMilliseconds &lhs, const C4TimeMilliseconds &rhs)
bool operator==(const C4TimeMilliseconds &lhs, const C4TimeMilliseconds &rhs)
bool operator>(const C4TimeMilliseconds &lhs, const C4TimeMilliseconds &rhs)
int32_t operator-(const C4TimeMilliseconds &lhs, const C4TimeMilliseconds &rhs)
C4TimeMilliseconds & operator+=(const uint32_t &rhs)
C4TimeMilliseconds()=default
StdCopyStrBuf AsString() const
uint32_t AsInt() const
C4TimeMilliseconds(const C4TimeMilliseconds &rhs)=default
C4TimeMilliseconds(C4TimeMilliseconds::Infinity infinity)
friend bool operator<(const C4TimeMilliseconds &lhs, const C4TimeMilliseconds &rhs)
C4TimeMilliseconds & operator=(const C4TimeMilliseconds &rhs)
C4TimeMilliseconds(uint32_t millis)
friend bool operator==(const C4TimeMilliseconds &lhs, const C4TimeMilliseconds &rhs)
static C4TimeMilliseconds Now()
C4TimeMilliseconds & operator-=(const uint32_t &rhs)
~C4TimeMilliseconds()=default
friend int32_t operator-(const C4TimeMilliseconds &lhs, const C4TimeMilliseconds &rhs)