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 = const 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 assigment operator are ok.
32 
33  static constexpr EmuTime makeEmuTime(uint64_t u) { return EmuTime(u); }
34 
35  // comparison operators
36  constexpr bool operator==(EmuTime::param e) const
37  { return time == e.time; }
38  constexpr bool operator!=(EmuTime::param e) const
39  { return time != e.time; }
40  constexpr bool operator< (EmuTime::param e) const
41  { return time < e.time; }
42  constexpr bool operator<=(EmuTime::param e) const
43  { return time <= e.time; }
44  constexpr bool operator> (EmuTime::param e) const
45  { return time > e.time; }
46  constexpr bool operator>=(EmuTime::param e) const
47  { return time >= e.time; }
48 
49  // arithmetic operators
50  constexpr EmuTime operator+(EmuDuration::param d) const
51  { return EmuTime(time + d.time); }
52  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  constexpr EmuDuration operator-(EmuTime::param e) const
61  { assert(time >= e.time);
62  return EmuDuration(time - e.time); }
63 
64  static constexpr EmuTime zero()
65  {
66  return EmuTime(uint64_t(0));
67  }
68  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  uint64_t time;
84 
85  // friends
86  friend EmuTime_param_impl<EmuTime>;
87  friend std::ostream& operator<<(std::ostream& os, EmuTime::param time);
88  friend class DynamicClock;
89  template<unsigned, unsigned> friend class Clock;
90 };
91 
92 template<> struct SerializeAsMemcpy<EmuTime> : std::true_type {};
93 
94 } // namespace openmsx
95 
96 #endif
constexpr bool operator>=(const uint128 &a, const uint128 &b)
Definition: uint128.hh:237
constexpr bool operator>(const uint128 &a, const uint128 &b)
Definition: uint128.hh:229
vecN< N, T > max(const vecN< N, T > &x, const vecN< N, T > &y)
Definition: gl_vec.hh:287
constexpr bool operator!=(const uint128 &a, const uint128 &b)
Definition: uint128.hh:245
const EmuDuration & param
Definition: EmuDuration.hh:27
constexpr bool operator==(const uint128 &a, const uint128 &b)
Definition: uint128.hh:241
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
constexpr uint128 operator+(const uint128 &a, const uint128 &b)
Definition: uint128.hh:182
std::ostream & operator<<(std::ostream &os, EmuTime::param time)
Definition: EmuTime.cc:6
constexpr bool operator<=(const uint128 &a, const uint128 &b)
Definition: uint128.hh:233
void serialize(Archive &ar, T &t, unsigned version)
constexpr uint128 operator-(const uint128 &a, const uint128 &b)
Definition: uint128.hh:186