openMSX
EmuTime.hh
Go to the documentation of this file.
1 #ifndef EMUTIME_HH
2 #define EMUTIME_HH
3 
4 #include "EmuDuration.hh"
5 #include "serialize.hh"
6 #include <iosfwd>
7 #include <cassert>
8 #include <limits>
9 
10 namespace openmsx {
11 
12 // EmuTime is only a very small class (one 64-bit member). On 64-bit CPUs
13 // it's cheaper to pass this by value. On 32-bit CPUs pass-by-reference
14 // is cheaper.
15 template<typename T, bool C = sizeof(void*) < 8> struct EmuTime_param_impl;
16 template<typename T> struct EmuTime_param_impl<T, true> { // pass by reference
17  using param = const T&;
18  static param dummy() { static constexpr auto e = T::zero(); return e; }
19 };
20 template<typename T> struct EmuTime_param_impl<T, false> { // pass by value
21  using param = T;
22  static param dummy() { return T(); }
23 };
24 
25 class EmuTime
26 {
27 public:
28  using param = EmuTime_param_impl<EmuTime>::param;
29  static param dummy() { return EmuTime_param_impl<EmuTime>::dummy(); }
30 
31  // Note: default copy constructor and assignment operator are ok.
32 
33  static constexpr EmuTime makeEmuTime(uint64_t u) { return EmuTime(u); }
34 
35  // comparison operators
36  [[nodiscard]] constexpr bool operator==(EmuTime::param e) const
37  { return time == e.time; }
38  [[nodiscard]] constexpr bool operator!=(EmuTime::param e) const
39  { return time != e.time; }
40  [[nodiscard]] constexpr bool operator< (EmuTime::param e) const
41  { return time < e.time; }
42  [[nodiscard]] constexpr bool operator<=(EmuTime::param e) const
43  { return time <= e.time; }
44  [[nodiscard]] constexpr bool operator> (EmuTime::param e) const
45  { return time > e.time; }
46  [[nodiscard]] constexpr bool operator>=(EmuTime::param e) const
47  { return time >= e.time; }
48 
49  // arithmetic operators
50  [[nodiscard]] constexpr EmuTime operator+(EmuDuration::param d) const
51  { return EmuTime(time + d.time); }
52  [[nodiscard]] constexpr EmuTime operator-(EmuDuration::param d) const
53  { assert(time >= d.time);
54  return EmuTime(time - d.time); }
55  constexpr EmuTime& operator+=(EmuDuration::param d)
56  { time += d.time; return *this; }
57  constexpr EmuTime& operator-=(EmuDuration::param d)
58  { assert(time >= d.time);
59  time -= d.time; return *this; }
60  [[nodiscard]] constexpr EmuDuration operator-(EmuTime::param e) const
61  { assert(time >= e.time);
62  return EmuDuration(time - e.time); }
63 
64  [[nodiscard]] static constexpr EmuTime zero()
65  {
66  return EmuTime(uint64_t(0));
67  }
68  [[nodiscard]] static constexpr EmuTime infinity()
69  {
70  return EmuTime(std::numeric_limits<uint64_t>::max());
71  }
72 
73  template<typename Archive>
74  void serialize(Archive& ar, unsigned /*version*/)
75  {
76  ar.serialize("time", time);
77  }
78 
79 private:
80  EmuTime() = default; // uninitialized
81  constexpr explicit EmuTime(uint64_t n) : time(n) {}
82 
83 private:
84  uint64_t time;
85 
86  // friends
87  friend EmuTime_param_impl<EmuTime>;
88  friend std::ostream& operator<<(std::ostream& os, EmuTime::param time);
89  friend class DynamicClock;
90  template<unsigned, unsigned> friend class Clock;
91 };
92 
93 template<> struct SerializeAsMemcpy<EmuTime> : std::true_type {};
94 
95 } // namespace openmsx
96 
97 #endif
const EmuDuration & param
Definition: EmuDuration.hh:27
constexpr vecN< N, T > max(const vecN< N, T > &x, const vecN< N, T > &y)
Definition: gl_vec.hh:287
constexpr bool operator==(PoolIndex i, PoolIndex j)
Definition: hash_set.hh:34
constexpr bool operator!=(PoolIndex i, PoolIndex j)
Definition: hash_set.hh:35
This file implemented 3 utility functions:
Definition: Autofire.cc:5
std::ostream & operator<<(std::ostream &os, EnumTypeName< CacheLineCounters >)
void serialize(Archive &ar, T &t, unsigned version)
constexpr bool operator<=(const uint128 &a, const uint128 &b)
Definition: uint128.hh:230
constexpr bool operator>=(const uint128 &a, const uint128 &b)
Definition: uint128.hh:234
constexpr uint128 operator-(const uint128 &a, const uint128 &b)
Definition: uint128.hh:183
constexpr uint128 operator+(const uint128 &a, const uint128 &b)
Definition: uint128.hh:179
constexpr bool operator>(const uint128 &a, const uint128 &b)
Definition: uint128.hh:226
constexpr bool operator<(const uint128 &a, const uint128 &b)
Definition: uint128.hh:222