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== (const EmuTime&) const = default;
37  [[nodiscard]] constexpr auto operator<=>(const EmuTime&) const = default;
38 
39  // arithmetic operators
40  [[nodiscard]] constexpr EmuTime operator+(EmuDuration::param d) const
41  { return EmuTime(time + d.time); }
42  [[nodiscard]] constexpr EmuTime operator-(EmuDuration::param d) const
43  { assert(time >= d.time);
44  return EmuTime(time - d.time); }
45  constexpr EmuTime& operator+=(EmuDuration::param d)
46  { time += d.time; return *this; }
47  constexpr EmuTime& operator-=(EmuDuration::param d)
48  { assert(time >= d.time);
49  time -= d.time; return *this; }
50  [[nodiscard]] constexpr EmuDuration operator-(EmuTime::param e) const
51  { assert(time >= e.time);
52  return EmuDuration(time - e.time); }
53 
54  [[nodiscard]] static constexpr EmuTime zero()
55  {
56  return EmuTime(uint64_t(0));
57  }
58  [[nodiscard]] static constexpr EmuTime infinity()
59  {
60  return EmuTime(std::numeric_limits<uint64_t>::max());
61  }
62 
63  template<typename Archive>
64  void serialize(Archive& ar, unsigned /*version*/)
65  {
66  ar.serialize("time", time);
67  }
68 
69 private:
70  EmuTime() = default; // uninitialized
71  constexpr explicit EmuTime(uint64_t n) : time(n) {}
72 
73 private:
74  uint64_t time;
75 
76  // friends
77  friend EmuTime_param_impl<EmuTime>;
78  friend std::ostream& operator<<(std::ostream& os, EmuTime::param time);
79  friend class DynamicClock;
80  template<unsigned, unsigned> friend class Clock;
81 };
82 
83 template<> struct SerializeAsMemcpy<EmuTime> : std::true_type {};
84 
85 } // namespace openmsx
86 
87 #endif
const EmuDuration & param
Definition: EmuDuration.hh:28
constexpr double e
Definition: Math.hh:18
constexpr vecN< N, T > max(const vecN< N, T > &x, const vecN< N, T > &y)
Definition: gl_vec.hh:276
This file implemented 3 utility functions:
Definition: Autofire.cc:9
bool operator==(const Event &x, const Event &y)
Definition: Event.cc:11
std::ostream & operator<<(std::ostream &os, EnumTypeName< CacheLineCounters >)
void serialize(Archive &ar, T &t, unsigned version)
constexpr uint128 operator-(const uint128 &a, const uint128 &b)
Definition: uint128.hh:185
constexpr uint128 operator+(const uint128 &a, const uint128 &b)
Definition: uint128.hh:181
constexpr auto operator<=>(const uint128 &a, const uint128 &b)
Definition: uint128.hh:224